Netweir 0.2.0 is on PyPI. The last post ended with a list of what came next. Two of its three are in this release, along with a few things that list didn’t have.

pip install -U netweir

Forms, logins and APIs

0.1.0 could only send GETs. Now netweir.post() sends a form with form=, or a page script’s request with json= or body=, and netweir.request() takes any method. How they’re sent is what matters to a scraper. A browser posts a form differently from the way a script calls an API, and a server can tell the two apart by their headers and the order they come in. Chrome 154, Firefox 156 and Safari 27 were each recorded doing both, and Netweir’s tests fail if one of its requests is a byte off.

Logging in takes three lines:

async with netweir.Client() as client:
    login = await client.get("https://example.com/login")
    home = await client.submit(login.form(data={"username": "ada", "password": "s3cret"}))

page.form() reads the form the way the browser would submit it, hidden CSRF field included, and the client keeps the cookies the login page set. Redirects after a POST follow the browsers’ rules too: a 303 turns into a GET, and a 307 sends the body again.

Sitemaps

Most sites list their pages in sitemaps, and reading them is often the quickest way to every product. Point sitemap_urls at a sitemap, a sitemap index or the site’s robots.txt, and send entries to callbacks with sitemap_rules. XML, gzipped and plain-text sitemaps all work. A broken one is logged and skipped, and the crawl goes on.

Crawls that stay where you put them, and stop when you say

allowed_domains = ["example.com"] keeps a crawl on that site and its subdomains. Anything else is dropped and counted. And a crawl no longer has to run out of pages to end:

netweir crawl shop.py -o items.jsonl -s max_items=500 -s max_time=600

max_items stops at exactly that many items. max_pages, max_errors and max_time do what their names say, and spider.finish_reason tells you which one ended it. With a checkpoint, the next run carries on from there.

Responses have a ceiling now as well. max_response_size stops reading at 64 MiB by default, counted after decompression, so a small gzip that unpacks to gigabytes doesn’t take your memory with it.

page.metadata()

Product pages often carry their data twice: once for you, and once, more tidily, for search engines. page.metadata() reads all of the second copy in one call, parsed in Rust: JSON-LD, microdata, Open Graph, Twitter cards and Dublin Core.

A cache for the days you spend writing a spider

You’ll run a spider thirty times before it’s right. Without a cache, that’s thirty crawls of the same pages, thirty waits on every polite delay, and thirty rounds of load on a site that never asked for any of it.

netweir crawl shop.py -s cache=.cache

The second run gets every page it already has off your disk. No request goes out, not even for robots.txt. Blocks, errors and 429s are never kept, so a bad answer isn’t replayed, and a cached login still logs you in. The crawling guide has the details.

Fixed

The nightly fuzzer found an XPath expression, a dozen nested predicates deep, that was still running on an eleven-element page after 28 minutes. Each nested predicate tested every node again for each node the one around it tested. Now each node’s answer is worked out once per query. Six levels of nesting went from 1.3 seconds to a quarter of a millisecond, and ordinary queries run as fast as they did.

Two smaller ones: -s max_depth=3 on the command line handed the crawl the string "3", and max_time crashed on Python 3.10 instead of stopping the crawl.

What’s next

  • netweir shell, fetch and view. Open a page, try selectors on it, and see what Netweir saw. With the cache behind it, that loop gets quick.
  • Hooks. Middlewares for requests and responses, and signals such as spider_opened and item_scraped.
  • More ways out. A JSON array, XML, and storage on S3 or GCS.

Every change is on the changelog. Issues and fixes are welcome on GitHub.