Netweir 0.7.0 is on PyPI. It finishes the browser list: finding elements by their text or role, PDFs, tracing and HAR replay. One change needs a minute of your attention if your spiders log in, and I’ll get to that.
pip install -U netweir
Click what the page says
You know the selector. .css-1x9f3k > div:nth-child(2) button, written on Tuesday, broken by Friday because somebody shipped a new build. What didn’t change is the button, which still says “Sign in”.
await page.click("text=Sign in")
await page.click("role=button[name=Sign in]")
await page.fill('role=textbox[name="Email"]', "[email protected]")
text= finds the smallest element holding those words, in either case; put the words in quotes and it wants the whole text, exactly. role= goes by what the element is and what it’s called, reading the name from aria-label, a <label>, alt, the text and a few more. That covers the common cases of the accessible name’s rules, not all of them. Crawl steps take the same selectors.
A recorded site, replayed offline
Debugging a scraper against a live site is slow, and the site keeps changing under you. Save the site once and it can’t:
await page.route_from_har("shop.har")
await page.goto("https://shop.example/")
The page gets the recorded answers and nothing goes to the network. Run it fifty times and you get the same result fifty times. A request the file doesn’t hold fails as if the network were down, so nothing slips out by accident; not_found="fallback" sends those to the network if that’s what you want. A crawl’s har setting writes a file you can replay.
PDFs
await page.pdf("report.pdf", format="A4", landscape=True)
That’s the page laid out for printing, and you get the bytes back as well. Margins, scale, page ranges and custom sizes are there when you need them. Inside a crawl it’s steps.pdf().
Why is this page slow?
await browser.start_tracing(screenshots=True)
await page.goto("https://example.com/")
await browser.stop_tracing("trace.json")
Open the file in Chrome’s Performance panel or at ui.perfetto.dev and you can see what Chrome spent its time on. Tracing slows Chrome down and a trace can run to many megabytes, so save it for the page you’re puzzled by.
If your spiders use auth=, read this
A checkpoint is a file on your disk, and a shared queue is a store other machines can read. Neither was a good place for the password on a request’s login, and both were keeping it. They don’t any more. They keep the user and the domain, and a crawl that resumes or leases that request looks for the password in a new setting, saved_logins, which you can read from wherever you keep secrets.
Without it, the request goes out without its login and the crawl warns you once. A spider using auth= with a shared queue needs the setting even in a single process, since every request goes through the queue. Older checkpoints get scrubbed when you open them. The crawling guide has the details.
And
netweir.parse(html).chunks(1000)raised aTypeErrorin 0.6. Parsed documents and single elements now chunk just as pages do.- A first launch that caught Chrome stuck on its first page used to fail with a timeout. Now it tries once more with a new Chrome.
The release notes have the rest. Next on the roadmap are more engines: Firefox and WebKit once they can be driven without announcing it, and HTTP/3 once a request can go out over it looking like the browser it claims to be. If a selector picks the wrong element, or a HAR won’t replay, open an issue with the page or the file.