【问题标题】:Table data not available in page_source HTML -Python/Selenium表格数据在 page_source HTML -Python/Selenium 中不可用
【发布时间】:2021-01-17 11:38:17
【问题描述】:

我正在尝试抓取 https://www.oddsportal.com/soccer/chile/primera-division/curico-unido-o-higgins-CtsLggl6/#over-under;2;6.50;0 中包含的赔率数据

我对“Over Under”部分感兴趣,其中有两个提供商 Pinnacle 和 AsianOdds 的具体高于/低于赔率,可以在“比较”按钮后面找到

enter image description here

在使用开发者工具时,可以看到这些都包含在以下HTML中:

   <div class=" deactivateOdd" onmouseout="delayHideTip()" onmouseover="page.hist(this,'P-4.50-0-0','4j5hgx1tkucxe2x0',476,event,0,1)">7.54</div>

我已经让 Selenium 打开页面,切换到 Over/Under 选项卡并展开所有“比较”部分。

url='https://www.oddsportal.com/soccer/chile/primera-division/curico-unido-o-higgins-CtsLggl6/'
browser.get(url)
browser.maximize_window()
time.sleep(2)        


#Open match
match=browser.page_source

# with open("matches.html", "w") as file:
#     file.write(match)

#Click Over/Under Tab
overunder=browser.find_element_by_css_selector("a[title='Over/Under']").click()
#odds_table=browser.find_element_by_class_name("bt-1").click()

#Enter Odds table
odds_table = browser.find_element_by_class_name('bt-1')
browser.execute_script("arguments[0].click();", odds_table)


browser.maximize_window()
odds_rows = WebDriverWait(browser, 10).until(
    EC.presence_of_all_elements_located((By.CSS_SELECTOR, '.table-header-light')))

for i in odds_rows:
    count = i.find_element_by_xpath('./span[@class="odds-cnt"]')
    elem = i.find_elements_by_xpath('.//*[contains(text(),"Compare")]')
    txt = count.text

    if txt != '' and len(elem):
       elem = elem[0]
       browser.execute_script("arguments[0].scrollIntoView();", elem)
       elem.click()

但是在运行时 几率=browser.page_source

使用 open("odds.html", "w") 作为文件: file.write(赔率)

生成的页面源不包含上述包含赔率的 HTML。 我在这里错过了什么?

【问题讨论】:

    标签: python html selenium


    【解决方案1】:

    您已将变量匹配设置为页面源,但尚未使用它。

    match = browser.page_source
    if "deactivateOdd" in match:
       #do something
    else:
       #do something else
    

    我很确定这是您的元素未显示的原因。

    【讨论】:

    • 对不起,我的错!我对页面源所做的操作:odds=browser.page_source with open("odds.html", "w") as file: file.write(odds) 但是生成的文件似乎不包含所需的 HTML ..
    • 啊,好吧,我明白了。是否有任何代码写入文件?如果是这样,该代码是来自您正在抓取的所需页面,还是完全不同的地方?另外,您希望将什么内容写入文件、整个页面或只是几率位?
    • 是的,文件中写入了代码-但不幸的是,它不包括赔率部分。我想提取整个 HTML,然后读出赔率部分
    • 将赔率部分打印到文件中不是更容易吗?或者这不是你要找的?你可以使用 BeautifulSoup 来做到这一点,如果你正在寻找的话,我可以告诉你如何去做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    • 2018-06-30
    • 2012-05-06
    • 2020-06-19
    • 1970-01-01
    • 2021-08-21
    相关资源
    最近更新 更多