Netweir 0.5.0 is on PyPI. The last post ended with a wish: pages that know what they are. That’s most of this release. Give Netweir a product page it has never seen and you get the product back, in the same shape Zyte’s API uses, with no selector written.

pip install -U netweir

A product, out of a page you’ve never looked at

import netweir

page = netweir.get("https://shop.example/p/arc-lamp")
product = page.extract(netweir.auto.Product)
product["name"], product["price"], product["currency"]
# ("Arc desk lamp", "49.99", "GBP")

Most shops already describe their products for search engines, in JSON-LD or microdata, so that’s where Netweir looks first. Where a page has none, it reads the page the way you would: the heading, the price closest to it, the buy button beside them. A struck-through price becomes regular_price. “Save £20” badges, the “You may also like” strip and the basket don’t count. Shops built with Next.js, Nuxt or Apollo, whose HTML is an empty <div> until a script draws it, are read from the state in their scripts, without running anything.

The same goes for category pages (ProductList, with the next page), job adverts (JobPosting, salary range included), articles, blog indexes, jobs boards and forum threads. A thread comes out as its posts, replies to replies included, from XenForo, phpBB, Discourse, GitHub issues, Reddit and the rest.

Every item carries metadata. Its probability says how sure Netweir is that the page holds one at all, and sources says where each field came from: json-ld, or dom: and the CSS path of the element. When one field is wrong for a site (the JSON-LD still has last week’s price, say), pin that field and leave the rest automatic:

class Lumo(netweir.auto.Product):
    price = netweir.css("span.sale")

A pinned field that finds nothing comes out as None with a message, never as the automatic value. If you pinned it, you meant it.

Want to try one before writing any Python? netweir extract URL prints the item as JSON.

A page that says what it is

A crawl that wanders reaches product pages, listings, the blog and the careers page in one run. page.page_type() tells them apart: "product", "product-list", "job-posting", "article", "forum-thread", and so on, or "other" for the About page.

The pages no type covers

A register of licences, a library catalogue, a list of branches. They all repeat, and none of them is a product. page.records() finds the page’s main repeated region and hands you the rows, with columns named from the table header, the labels beside the values, or what the values are:

page.records().rows[0]
# {"name": "Ada Byron", "name_url": "https://example.org/l/1",
#  "licence_no": "L-001", "issued": "2024-01-03"}

It also gives you the selectors it used, so when it picks the wrong region you’ve got a head start on writing your own.

A whole shop from one URL

netweir crawl ecommerce --url https://shop.example/ -o products.jsonl

Netweir finds the categories, follows the sub-categories and the next pages, and extracts every product it reaches. article and job work the same way for a blog or a jobs board. --strategy says how far to go, from pagination_only through one listing to full for the whole site. Every strategy stays on the sites you started from. They’re spiders, so you can subclass one to pin a field.

Half the parse time

Netweir has its own HTML parser now, written in Rust from the HTML Standard, in place of lexbor. Pulling every price, title and link out of a 1 MB shop page took 5.0 ms in 0.4 and takes 3.1 ms now, and XPath went from 9.9 ms to 7.7. It passes the html5lib test suites, and on 50 real pages it builds the same tree lexbor did. The parser post has the details.

You won’t need to change any code, but a few selectors now behave as they do in Chrome and Firefox rather than as they did in lexbor. [href] no longer matches SVG’s xlink:href ([*|href] matches both), #id and .class compare exactly outside quirks mode, and :lexbor-contains is gone. The release notes list them all.

Also

  • Request(url, cb_kwargs={...}) works, and a spider with only start_requests() crawls.
  • A browser launch honours its timeout instead of always waiting 30 seconds.
  • On macOS, the pipes to Chrome no longer leak into processes Netweir starts.
  • netweir queue push with no URLs is now a usage error, not an empty push that looked like success. Thanks to @bfs2021 for that one.
  • Items can hold a page’s Markdown or clean text, and netweir.parse_gtin, parse_rating, parse_availability and parse_salary work on any string.

What’s next

More types: places and businesses, property listings, search results, social posts. Then splitting pages into chunks for retrieval, so a crawl can feed a RAG pipeline directly.

Every change is on the changelog. The auto-extraction guide goes through all of it. If a page reads wrong, open an issue with its URL.