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

Removing parts of a string returned when scraping with Selenium

我已经在 Selenium 中编写了代码,以便在传递某些信息后抓取 Accor's booking website。我可以使用此代码抓取并 return 结果页面上所有酒店的名称。

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)

这 return 页面上的所有酒店名称都符合预期,但是,它还 return 为每个酒店名称和酒店的星级评级增加了一行,这我没有在页面上的 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

有没有办法删除这行额外的文字,因为评级是 return与酒店名称一起使用的?因为我只想要酒店名称,因为我使用这些名称来比较不同网站上的价格。感谢任何帮助,谢谢。

由于您有两个字符串,一个是名称,另一个是评级,您可以拆分字符串,并且只能使用酒店名称部分。这是示例:

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 还有许多其他内部网络元素。
因此,要仅获得所需的元素文本,您必须排除子元素文本。
像这样:

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)

可以将其他答案中的想法结合起来,使某些内容更具体一些,然后拆分或删除一些内容。我注意到这些所有元素都有一个 title 属性,其中包含酒店名称 + ' - New Window'.

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

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