python - 删除使用 Selenium 抓取时返回的部分字符串
问题描述
在某些信息通过后,我在 Selenium 中编写了代码来抓取Accor 的预订网站。我可以使用此代码在结果页面上抓取并返回所有酒店的名称。
url = 'https://all.accor.com/ssr/app/accor/hotels/london/index.en.shtml?dateIn=2021-08-20&nights=8&compositions=1&stayplus=false'
driver = webdriver.Chrome(executable_path='C:\\Users\\conor\\Desktop\\diss\\chromedriver.exe')
driver.get(url)
time.sleep(10)
working = driver.find_elements_by_class_name('hotel__wrapper')
for work in working:
name = work.find_element_by_class_name('title__link').text
name = name.strip()
print(name)
这将按预期返回页面上的所有酒店名称,但是,它还返回一个带有每个酒店名称的额外行,以及酒店的星级,我在页面上的 HTML 标记中没有看到。这是输出。
Sofitel London St James
5 Star rating
The Savoy
5 Star rating
Mercure London Bloomsbury Hotel
4 Star rating
Novotel London Waterloo
4 Star rating
ibis London Blackfriars
3 Star rating
Novotel London Blackfriars
4 Star rating
Mercure London Bridge
4 Star rating
Novotel London Bridge
4 Star rating
ibis Styles London Southwark - near Borough Market
3 Star rating
Pullman London St Pancras
4 Star rating
有没有办法删除与酒店名称一起返回的评级的额外文本行?因为我只想要酒店名称,因为我使用这些名称来比较不同网站的价格。任何帮助表示赞赏,谢谢。
解决方案
由于您有两个字符串,一个带有名称,另一个带有评级,您可以拆分字符串并且只能使用酒店名称部分。这是示例:
for work in working:
name_with_rating = work.find_element_by_class_name('title__link').text
name = name_with_rating.split("\n")[0]
print(name)
推荐阅读
- http - 如何更改 Pivotal Cloud Foundry Web 应用程序的 URL
- excel - Autoshape 命令出错的运行时错误“1004”
- php - 无法在 PHP 中向数组中添加元素
- r - drake - 映射 ggplot 目标以输出它们
- cpu-architecture - PCIe TLP 仅写入数据包地址 31:2 位
- java - 使用 Flux 持续减少状态
- asp.net - 配置验证上下文以使用 DI 容器
- wpf - WPF CaptureMouse 不会在主窗口之外捕获鼠标事件
- asp.net-mvc - 我无法配置路由
- ruby - 根据某些条件匹配每个值的区域