【问题标题】:scrape odds using selenium in python在 python 中使用 selenium 来获取赔率
【发布时间】:2020-04-14 09:44:35
【问题描述】:

我想抓取这个页面,在 Python 中使用 Selenium 和 Chrome 驱动程序

https://www.betexplorer.com/soccer/england/premier-league-2018-2019/brighton-manchester-city/UFOgEYGu/

我只对 Bet365 的开盘赔率感兴趣。

enter image description here

bet365_row = driver.find_element_by_xpath("//div[@id='odds-content']").find_element_by_tag_name('tbody').find_element_by_xpath("//tr[@data-bid='16']")
odd1= driver.find_element_by_xpath("//tr[@data-originid='1']").find_element_by_xpath("//td[@class='table-main__detail-odds table-main__detail-odds--first']").find_element_by_xpath("//span[@class='table-main__detail-odds--hasarchive']").text
print(odd1)

我写了这几行代码,但我只能刮取 10Bet 行的表的第一个奇数,但希望 bet365 行的开盘奇数。

【问题讨论】:

  • 为了得到所有的奇数,你必须在driver.find_element_by_xpath 的元素中添加一个's',换句话说,你只会得到第一次出现。试试看:driver.find_elements_by_xpath(...)
  • driver.find_elements_by_xpath("//div[@id='odds-content']") bet365odd=driver.find_elements_by_xpath("//tr[@data-bid='16']").find_elements_by_xpath("//tr[@data-originid='1']").find_elements_by_xpath("//td[@class='table-main__detail-odds table-main__detail-odds--first']").find_elements_by_xpath("//span[@class='table-main__detail-odds--hasarchive']").text 我写了这个,添加了 find_elements,但是我得到了这个错误:AttributeError: 'list' object has no attribute 'find_elements_by_xpath'
  • 是的,因为find_elements_by_xpath 返回一个硒对象列表。如果是我,我会做这样的事情(查看我编辑的回复)

标签: python selenium selenium-webdriver


【解决方案1】:

您可以找到表中的所有行,然后测试该行是否有 bet365:

trs = browser.find_elements_by_xpath(".//div[@id='odds-content']/div/div/table/tbody/*")


for tr in trs:
    if "bet365" in tr.text:
        print(tr.text)
        # Do whatever you want

【讨论】:

    【解决方案2】:

    完美运行。我会通过提取开盘赔率来改进

    enter image description here

     for tr in trs:
        if "bet365" in tr.text:
            odd = driver.find_elements_by_class_name('data-opening-odd').text()
            print(odd)   
    

    但是我收到了这个错误 AttributeError:“列表”对象没有属性“文本”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-04
      • 2021-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-04
      相关资源
      最近更新 更多