Posts

Showing posts with the label Beautifulsoup

Beautifulsoup Multiple Class Selector

Answer : Use css selectors instead: soup.select('div.A.B') You can use CSS selectors instead, which is probably the best solution here. soup.select("div.classname1.classname2") You could also use a function. def interesting_tags(tag): if tag.name == "div": classes = tag.get("class", []) return "A" in classes and "B" in classes soup.find_all(interesting_tags) table = soup.find_all("tr",class_=["odd","even"]) Try this way! Make sure you are using proper structure of those quotes and braces. It confused me.

Beautiful Soup And Table Scraping - Lxml Vs Html Parser

Answer : There is a special paragraph in BeautifulSoup documentation called Differences between parsers, it states that: Beautiful Soup presents the same interface to a number of different parsers, but each parser is different. Different parsers will create different parse trees from the same document. The biggest differences are between the HTML parsers and the XML parsers. The differences become clear on non well-formed HTML documents. The moral is just that you should use the parser that works in your particular case. Also note that you should always explicitly specify which parser are you using. This would help you to avoid surprises when running the code on different machines or virtual environments. Short answer. If you already installed lxml , just use it. html.parser - BeautifulSoup(markup, "html.parser") Advantages: Batteries included, Decent speed, Lenient (as of Python 2.7.3 and 3.2.) Disadvantages: Not very lenient (before Pytho...