【问题标题】:Removing parts of a string returned when scraping with Selenium删除使用 Selenium 抓取时返回的部分字符串
【发布时间】:2021-07-27 18:20:32
【问题描述】:

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

有没有办法删除与酒店名称一起返回的评级的额外文本行?因为我只想要酒店名称,因为我使用这些名称来比较不同网站的价格。任何帮助表示赞赏,谢谢。

【问题讨论】:

  • 您是否希望 Ibis Styles London Southwark 包含“ - 近自治市镇市场”?

标签: python selenium web-scraping selenium-chromedriver


【解决方案1】:

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

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)

【讨论】:

    【解决方案2】:

    在您实际进入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)
    

    【讨论】:

      【解决方案3】:

      可以将其他答案中的想法结合起来,以获得更具体的内容并拆分或删除内容。我注意到这些所有元素都有一个带有酒店名称 + ' - 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-31
        • 2014-12-05
        • 2011-03-07
        • 2013-11-07
        • 2019-08-10
        • 1970-01-01
        相关资源
        最近更新 更多