我已经用Selenium编写了代码,以便在传递某些信息后清除Accor's booking website。我可以用这个代码在结果页面上刮取并返回所有酒店的名称
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
是否有办法删除随酒店名称返回的评级的这一额外文本行?因为我只想要酒店的名字,因为我用这些名字来比较不同网站的价格。感谢您的帮助
有可能将其他答案中的想法结合起来,得到更具体的东西,并将其拆分或截断。我注意到这些元素都有一个title属性,名为hotel+“-newwindow”
这意味着如果需要全名,可以执行以下操作:
输出为:
或者,如果您决定
ibis Styles London Southwark - near Borough Market
实际上应该是ibis Styles London Southwark
,请改为使用以下选项:并获得输出:
由于您有两个字符串,一个是name,另一个是rating,因此可以拆分字符串,并且只能使用hotel name部分。以下是一个例子:
在您实际进入的元素中
names
有许多其他内部web元素。因此,要仅获取所需的元素文本,您必须排除子元素文本。
大概是这样的:
相关问题 更多 >
编程相关推荐