News for hackers. Stuff that matters.
Re:Ask HN: What are best tools for web scraping? by Bromskloss on Tuesday November 14, 2017 @08:45AM (#15694868)
> BeautifulSoup / lxml

When should one use one or the other, would you say?

Re:Ask HN: What are best tools for web scraping? by ivan_ah on Tuesday November 14, 2017 @08:52AM (#15694924)
You can use the BeautifulSoup API with the `lxml` parser: https://www.crummy.com/software/BeautifulSoup/bs4/doc/#insta...

I've heard that `lxml` can choke on certain badly-formed markup, but it's very fast. Personally has never failed on me.

Re:Ask HN: What are best tools for web scraping? by nerdponx on Tuesday November 14, 2017 @02:43PM (#15698442)
LXML also is known to have memory leaks [0][1], so be careful using it in any kind of automated system that will be parsing lots of small documents. I personally encountered this issue, and actually caused to abandon a project until months later when I found the references I linked above. It works nice and fast for one-off tasks, though.

Also, a question: how often do you really encounter badly-formed markup in the wild? How hard is it really to get HTML right? It seems pretty simple, just close tags and don't embed too much crazy stuff in CDATA. Yet I often read about how HTML parsers must be "permissive" while XML parsers don't need to be. I've never had a problem parsing bad markup; usually my issues have to do with text encoding (either being mangled directly or being correctly-encoded vestiges of a prior mangling) and the other usual problems associated with text data.

[0]: https://benbernardblog.com/tracking-down-a-freaky-python-mem...

[1]: https://stackoverflow.com/q/5260261

Re:Ask HN: What are best tools for web scraping? by gros_roberts on Tuesday November 14, 2017 @09:19AM (#15695156)
lxml.etree.HTMLParser(recover=True) should work for bad HTML. A few times I had to replace characters before giving the page to lxml, but it was more of an encoding issue.
Re:Ask HN: What are best tools for web scraping? by toutenrab on Tuesday November 14, 2017 @11:12AM (#15696272)
It may not be related but I also noted that processing HTML with lxml (e.g. update every URL of a HTML document with a different domain for instance) was producing malformed HTML with duplicated tags. So I would recommend to use lxml only as a data extraction tool.
Re:Ask HN: What are best tools for web scraping? by j_s on Tuesday November 14, 2017 @03:11PM (#15698682)
Use https://github.com/kovidgoyal/html5-parser, which (in my limited understanding) does a better job faster and is backwards-compatible with both.

Recommendation by the author (of Calibre fame) on a similar discussion: https://news.ycombinator.com/item?id=15539853

Dedicated discussion: https://news.ycombinator.com/item?id=14588333

Re:Ask HN: What are best tools for web scraping? by jackschultz on Tuesday November 14, 2017 @09:00AM (#15694991)
BeautifulSoup. The difference is that lxml can run a little faster in certain cases for a huge scrape, but you'll very very very if ever need that. It's interesting and probably worthwhile to try both and know the difference, but bs BeautifulSoup is definitely where to start
Re:Ask HN: What are best tools for web scraping? by darpa_escapee on Tuesday November 14, 2017 @10:18AM (#15695711)
BeautifulSoup has a friendly API, but it is slow. It has a lxml backend, however.

If you're familiar with writing XPath queries, lxml is great.