Downloading JSON and YAML files while crawling with Playwright
Hi there. Is it possible to detect the
Content-Type
header of responses and download JSON or YAML files? I'm using Playwright to crawl my sites and have some JSON and YAML content I would like to capture, as well.11 Replies
Someone will reply to you shortly. In the meantime, this might help:
foreign-sapphireā¢6mo ago
Yes of course
@Exp just advanced to level 8! Thanks for your contributions! š
xenial-blackOPā¢6mo ago
Thanks, @Exp. What's the best way of accomplishing that? I'm having trouble finding it in the API docs.
foreign-sapphireā¢6mo ago
When you use the Playwright crawlee, you can capture all https request
Did you try it?
xenial-blackOPā¢6mo ago
Before the request handler fires, Playwright waits for the load event. When the crawler encounters JSON or YAML content types, the load event never fires, of course, and the request is aborted. Iām trying to figure out how I can intercept the crawler before that happens and either capture the content or queue it up for a different crawler.
@kevinswiber You can use https://crawlee.dev/api/playwright-crawler/interface/PlaywrightCrawlerOptions#preNavigationHooks for this case. Something like this in js:
preNavigationHooks:[
async (crawlingContext) => {
crawlingContext.page.on('response', async function (response) {
if (response.url().includes('......') {
const body = await response.json();
// check content type here and decide what to do here
const headers = response.headers();
const body = await response.json() // or text()
}
}
)
}
]
xenial-blackOPā¢6mo ago
@azzouzana Thank you! That was helpful. I feel the best solution would probably be creating a new crawler that extends the Playwright crawler. But in the meantime, I'm using this:
Glad it helped @kevinswiber I recently faced a similar case where some of the requests might return raw json, I tried a much simpler approach & it's working (You can inspect the response & check its headers)

xenial-blackOPā¢6mo ago
@azzouzana Is this using the Playwright crawler?
@kevinswiber you're right. It's using Cheerio's