我正在使用 Selenium 來提高足球賠率。團隊在 HTML 中是這樣的:
<div class="participant">
Kansas City Chiefs
<span class='participant country'>
</span>
</div>
金錢線看起來像這樣:
...
<div class="option option-value ng-star-inserted">
<ms-font-resizer maxchars="6"> 145</ms-font-resizer>
</div>
我正在嘗試將球隊(其中一個是堪薩斯城酋長隊)和賠率(此處為 145)提取到串列中。
from selenium import webdriver
import pandas as pd
url = "https://sports.va.betmgm.com/en/sports/football-11/betting/usa-9"
xpaths = ["//div[@class='participant']",
"//div[@class='option option-value ng-star-inserted']"]
with webdriver.Firefox() as driver:
driver.get(url)
teams_elems = driver.find_elements_by_xpath(xpaths[0])
mlines_elems = driver.find_elements_by_xpath(xpaths[1])
mlines, teams = [], []
for m, t in zip(mlines_elems, teams_elems):
mlines.append(m.text)
teams.append(t.text)
driver.close()
這運行沒有錯誤,但元素串列回傳空。我想我使用 XPaths 是錯誤的。我在 DraftKings 上使用了類似的代碼,它可以作業,但不適用于此 HTML。感謝您對此的任何幫助。
uj5u.com熱心網友回復:
mlines 標簽沒有一個名為的'option option-value ng-star-inserted'類'option',它有 3 個名為'option-value'、 和 的類'ng-star-inserted'。
如果要根據多個類定位標簽,則需要使用contains.
"//div[contains(@class, 'option option-value ng-star-inserted')]"
或者,如果您想以任何順序查找這些類,請嘗試
"//div[contains(@class, 'option')][contains(@class, 'option-value')][contains(@class, 'ng-star-inserted')]"
uj5u.com熱心網友回復:
嘗試使用以下 xpaths 并確認:
driver.get("https://sports.va.betmgm.com/en/sports/football-11/betting/usa-9")
i = 0
try:
while True:
teams = driver.find_elements_by_xpath("//ms-six-pack-event//div[@class='participant']")
mlines = driver.find_elements_by_xpath("//ms-six-pack-event//div[@class='option-indicator']//following-sibling::div/ms-font-resizer")
driver.execute_script("arguments[0].scrollIntoView(true);",teams[i])
print(teams[i].text,mlines[i].text)
i = 1
time.sleep(.5)
except:
pass
Denver Broncos 1.91
Cleveland Browns 1.91
Washington Football Team 1.91
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/324927.html
上一篇:Python-簡單的網頁抓取不拉
