【发布时间】:2020-10-04 22:27:45
【问题描述】:
我想提取这个网页中所有soocer事件的url链接:https://www.coteur.com/cotes-foot.php
当我使用 xpath 逐个尝试匹配时,没关系,您可以在下面的代码中看到它,用于夹具 1 和夹具 2。 但是我想自动提取所有足球事件,以便我使用循环但它不起作用。似乎不可能在 xpath 函数中使用循环。如何解决这个问题?
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from selenium import webdriver
from bs4 import BeautifulSoup
driver = webdriver.Firefox()
url = 'https://www.coteur.com/cotes-foot.php'
driver.get(url)
fixture1 = driver.find_element_by_xpath("/html/body/div[3]/div/div[2]/div/div/div[2]/div/table/tbody/tr[3]/td[3]/a")
print(fixture1.text)
fixture2 = driver.find_element_by_xpath("/html/body/div[3]/div/div[2]/div/div/div[2]/div/table/tbody/tr[23]/td[3]/a")
print(fixture2.text, '\n')
links = []
i = 3
while i <= 23:
fixture = driver.find_element_by_xpath("/html/body/div[3]/div/div[2]/div/div/div[2]/div/table/tbody/tr[i]/td[3]/a")
links.append(fixture)
i = i + 1
print(links)
driver.close()
【问题讨论】:
标签: python loops xpath href data-extraction