Understand the Find() function in Beautiful Soup

后端 未结 1 1137
萌比男神i
萌比男神i 2020-12-03 02:19

I know what I\'m trying to do is simple but it\'s causing me grief. I\'d like pull data from HTML using BeautifulSoup. To do that I need to properly use the .find()

相关标签:
1条回答
  • 2020-12-03 02:34

    soup.find("div", {"class":"real number"})['data-value']

    Here you are searching for a div element, but the span has the "real number" class in your example HTML data, try instead:

    soup.find("span", {"class": "real number", "data-value": True})['data-value']
    

    Here we are also checking for presence of data-value attribute.


    To find elements having "real number" or "fake number" classes, you can make a CSS selector:

    for elm in soup.select(".real.number,.fake.number"):
        print(elm.get("data-value"))
    

    To get the 69% value:

    soup.find("div", {"class": "percentage good"}).get_text(strip=True)
    

    Or, a CSS selector:

    soup.select_one(".percentage.good").get_text(strip=True)
    soup.select_one(".score .percentage").get_text(strip=True)
    

    Or, locating the h6 element having Audit score text and then getting the preceding sibling:

    soup.find("h6", text="Audit score").previous_sibling.get_text(strip=True)
    
    0 讨论(0)
提交回复
热议问题