代码提案:
收集网页(https://int.soccerway.com/matches/2021/07/28/)上当天所有游戏的链接,让我可以自由更改日期,如2021/08/01
等。因此,将来我可以在一次代码调用中同时循环和收集来自不同日期的列表
尽管它是一个非常慢的模型,但不使用Headless
,该模型单击所有按钮,展开数据并导入所有列出的465个匹配链接:
for btn in driver.find_elements_by_xpath("//tr[contains(@class,'group-head clickable')]"):
btn.click()
完整代码:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
options = Options()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-logging"])
driver = webdriver.Chrome(r"C:\Users\Computador\Desktop\Python\chromedriver.exe", options=options)
url = "https://int.soccerway.com/matches/2021/07/28/"
driver.get(url)
driver.find_element_by_xpath("//div[@class='language-picker-trigger']").click()
driver.find_element_by_xpath("//a[@href='https://int.soccerway.com']").click()
time.sleep(10)
for btn in driver.find_elements_by_xpath("//tr[contains(@class,'group-head clickable')]"):
btn.click()
time.sleep(10)
jogos = driver.find_elements_by_xpath("//td[contains(@class,'score-time')]//a")
for jogo in jogos:
resultado = jogo.get_attribute("href")
print(resultado)
driver.quit()
但是,当我添加options.add_argument("headless")
使浏览器无法在屏幕上打开时,模型返回以下错误:
Message: element click intercepted
为了解决这个问题,我分析了选项,在WebDriverWait
(https://stackoverflow.com/a/62904494/11462274)上找到了这个选项,并尝试这样使用它:
for btn in WebDriverWait(driver, 1).until(EC.element_to_be_clickable((By.XPATH, "//tr[contains(@class,'group-head clickable')]"))):
btn.click()
完整代码:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
options.add_argument("headless")
options.add_experimental_option("excludeSwitches", ["enable-logging"])
driver = webdriver.Chrome(r"C:\Users\Computador\Desktop\Python\chromedriver.exe", options=options)
url = "https://int.soccerway.com/matches/2021/07/28/"
driver.get(url)
driver.find_element_by_xpath("//div[@class='language-picker-trigger']").click()
driver.find_element_by_xpath("//a[@href='https://int.soccerway.com']").click()
time.sleep(10)
for btn in WebDriverWait(driver, 1).until(EC.element_to_be_clickable((By.XPATH, "//tr[contains(@class,'group-head clickable')]"))):
btn.click()
time.sleep(10)
jogos = driver.find_elements_by_xpath("//td[contains(@class,'score-time')]//a")
for jogo in jogos:
resultado = jogo.get_attribute("href")
print(resultado)
driver.quit()
但因为它不可iterable,所以返回时出错:
'NoneType' object is not iterable
我为什么需要此选项?
1-我将在一个在线终端上实现自动化,这样屏幕上就不会有任何浏览器可以打开,我需要加快速度,这样我就不会在终端上花费太多的时间限制
2-我需要找到一个选项,可以使用任何日期,而不是2021/07/28
在:
url = "https://int.soccerway.com/matches/2021/07/28/"
其中,将来我将添加参数:
today = date.today().strftime("%Y/%m/%d")
在这个答案(https://stackoverflow.com/a/68535595/11462274)中,一个家伙指出了一个非常快速和有趣的选项(他在答案末尾将该选项命名为:更快的版本),而不需要WebDriver
,但当我尝试使用一年中的其他日期时,我只能在网站的第一页上使用它,他一直只返回当前游戏的链接
预期结果(共有465个链接,但由于存在字符限制,所以我没有给出整个结果):
https://int.soccerway.com/matches/2021/07/28/europe/uefa-champions-league/fc-sheriff-tiraspol/alashkert-fc/3517568/
https://int.soccerway.com/matches/2021/07/28/europe/uefa-champions-league/fk-neftchi/olympiakos-cfp/3517569/
https://int.soccerway.com/matches/2021/07/28/europe/uefa-champions-league/scs-cfr-1907-cluj-sa/newcastle-fc/3517571/
https://int.soccerway.com/matches/2021/07/28/europe/uefa-champions-league/fc-midtjylland/celtic-fc/3517576/
https://int.soccerway.com/matches/2021/07/28/europe/uefa-champions-league/fk-razgrad-2000/mura/3517574/
https://int.soccerway.com/matches/2021/07/28/europe/uefa-champions-league/galatasaray-sk/psv-nv/3517577/
https://int.soccerway.com/matches/2021/07/28/europe/uefa-champions-league/bsc-young-boys-bern/k-slovan-bratislava/3517566/
https://int.soccerway.com/matches/2021/07/28/europe/uefa-champions-league/fk-crvena-zvezda-beograd/fc-kairat-almaty/3517570/
https://int.soccerway.com/matches/2021/07/28/europe/uefa-champions-league/ac-sparta-praha/sk-rapid-wien/3517575/
https://int.soccerway.com/matches/2021/07/28/world/olympics/saudi-arabia-u23/brazil--under-23/3497390/
https://int.soccerway.com/matches/2021/07/28/world/olympics/germany-u23/cote-divoire-u23/3497391/
https://int.soccerway.com/matches/2021/07/28/world/olympics/romania-u23/new-zealand-under-23/3497361/
https://int.soccerway.com/matches/2021/07/28/world/olympics/korea-republic-u23/honduras-u23/3497362/
https://int.soccerway.com/matches/2021/07/28/world/olympics/australia-under-23/egypt-under-23/3497383/
https://int.soccerway.com/matches/2021/07/28/world/olympics/spain-under-23/argentina-under-23/3497384/
https://int.soccerway.com/matches/2021/07/28/world/olympics/france-u23/japan-u23/3497331/
https://int.soccerway.com/matches/2021/07/28/world/olympics/south-africa-u23/mexico-u23/3497332/
https://int.soccerway.com/matches/2021/07/28/africa/cecafa-senior-challenge-cup/uganda-under-23/eritrea-under-23/3567664/
注1:有多种类型的score-time
,例如score-time status
和score-time score
,这就是我在"//td[contains(@class,'score-time')]//a"
中使用contains
的原因
如果可能的话,除了帮助我解决当前的问题外,我还对当前使用的方法的改进和更快的选项感兴趣。(我还在学习,所以我的方法很陈旧)
尝试添加}{}
Options
{O/p
布朗德比如果
我明白了,你的剧本有两个问题
首先是
基本上,这是错误的,因为
element_to_be_clickable
将再次返回单个webelement,因此您将得到non-inerrable error
,而我们可以使用visibility_of_all_elements_located
返回列表其次,您不能直接
click
,因为Selenium
视图端口中没有几个元素,所以我们必须使用ActionsChain
见下文:
输出:
你不需要硒
硒永远不应该是从web上抓取数据的主要方式。它的速度很慢,通常比它的备选方案需要更多的代码行。尽可能使用
requests
与lxml
解析器结合使用。在这个特定的用例中,您只使用selenium
在不同的URL之间切换,这是一种可以很容易地硬编码的东西,从而避免了首先使用它的需要那么什么时候硒值得使用呢
如今,网站通常提供动态内容,因此如果您想要检索的数据不是静态加载的:
requests
来模拟它李>如果由于网页的设计方式,第1点和第2点是不可能的,那么最好的选择是使用
selenium
,它将通过模拟用户交互获取所需的内容。对于HTML解析,您仍然可以选择使用lxml
,或者您可以坚持使用selenium
,它也提供了该功能第一次编辑:
第二次编辑:
get_page_params()
和request_other_pages()
浏览每个列出的竞赛的所有页面的功能相关问题 更多 >
编程相关推荐