删除使用Selenium刮取时返回的字符串部分

2024-09-27 00:11:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经用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

是否有办法删除随酒店名称返回的评级的这一额外文本行?因为我只想要酒店的名字,因为我用这些名字来比较不同网站的价格。感谢您的帮助


Tags: 代码name名称urlbydriver页面find
3条回答

有可能将其他答案中的想法结合起来,得到更具体的东西,并将其拆分或截断。我注意到这些元素都有一个title属性,名为hotel+“-newwindow”

这意味着如果需要全名,可以执行以下操作:

for work in working:
    title = work.find_element_by_class_name('title__link').get_attribute('title')
    print(title[:-13])#13 is length of ' - New Window'

输出为:

Sofitel London St James
The Savoy
Mercure London Bloomsbury Hotel
Novotel London Waterloo
ibis London Blackfriars
Novotel London Blackfriars
Mercure London Bridge
Novotel London Bridge
ibis Styles London Southwark - near Borough Market
Pullman London St Pancras

或者,如果您决定ibis Styles London Southwark - near Borough Market实际上应该是ibis Styles London Southwark,请改为使用以下选项:

for work in working:
    title = work.find_element_by_class_name('title__link').get_attribute('title')
    print(title.split(' - ')[0])

并获得输出:

Sofitel London St James
The Savoy
Mercure London Bloomsbury Hotel
Novotel London Waterloo
ibis London Blackfriars
Novotel London Blackfriars
Mercure London Bridge
Novotel London Bridge
ibis Styles London Southwark
Pullman London St Pancras

由于您有两个字符串,一个是name,另一个是rating,因此可以拆分字符串,并且只能使用hotel name部分。以下是一个例子:

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)

在您实际进入的元素中names有许多其他内部web元素。
因此,要仅获取所需的元素文本,您必须排除子元素文本。
大概是这样的:

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')
    total = name.text
    children = name.find_flements_by_xpath(".//*")
    for child in children:
        total = total.replace(child.text,'')    
    print(total)

相关问题 更多 >

    热门问题