8 ms·
Requests-HTML: HTML Parsing for Humans
- ecthiender 9y agoKenneth Reitz comes out with yet another good UI (in terms of library) to accomplish daily mundane tasks, with joy.
- sixhobbits 9y agoReally looking forward to using this. BeautifulSoup is great, but it's counterintuitive (I always need to refer to documentation even for very basic aspects that I've used dozens of times before) and it's often slow and has some really weird xml bugs) Also check out newspaper3k if you haven't seen it. It is high level, but really useful for a bunch of simple scraping related use cases
- kcole16 9y agoSame! Python requests is one of my favorite libraries of all time. Kenneth Reitz is a treasure.
- Momquist 9y ago> BeautifulSoup is great, but it's counterintuitive (I always need to refer to documentation even for very basic aspects that I've used dozens of times before) and it's often slow and has some really weird xml bugs) I won't argue the slowness and the occasional bugs, but unlike you I find b4 to be very intuitive. And this is mainly why I use it, despite its faults. Maybe our use case are different, but with a basic knowledge of html, I only rarely find myself reading the documentation. Care to give some examples of what you find counter-intuitive?
- amenod 9y agoOooo... I just finished writing a script with BeautifulSoup. While it wasn't all that bad (it works ;) ), I'm sure the "Kenneth Reitz experience" would be much better. I won't be rewriting the script now, I can't wait to find an excuse to try this. :) EDIT: first commit 22 hours ago - goes to show that when you have thought about the idea and know what you're doing it doesn't take long to produce the first version. :)
- sitkack 9y ago> Kenneth Reitz Experience I heard KRE was going to headline at the upcoming PyCon.
- halflings 9y agoWould not recommend BeautifulSoup for this type of thing. lxml.html is much better in my experience. If you want to use CSS selectors, there's pyquery.
- staticautomatic 9y agoI prefer lxml.etree even for HTML on account of the parser. Either way, I don't understand what's not "for humans" about lxml. It provides a ton of simple and useful abstractions, is easy to learn, and is crazy robust and fast under the hood.
- davidwtbuxton 9y agoYou can also use css selectors with lxml. Works great, in my experience. http://lxml.de/cssselect.html http://lxml.de/cssselect.html
- masklinn 9y ago> lxml.html is much better in my experience. lxml.html has a terrible parser. > If you want to use CSS selectors, there's pyquery. CSS selectors are built into lxml through cssselect[0] which is used to convert CSS3 to XPath 1.0 selectors. [0] If you want to use CSS selectors, there's pyquery.
- ameliaquining 9y agoInteresting! How does this compare with MechanicalSoup (which seems to be the current best-in-class solution for scraping in Python)?
- kenneth_reitz 9y agoVery different use cases, imo. MechanicalSoup emulates a web browser experience. This is more for scraping.
- brilee 9y agoThis is a nice wrapper around requests, pyquery https://github.com/gawel/pyquery/ https://github.com/gawel/pyquery/ and parse https://github.com/r1chardj0n3s/parse https://github.com/r1chardj0n3s/parse of which only requests is Kenneth Reitz. Let's give credit where it's due.
- ak217 9y agoTo give even more credit where it's due, requests is a nice wrapper around urllib3, which is the work of Andrey Petrov, Cory Benfield and contributors. While requests provides good user-friendly defaults and API semantics, urllib3 does a lot of the heavy lifting.
- kenneth_reitz 9y agoIt wasn't at first, actually. It was originally a wrapper around urllib2 — Andrey and I collabed early on in both project's histories to make them what they are today.
- halflings 9y agorequests does much more on top of urllib3 than this new requests-html does on top of requests + pyquery. It greatly simplifies the most common patterns in doing HTTP requests; things like authentication, passing headers, retry logic, etc.
- tty7 9y agoWhich is written in python, so better give credit for that. Wow and now python is in C so line up your credit books, we are in for a long night tonight. Thoughts & prays for all contributors
- d33 9y ago...I know you're trolling, but I seriously wonder where it would stop if we were to go down all that way.
- 9y ago
- vapemaster 9y agoSo stoked this popped up. Literally woke up this morning debating about moving away from bs4 and wrapping the functionality I needed in lxml. I was just thinking I wish there was a requests equivalent for parsing...
- halflings 9y agoIs there any useful feature in bs4 that is not available in lxml.html?
- Const-me 9y agoSame functionality for .NET: http://html-agility-pack.net/ http://html-agility-pack.net/
- zerkten 9y agoThere is overlap, but this isn't the same as Requests-HTML. It is nice that this library has been developed further over the years.
- javajosh 9y agoThe output of r.html.absolute_links on the home page looks like it contains errors. For example, 'https://www.python.org//docs.python.org/3/tutorial/' 'https://www.python.org//docs.python.org/3/tutorial/controlflow.html#defining-functions' I think it's important to remember for every single new library that comes out, that you are trading apparent usability for unknown issues that haven't surfaced yet because the library is so new.
- kenneth_reitz 9y agoThat bug has been fixed, but the docs hadn't been. Fixed now :)
- kenneth_reitz 9y agoLooks like there's still some room for improvement though!
- mixmastamyk 9y agoAlways, very nice work however. Thanks for the pipenv also, makes working with venvs tolerable.
- javajosh 9y agoThere is always room for improvement. I think it's easy to underestimate the time required to dwell in a thing before we really understand it. This is true for runtimes, libraries, even entire programming paradigms. We want to take shortcuts using abstract reasoning, and that works well usually, but sometimes you just gotta use something for years. (Alas, one lifetime may be too short to dwell in all the various paradigms the way they each require. But I digress...)
- nishs 9y ago> Select an element with a jQuery selector. What is a 'jQuery' selector? Is it the same as CSS selectors, or does jQuery support non-standard syntax?
- kenneth_reitz 9y agoupdated :)
- madeofpalk 9y agoFor what it's worth, yes, JQuery does support non-standard syntax/selectors
- friendlydude12 9y ago> Render an Element as Markdown: I prefer my dependencies to be orthogonal and lightweight i.e. do one thing well. Maybe this is better for interactive use in the REPL.
- kenneth_reitz 9y agoI consider it a nice-to-have, but we can definitely remove it if deemed unnecessary. Want to open an issue about it?
- friendlydude12 9y agoThat’s just my preference. Keep the features that are best for your specific use cases. It’s your project after all. I think that’s better than design by committee. Linus didn’t write Linux for other people.
- nojvek 9y agoIf you’re doing markdown conversion well, keep it. Apis are not about doing the smallest thing, it’s about designing a clean abstraction of a problem you’re trying to solve.
- friendlydude12 9y agoSure, if APIs existed within a vacuum. In the real world we have to worry about ease of packaging, ease of maintenance, and various other practical costs.
- 3pt14159 9y agoFor what it is worth, I strongly prefer leaving it in. Sometimes I want to just generate documentation for different things based on different sources and being able to just clean the html by rendering to Markdown would be great.
- mixmastamyk 9y agoCould be made an optional feature in setup.py.
- coolgoose 9y agoAlso in PHP with Symfony Dom Crawler: https://symfony.com/doc/current/components/dom_crawler.html https://symfony.com/doc/current/components/dom_crawler.html or Goutte for an easy to use web scraper https://github.com/FriendsOfPHP/Goutte https://github.com/FriendsOfPHP/Goutte that uses Dom Crawler
- fao_ 9y agoHTML requests are: plain text, self-explanatory ("Content-length", "charset", etc.). What exactly is unhuman about that?
- dullgiulio 9y agoThat's HTTP, not HTML.
- fao_ 9y agoYikes, major misread on my part. Whoops.
- lvh 9y agoHTML request bodies are arbitrary bytes. There are nice ways to do useful things with those arbitrary bytes and less nice ways. This library purports to collect some of the nice ones. "XYZ for Humans" is a trope for the author of this library; for example, requests' tagline is "HTTP for humans".
- acdha 9y agoThat’s HTTP, but both it an HTML share a lot of things which are tedious for humans to deal with – encodings, error handling, handling things like attributes which may but usually do not have multiple values, etc. – and most HTML tooling has user-hostile APIs dating back to the XML era when developer convenience was seen as coddling the weak.
- deleted 9y ago[deleted]
- nbrempel 9y agoI love Kenneth’s work. He’s absolutely focused on user experience – in this case the developer experience – and it absolutely shows. I’m sure I’ll end up using this utility at some point in the future.
- darpa_escapee 9y agoLooks like this is built on top of lxml and parse. I built an adapter over the bs4 interface in lxml, which was much faster than using bs4 with an lxml backend. This is great news, as this space was dominated by bs4 in the Python ecosystem. Can't wait to use this in the future :)
- realhamster 9y agoA similar library is https://github.com/tryolabs/requestium https://github.com/tryolabs/requestium Though it adds parsel as a parser (which has a really nice api) to requests. It also integrates with selenium.
- jacquesm 9y agoThat's a neat little library. The problem is that the web is rapidly moving away from having HTML as the main information carrier to HTML merely being an envelope to deliver a bunch of JavaScript (and if you're even more unlucky: WebAsm). Scraping the web will become a lot harder in the future.
- MildlySerious 9y agoThen again things keep becoming more API driven, with the JS delivered just containing the views. If it gets easier or harder remains to be seen I think
- simonw 9y agoI'm a lot less worried about that thanks to Chrome Headless and the puppeteer library: https://github.com/GoogleChrome/puppeteer https://github.com/GoogleChrome/puppeteer puppeteer makes scripting headless Chrome for scraping-style tasks trivial, and it's supported by the Chrome development team so it's likely to keep on working long into the future.
- ivan_ah 9y agoIs there a difference between Chrome+puppeteer vs Chrome+selenium? Is there anything special that Chrome headless has that ordinary Chrome wouldn't have?
- simonw 9y agoI know plenty of people who dislike working with Selenium. I haven't yet met anyone who's had enough experience with Puppeteer to hate it yet.
- nojvek 9y agoChrome headless is just that. Chrome that’s headless. Selenium is a cross browser api that connects to browser via some port. But usually it’s a high level api since it’s a common denominator of browser apis. Selenium uses middleware webdriver libs sometimes and usually quite bloated. Puppeteer uses Chrome Remote Debug Protocol, which is the same protocol Devtools uses. It’s just a simple JSON RPC over websockets. Puppeteer creates a nice library abstraction over this api. The advantage of puppeteer over selenium is that you have a lot more control over chrome. Network, perf, screenshots, coverage, dom & style traversal, etc. It’s a very reliable API too since Chrome Devtools team maintains the backend. Selenium on the other hand surprises me with all sorts of quirks.
- vinceguidry 9y agoIs there a shell interface, or do I have to call it through python? It would be nice to be able to use it in a Ruby project.
- Lxr 9y agoThis looks awesome, can’t wait to try it. Last time I used pyquery though it was considerably more limited than CSS and jQuery syntax, and I reverted to XPath. Has it improved recently?
- ivan_ah 9y agoNice. It would be pretty cool to have an "run javascript and wait for network idle" optional for scraping js-requiring websites. Can selenium do this? Headless chrome? I personally use bs4 for web scraping and it works pretty well, but if there was an option to also do js with a sane API, I'd switch in a heartbeat.
- bshimmin 9y agoPuppeteer has "networkIdle0" (0 network connections for 500ms) and "networkIdle2" (no more than 2 network connections for 500ms). My experiences with it have been very positive.
- _bohm 9y agoSelenium can absolutely be used for this, the caveat being that it is much slower than using a regular HTML parser. In my experience, it's best to milk as much as you possibly can out of a site's plain HTML and APIs, only resorting to Selenium where it's absolutely necessary.
- nojvek 9y agoWe use puppeteer for our smoke tests. Ensure all network requests load, no JavaScript errors, take screenshot of page and simple validations so we know our deploys aren’t borked. I really love puppeteer over selenium. Much deeper control.
- ospider 9y agoI don't get the point, it's just a nicer alternative to beautifulsoup. Html pages are always changing, instead of writing the parsing logic in code, I think we should put xpath or css expressions in some config files.
- closed 9y agoI'm sure some pages could be represented as css expressions in config files, but in general it seems like scraping is about working around unanticipated idiosyncrasies(e.g. X% of the pages have a different structure for mysterious reasons). I haven't used xpath much though (and it seems pretty beefy!).
- kmike84 9y ago+1 to make nicer APIs! It is always good to have more high-quality API designs to look at. That said, it looks more like an API experiment, not a practical solution for a day job, at least in its current state: * response body encoding detection is wrong, as it doesn't take meta tags or BOM into account; * base url detection is wrong, as it doesn't take <base> tag in account; * URL parsing (joining, etc.) is implemented using string operations instead of stdlib, so a careful inspection is required to make sure it works in edge cases. For example, I can see right away that .absolute_links is wrong for protocol-related urls (e.g. "//ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js") * html2text used for .markdown is GPL - I know people have different opinion on this, but in my book if you import from a GPL package, your package becomes GPL as well; * each .xpath call parses a chnk of HTML again, even if a tree is already present * shortcuts are opinionated and with no clear behavior, e.g. .links deduplicates URLs by default, it deduplicates them using string matches (so e.g. different order of GET arguments => URLs are considering unique); it checks for '.startswith('#")' which looks arbitrary (what if these links are used in a headless browser? what if someone wants to fetch them using _escaped_fragment which many sites still support? why filter out such URLs if they are relative, but not if they are absolute?)
- EmilStenstrom 9y agoWould you mind posting these as issues?
- kmike84 9y agoTBH I don't see myself using this package: in its current stage it is very little code, and almost every method has an issue either with edge cases or with API; also, it is tied to requests library, unnecessarily IMHO, and in my opinion it is GPL even if setup.py says it is MIT. Because there is nothing usable code-wise in requests-html from my point of view (it is no better than existing alternatives), I don't feel like raising these issues, advocating for fixing them, discussing alternative solutions with a goal of improving requests-html. Of course, everyone is free to raise these issues in a repo. I appreciate the work put into requests-html API design, the design is very nice overall. This might be a way to go: create a nice API design, attract people, fix implementation over time, but this battle is not mine, sorry :(