我在用硒報廢足球隊名稱時遇到問題。問題是關于主隊和客隊的跨度類是相同的(“Nh”)
<span class="Lh">
<span class="Nh" id="0-639478__match-row__home-team-name">
Estudiantes Merida
</span>
</span>
<span class="Mh">
<span class="Nh" id="0-639478__match-row__away-team-name">
Universidad Central
</span>
</span>
我如何獲得輸出:主隊 vs 客隊?在這種情況下是:Estudiantes Merida vs Universidad Central
我試過這個:
team_home = soup.find_all('span', class_="Nh") 列印(teams_home)
我也嘗試通過 ID 查找,但也沒有用。請問有什么幫助嗎?
網址:https ://www.livescores.com/football/live/? tz=-3,編輯我忘了
uj5u.com熱心網友回復:
您的標題說您在標題中使用了 Selenium,但您的代碼片段使用soup了這讓我相信您使用的是 Beautiful Soup。然而,無論哪種方式,看起來你最好的選擇是獲取類名為“Lh”的元素和類名為“Mh”的元素,然后從那里獲取子文本。
如果使用 Selenium 這對我有用:
driver.get('https://www.livescores.com/football/live/?tz=-3')
WebDriverWait(driver, 60).until(lambda d: d.find_elements(By.CLASS_NAME, 'Lh'))
first = driver.find_elements(By.CLASS_NAME, 'Lh')
second = driver.find_elements(By.CLASS_NAME, 'Mh')
for i, element in enumerate(first):
print(first[i].text ' vs ' second[i].text)
如果使用 BeautifulSoup 這也對我有用:
page = requests.get('https://www.livescores.com/football/live/?tz=-3')
soup = BeautifulSoup(page.content, 'html.parser')
first = soup.find_all(class_='Lh')
second = soup.find_all(class_='Mh')
for i, element in enumerate(first):
print(first[i].text ' vs ' second[i].text)
兩者都給出了輸出:
CA Huracan vs Rosario Central
Independiente vs Talleres
Chapecoense AF vs Criciuma
ASC San Diego vs California United
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/489019.html
上一篇:試圖抓取網頁的內容
下一篇:沒有收到任何HTML回應代碼
