[英]Parsing HTML span with Beautiful Soup
我试图弄清楚如何使用美丽汤,并且遇到了困难。
我的HTML页面具有以下几个元素:
<a class="propertyName" href="/preferredguest/property/overview/index.html?propertyID=1023"><span>The Westin Peachtree Plaza, Atlanta
</span></a>
<a class="propertyName" href="/preferredguest/property/overview/index.html?propertyID=1144"><span>Sheraton Atlanta Hotel
</span></a>
我正在尝试使用酒店名称创建一个数组。 到目前为止,这是我的代码:
import requests
from bs4 import BeautifulSoup
url = "removed"
response = requests.get(url)
soup = BeautifulSoup(response.text)
hotels = soup.find_all('a', class_="propertyName")
但是我不知道如何遍历Hotels数组以显示span元素。
您的“旅馆”名称在span
。 一种方法是使用.select()
方法
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup('''<a class="propertyName" href="/preferredguest/property/overview/index.html?propertyID=1023"><span>The Westin Peachtree Plaza, Atlanta
... </span></a>
...
... <a class="propertyName" href="/preferredguest/property/overview/index.html?propertyID=1144"><span>Sheraton Atlanta Hotel
... </span></a>
... ''', 'lxml')
>>> [element.get_text(strip=True) for element in soup.select('a.propertyName > span')]
['The Westin Peachtree Plaza, Atlanta', 'Sheraton Atlanta Hotel']
>>>
要么
>>> names = []
>>> for el in hotels:
... names.append(el.find('span').get_text(strip=True))
...
>>> names
['The Westin Peachtree Plaza, Atlanta', 'Sheraton Atlanta Hotel']
>>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.