如何使用beautifulsoup提取h1标签文本

时间:2016-11-21 09:04:46

标签: python beautifulsoup

我想了解如何使用漂亮的汤来提取包含许多其他标签的h1标签文本:

<h1 class="listing-name">
Hôtel Vevey 
<span class="entry-feedbacks-summary-title-rating-stars-container bootstrap">
<span class="entry-feedbacks-summary-title-rating-stars entry-feedbacks-summary-title-rating-stars-empty" data-container=".entry-feedbacks-summary-title-rating-stars-container" data-content="Il n'y a pas encore d'avis de clients à propos de Astra Hôtel Vevey 4*sup. Cliquez pour évaluer." data-placement="right" data-toggle="popover" data-trigger="hover" data-original-title="" title="">
<a class="feedback-login-link entry-feedbacks-header-link" href="/auth/localch?origin=https%3A%2F%2Ftel.local.ch%2Ffr%2Fd%2FVevey%2F1800%2FHotel%2FAstra-Hotel-Vevey-4sup-SVGb8b5z-QdrzGTddmyAAg%3Fwhat%3DHotel%26where%3DVaud%2B%2528Canton%2529%23entry-feedbacks-bottom-rate-button"><span class="entry-feedback-rating-star">
<i class="icon-star-outline entry-feedback-rating-star-empty"></i>
</span>
<span class="entry-feedback-rating-star">
<i class="icon-star-outline entry-feedback-rating-star-empty"></i>
</span>
<span class="entry-feedback-rating-star">
<i class="icon-star-outline entry-feedback-rating-star-empty"></i>
</span>
<span class="entry-feedback-rating-star">
<i class="icon-star-outline entry-feedback-rating-star-empty"></i>
</span>
<span class="entry-feedback-rating-star">
<i class="icon-star-outline entry-feedback-rating-star-empty"></i>
</span>

</a></span>

</span>
</h1>

我正在尝试在h1标签“hôtelVevey”之后提取文本。

import requests
from bs4 import BeautifulSoup

url = "https://tel.local.ch/fr/d/Vevey/1800/Hotel/Astra-Hotel-Vevey-4sup-SVGb8b5z-QdrzGTddmyAAg?what=Hotel&where=Vaud+%28Canton%29"
get_url = requests.get(url)
get_text = get_url.text
soup = BeautifulSoup(get_text, "html.parser")

company = soup.find_next('h1', 'class:listing-name')


print(company)

它让我“无”

2 个答案:

答案 0 :(得分:6)

对于您提供的当前链接,您可以这样做:

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

输出:

Astra Hôtel Vevey 4*sup

答案 1 :(得分:2)

尝试使用字典:

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

或以下内容:

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

请注意课后的下划线。这是因为class是python中的保留字。

可以在此处找到更多信息:https://www.crummy.com/software/BeautifulSoup/bs4/doc/#attrs