Netweir 0.3.1 is on PyPI. The last post promised a shell, hooks and more ways to write items out. All three are here, and the list kept growing after that.

pip install -U netweir

Why 0.3.1 and not 0.3.0? The 0.3.0 release got as far as testing its own wheels and stopped, because the tests needed packages the release job didn’t install. Nothing reached PyPI. The tag stays, and this is the same code, plus the fix to the release job.

Try a selector before it goes in a spider

netweir shell https://books.toscrape.com/

That’s a Python prompt with the page in page, fetched as Chrome would fetch it. Try page.css("h3 a::text").getall(), change it, try it again. netweir fetch URL writes the body to stdout, and netweir view URL opens what Netweir got in your browser. When a site serves Netweir something different from what you see, view is how you find out.

Scrapy’s hooks, where you’d expect them

Middlewares see each request on the way out and each response on the way back. Signals such as spider_opened and item_scraped fire when you’d expect, and pipelines get open_spider and close_spider. They work the way Scrapy’s do, so simple Scrapy middlewares and pipelines carry over as they are.

And if you’d rather stay on Scrapy, you can, and fetch with Netweir anyway:

DOWNLOAD_HANDLERS = {
    "http": "netweir.scrapy.DownloadHandler",
    "https": "netweir.scrapy.DownloadHandler",
}

Every request then goes out as Chrome, Firefox or Safari sends it, from the TLS handshake to the order of the headers. The rest of your project doesn’t change.

More ways out

Items go to JSON, XML, JSON Lines, CSV or Parquet, written in Rust. Give -o an s3:// or gs:// URL and the file is uploaded when the crawl finishes. batch=1000 starts a new file every thousand items. JSON and XML come out as Scrapy writes them, so whatever reads your Scrapy output reads this too.

A crawl that finished isn’t always a crawl that worked

A site changes its markup on a Tuesday. Your crawl still runs, still exits 0, and writes a thousand items with no price. Checks catch that:

class Books(netweir.Spider):
    checks = [netweir.Checks(min_items=900, coverage={"title": 1.0, "price": 0.95})]

When one fails, netweir crawl exits 1, so cron or CI notices. netweir.validate checks each item against a dataclass or a JSON Schema on the way through. And max_memory stops a crawl that’s using more memory than you gave it, before the machine runs short.

Proxy pools

Give a crawl a list of proxies and pick how it uses them: a new one for every request, one per site, or none until a site blocks you. A proxy that keeps failing sits out for a while, and proxy_stats tells you which ones are carrying the crawl.

Skip what you already have

You crawl a shop every morning. Yesterday you got ten thousand products. Today maybe forty are new, and fetching all ten thousand to find them is slow for you and rude to the shop.

netweir crawl shop.py -o shop.jsonl -s deltafetch=state/shop

A page that gave an item before isn’t fetched again. Listing pages give links, not items, so they’re read every run, and that’s how the forty get found. The output file keeps every run’s items, so shop.jsonl holds yesterday’s ten thousand and today’s forty. A crash part way through doesn’t lose any of them.

Keep what you fetched

Sometimes you need the site, not just the items: proof of what a page said, pages to parse again next month without asking twice, or the exact request that got a strange answer.

netweir crawl shop.py -s warc=crawl.warc.gz -s har=crawl.har

The WARC file holds every response, each with the request that asked for it, header for header as it went out. Redirect hops, retries, block pages and robots.txt are in there too. Read it with warcio. The HAR file opens in a browser’s network panel.

Spiders as a service

netweir serve --project shop=spiders/
curl http://127.0.0.1:6800/schedule.json -d project=shop -d spider=books

netweir serve runs spiders as jobs you schedule, watch and cancel over HTTP, and keeps each job’s log, items and stats. It speaks much of scrapyd’s API, so what you used with scrapyd mostly works. A bad setting is refused when you schedule, not an hour into the job. Cancelling a job saves its items and checkpoint before it stops. It listens on 127.0.0.1 by default, and won’t listen anywhere else without a password unless you tell it to. The guide has the rest.

One crawl, many machines

netweir crawl books.py -o "items-$(hostname).jsonl" --queue redis://10.0.0.5:6379/0

Run that on four machines and they share one crawl: one request queue and one duplicate filter, in Redis, as with scrapy-redis. Each request goes to one machine, whichever asks first. If a machine dies, the others pick up the requests it was holding once its lease runs out. You can feed a running crawl from anywhere with netweir queue push. One crawl, many machines covers what each machine keeps to itself, and how to keep the Redis safe.

Also

  • parse_price turns £1,234.50 or 1.234,50 € into a Decimal and a currency. parse_date reads ISO and HTTP dates, month names in seven languages, and 3 days ago. page.js_data("__INITIAL_STATE__") reads the data pages keep in their scripts.
  • On Windows, a crawl that stopped while worker processes were still busy could log “Future exception was never retrieved”. It doesn’t any more.

What’s next

Firefox in the browser driver, once there’s a Firefox that doesn’t announce it’s being automated. Until then, there’s the Chrome driver.

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