Live data from Hacker News

Beautiful Soup

crummy.com

71–80 of 95 posts

Re: Beautiful Soup

#71

Great memories with this library, one of my all time favs. It is fast? no. But it had a fantastic mission: extracting data from malformed HTML. Might be less common now but back then (~10+ years ago) it was still rampant. Many if not most parsers would barf on any deviation from the standard, leaving you to hand-roll regex solutions and ugly corner cases. BS covered a LOT of these cases without forcing you to write t…

It's actually pretty trivial to speed up, if you have multiple documents to parse, you can use multiprocessing.

    from multiprocessing import Pool
    
    def parse(html):
      result = []
      soup = BeautifulSoup(html, 'html.parser')
      for p in soup.select('div > p'):
        result.append(p.text)
      return result
    
    with Pool(processes=16) as pool:
      for texts in pool.imap_unordered(parse, my_html_texts):
        for text in texts:
          print(text)

Re: Beautiful Soup

#72

I used this recently to scrape a bunch of online restaurant reviews by a guy who I really like, use regex to get the postcode from the markup, do a geocode using postcodes.io, then plot the reviews on a Google map. It took about two / three hours and felt kinda dirty in a good way. Beautiful Soup made the first part really easy.

Hmm, cool project idea but if the subject doesn't know you're doing this, it is a bit weird to stalk their online footprint. Unless you mean the person is a professional restaurant reviewer and you like their opinion. Oh well, good luck either way.

"Hmm, cool project idea but if the subject doesn't know you're doing this, it is a bit weird to stalk their online footprint."

Wait, which one is the real cyberlurker? ;)

Re: Beautiful Soup

#73

Earlier quoted context omitted.

I’ve found Playwright to be a really great tool for scraping

Headless scraping is in the region of 10x slower and more resource intensive, even when carefully blocking requests such as images. It should always be a second choice. Other than that Playwright is incredible, by far the best browser automation api.

For sure it’s a heavy approach, but if you need a full blown browser with JS, then that’s just what you’ll have to do. Use the right tool for the job.

Re: Beautiful Soup

#74

Great memories with this library, one of my all time favs. It is fast? no. But it had a fantastic mission: extracting data from malformed HTML. Might be less common now but back then (~10+ years ago) it was still rampant. Many if not most parsers would barf on any deviation from the standard, leaving you to hand-roll regex solutions and ugly corner cases. BS covered a LOT of these cases without forcing you to write t…

I'm not sure why you're using the past tense, the current version is only a year old, and I will never stop using it because panning messy datasets for hidden gold is my thing.

Re: Beautiful Soup

#75
post #28

Great memories with this library, one of my all time favs. It is fast? no. But it had a fantastic mission: extracting data from malformed HTML. Might be less common now but back then (~10+ years ago) it was still rampant. Many if not most parsers would barf on any deviation from the standard, leaving you to hand-roll regex solutions and ugly corner cases. BS covered a LOT of these cases without forcing you to write t…

"Might be less common now" It is less necessary now. One of the most important parts of the HTML5 standards, IMHO, is that it specifies how to parse HTML that doesn't conform to the standards in a standard way. In principle, every bag of bytes now has a standard-compliant way to parse it that every HTML5 parser should agree on. I don't use this to know how many edge cases the standard and/or implementations have, but…

Not just HTML standards, but I've used their detwingle function because one of the sites I'm scraping has a mixture of Windows-1252 and Unicode. It was clearly stored correctly, but encoded differently depending on what page view you were looking at. For example titles in an outline were broken, but on the actual individual pages fine. Their rendering also treated multibyte characters incorrectly during truncation.

Re: Beautiful Soup

#78

Great memories with this library, one of my all time favs. It is fast? no. But it had a fantastic mission: extracting data from malformed HTML. Might be less common now but back then (~10+ years ago) it was still rampant. Many if not most parsers would barf on any deviation from the standard, leaving you to hand-roll regex solutions and ugly corner cases. BS covered a LOT of these cases without forcing you to write t…

I'm not sure why you're using the past tense, the current version is only a year old, and I will never stop using it because panning messy datasets for hidden gold is my thing.

What type of datasets?

Re: Beautiful Soup

#79

Earlier quoted context omitted.

I'm not sure why you're using the past tense, the current version is only a year old, and I will never stop using it because panning messy datasets for hidden gold is my thing.

What type of datasets?

Things with janky APIs or built on abandonware like imageboards, niche forums, dead comment communities, data dumps etc.

Re: Beautiful Soup

#80
post #71

Great memories with this library, one of my all time favs. It is fast? no. But it had a fantastic mission: extracting data from malformed HTML. Might be less common now but back then (~10+ years ago) it was still rampant. Many if not most parsers would barf on any deviation from the standard, leaving you to hand-roll regex solutions and ugly corner cases. BS covered a LOT of these cases without forcing you to write t…

It's actually pretty trivial to speed up, if you have multiple documents to parse, you can use multiprocessing. from multiprocessing import Pool def parse(html): result = [] soup = BeautifulSoup(html, 'html.parser') for p in soup.select('div > p'): result.append(p.text) return result with Pool(processes=16) as pool: for texts in pool.imap_unordered(parse, my_html_texts): for text in texts: print(text)

it's a pretty common case to be parsing thousands of locally-stored pages. There are only so many cores and the task of scraping an entire site can still easily still be CPU limited.
Post reply on HN