{"by":"rodricios","descendants":9,"id":46618472,"kids":[46698193,46698645,46696529],"score":64,"text":"<i>wxpath</i> is a declarative web crawler where web crawling and scraping are expressed directly in XPath.<p>Instead of writing imperative crawl loops, you describe what to follow and what to extract in a single expression:<p><pre><code>    import wxpath\n\n    # Crawl, extract fields, build a Wikipedia knowledge graph\n    path_expr = &quot;&quot;&quot;\n    url(&#x27;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Expression_language&#x27;)\n         &#x2F;&#x2F;&#x2F;url(&#x2F;&#x2F;main&#x2F;&#x2F;a&#x2F;@href[starts-with(., &#x27;&#x2F;wiki&#x2F;&#x27;) and not(contains(., &#x27;:&#x27;))])\n             &#x2F;map{\n                &#x27;title&#x27;: (&#x2F;&#x2F;span[contains(@class, &quot;mw-page-title-main&quot;)]&#x2F;text())[1] ! string(.),\n                &#x27;url&#x27;: string(base-uri(.)),\n                &#x27;short_description&#x27;: &#x2F;&#x2F;div[contains(@class, &#x27;shortdescription&#x27;)]&#x2F;text() ! string(.),\n                &#x27;forward_links&#x27;: &#x2F;&#x2F;div[@id=&quot;mw-content-text&quot;]&#x2F;&#x2F;a&#x2F;@href ! string(.)\n             }\n    &quot;&quot;&quot;\n\n    for item in wxpath.wxpath_async_blocking_iter(path_expr, max_depth=1):\n        print(item)\n</code></pre>\nThe key addition is a `url(...)` operator that fetches and returns HTML for further XPath processing, and `&#x2F;&#x2F;&#x2F;url(...)` for deep (or paginated) traversal. Everything else is standard XPath 3.1 (maps&#x2F;arrays&#x2F;functions).<p>Features:<p>- Async&#x2F;concurrent crawling with streaming results<p>- Scrapy-inspired auto-throttle and polite crawling<p>- Hook system for custom processing<p>- CLI for quick experiments<p>Another example, paginating through HN comments (via &quot;follow=&quot; argument) pages and extracting data:<p><pre><code>    url(&#x27;https:&#x2F;&#x2F;news.ycombinator.com&#x27;,\n        follow=&#x2F;&#x2F;a[text()=&#x27;comments&#x27;]&#x2F;@href | &#x2F;&#x2F;a[@class=&#x27;morelink&#x27;]&#x2F;@href)\n        &#x2F;&#x2F;tr[@class=&#x27;athing&#x27;]\n          &#x2F;map {\n            &#x27;text&#x27;: .&#x2F;&#x2F;div[@class=&#x27;comment&#x27;]&#x2F;&#x2F;text(),\n            &#x27;user&#x27;: .&#x2F;&#x2F;a[@class=&#x27;hnuser&#x27;]&#x2F;@href,\n            &#x27;parent_post&#x27;: .&#x2F;&#x2F;span[@class=&#x27;onstory&#x27;]&#x2F;a&#x2F;@href\n          }\n</code></pre>\nLimitations: HTTP-only (no JS rendering yet), no crawl persistence. \nBoth are on the roadmap if there&#x27;s interest.<p>GitHub: <a href=\"https:&#x2F;&#x2F;github.com&#x2F;rodricios&#x2F;wxpath\" rel=\"nofollow\">https:&#x2F;&#x2F;github.com&#x2F;rodricios&#x2F;wxpath</a><p>PyPI: pip install wxpath<p>I&#x27;d love feedback on the expression syntax and any use cases this might unlock.<p>Thanks!","time":1768409554,"title":"Show HN: wxpath – Declarative web crawling in XPath","type":"story","url":"https://github.com/rodricios/wxpath"}