如何使用 python Selenium 获取多个匹配的链接(href)?

问题描述 投票:0回答:1

如果您访问此网站,

https://www.premierleague.com/results

您将能够看到多场比赛的结果。如果您点击每场比赛,您将被定向到另一个网站。

我的问题是如何获得每场比赛的href(链接)。

links = driver.find_elements(By.XPATH, '//*[@id="mainContent"]/div[3]/div[1]')
for link in links:
    x = link.get_attribute("href")
    List.append(x)

这是我到目前为止所拥有的,但它不起作用。

python selenium-webdriver web-scraping
1个回答
1
投票

我看到类似的元素

<div data-href="//www.premierleague.com/match/66686" ...>

你可以搜索

//div[@data-href]

然后使用

get_attribute("data-href")


完整的工作代码

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
#from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager
#import time

url = 'https://www.premierleague.com/results'

#driver = webdriver.Chrome(executable_path=ChromeDriverManager().install())
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())

driver.get(url)

wait = WebDriverWait(driver, 10)

#time.sleep(5)

# close popup window with "Accept All Cookies"
button = wait.until(EC.visibility_of_element_located((By.XPATH, '//button[text()="Accept All Cookies"]')))
button.click()

all_items = driver.find_elements(By.XPATH, '//div[@data-href]')
print('len(all_items):', len(all_items))
for item in all_items:
    print(item.get_attribute('data-href'))

结果:

len(all_items): 40

//www.premierleague.com/match/66686
//www.premierleague.com/match/66682
//www.premierleague.com/match/66687
//www.premierleague.com/match/66689
//www.premierleague.com/match/66691
//www.premierleague.com/match/66684
//www.premierleague.com/match/66705
//www.premierleague.com/match/66677
//www.premierleague.com/match/66674
//www.premierleague.com/match/66675
//www.premierleague.com/match/66676
//www.premierleague.com/match/66679
//www.premierleague.com/match/66672
//www.premierleague.com/match/66678
//www.premierleague.com/match/66680
//www.premierleague.com/match/66681
//www.premierleague.com/match/66673
//www.premierleague.com/match/66633
//www.premierleague.com/match/66584
//www.premierleague.com/match/66513
//www.premierleague.com/match/66637
//www.premierleague.com/match/66636
//www.premierleague.com/match/66635
//www.premierleague.com/match/66666
//www.premierleague.com/match/66670
//www.premierleague.com/match/66668
//www.premierleague.com/match/66665
//www.premierleague.com/match/66667
//www.premierleague.com/match/66669
//www.premierleague.com/match/66654
//www.premierleague.com/match/66656
//www.premierleague.com/match/66659
//www.premierleague.com/match/66657
//www.premierleague.com/match/66655
//www.premierleague.com/match/66652
//www.premierleague.com/match/66660
//www.premierleague.com/match/66661
//www.premierleague.com/match/66653
//www.premierleague.com/match/66658
//www.premierleague.com/match/66524
© www.soinside.com 2019 - 2024. All rights reserved.