How to extract h1 tag text with beautifulsoup

前端 未结 2 1028
温柔的废话
温柔的废话 2021-01-06 12:01

I\'d like to understand how to extract a h1 tag text which contains many others tags in it using beautiful soup :

相关标签:
2条回答
  • 2021-01-06 12:15

    For the current link that you have provided you can get it like this:

    company = soup.select('h1.listing-name')[0].text.strip()
    print(company)
    

    Output:

    Astra Hôtel Vevey 4*sup
    
    0 讨论(0)
  • 2021-01-06 12:15

    Try using a dictionary:

    company = soup.find('h1', {'class' : 'listing-name'})
    

    Or the following:

    company = soup.find('h1', class_ ='listing-name')
    

    Note the underscore after class. This is because class is a reserved word in python.

    More info can be found here: https://www.crummy.com/software/BeautifulSoup/bs4/doc/#attrs

    0 讨论(0)
提交回复
热议问题