Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有一种好方法可以循环浏览由javascript pageNav值定义的数百个页面?_Python_Selenium_Web Scraping - Fatal编程技术网

Python 有没有一种好方法可以循环浏览由javascript pageNav值定义的数百个页面?

Python 有没有一种好方法可以循环浏览由javascript pageNav值定义的数百个页面?,python,selenium,web-scraping,Python,Selenium,Web Scraping,我正在做一个网页抓取项目,我正在尝试爬过数百页的搜索结果,并将每页上显示的100个链接的ID存储到一个列表中。我已经存储了第一页的链接ID,但我正在寻找一种高效的方法来移动到下一页,存储ID,移动到下一页,存储ID,等等。我已经附上了我当前的代码来存储第一页上的ID,以及一个用于搜索结果页面导航的网站代码截图。我尝试过使用“单击”按钮,但没有效果,我似乎不知道如何使用pageNav值来创建一个好的循环。请让我知道,如果你看到一个很好的方法来解决这个问题!我对python非常陌生,很可能会错过一个

我正在做一个网页抓取项目,我正在尝试爬过数百页的搜索结果,并将每页上显示的100个链接的ID存储到一个列表中。我已经存储了第一页的链接ID,但我正在寻找一种高效的方法来移动到下一页,存储ID,移动到下一页,存储ID,等等。我已经附上了我当前的代码来存储第一页上的ID,以及一个用于搜索结果页面导航的网站代码截图。我尝试过使用“单击”按钮,但没有效果,我似乎不知道如何使用pageNav值来创建一个好的循环。请让我知道,如果你看到一个很好的方法来解决这个问题!我对python非常陌生,很可能会错过一个简单的解决方案

从selenium.webdriver.common.desired_功能导入DesiredCapabilities 从selenium.webdriver.common.action\u链导入ActionChains 从selenium.webdriver.common.by导入 从selenium.webdriver.support.ui导入WebDriverWait 从selenium.webdriver.support将预期的_条件导入为EC 从selenium.webdriver.support.ui导入选择 从selenium.webdriver.common.keys导入密钥 从selenium.webdriver.chrome.options导入选项 从selenium导入webdriver 从时间上导入睡眠 作为pd进口熊猫 进口表格 导入时间 导入操作系统 导入模块名称 路径=/Users/williamsheehan/Documents/ChromeDriver/ChromeDriver driver=webdriver.ChromePATH driver.implicitly_wait15 action=ActionChainsdriver 去equibase 驾驶员gethttps://www.equibase.com/stats/View.cfm?tf=year&tb=horse 马匹以福林年为单位 horses\u by\u foaling\u year=WebDriverWaitdriver,120.untelec.presence\u\u元素的位置由.PARTIAL\u LINK\u TEXT确定,foaling year 按年份排列的马。单击 福林年开放菜单 年份菜单=选择Driver.find\u element\u by\u idfoalYearList 1998 年份菜单。按值选择年份1998 创建马ID列表 ids_1998=[] 遍历第1页上的马,并将ID添加到列表中 first\u horses=driver.find\u elements\u by\u class\u namehouse 对于第一批马中的马: ids_1998.appendhorse.get_attributeid
printids_1998你做的工作比你必须做的多得多。这不需要硒。如果记录浏览器的网络流量,可以看到,一旦页面开始加载,浏览器就会通过HTTP GET向REST API发出一个XHR请求,响应为JSON,并包含您可能需要的所有信息。不确定您的horse ID实际上是什么样子,但我几乎可以肯定它们是JSON响应的一部分。以下是JSON响应的一部分:

def主: 导入请求 url=https://www.equibase.com/Data.cfm/Stats/Horse/Year/Page 参数={ 年份:2021年, 页码:1, 分类:收入, 导演:A, 列表:N, 类别:A,, 属性_总计:1024, 全套, 种族(品种)类型:结核病,, _: 1612374713790 } response=requests.geturl,params=params response.raise_以获取_状态 horses=response.json[stats] 对于马匹中的马: printft这匹马的名字叫\{horse['horseName']}\它的参考号是{horse['referenceNumber']} 返回0 如果uuuu name uuuuu==\uuuuuuuu main\uuuuuuuu: 导入系统 sys.exitmain 输出:

This horse is named "Knicks Go" and its reference number is 10049303
This horse is named "Jesus' Team" and its reference number is 10280418
This horse is named "Colonel Liam" and its reference number is 10280403
This horse is named "Independence Hall" and its reference number is 10292074
This horse is named "Largent" and its reference number is 10095939
This horse is named "Letruska" and its reference number is 10104837
This horse is named "Sleepy Eyes Todd" and its reference number is 10101749
This horse is named "Midnight Bourbon" and its reference number is 10400317
This horse is named "Wildman Jack" and its reference number is 10097023
This horse is named "Pacific Gale" and its reference number is 9900200
...

您可以在params查询字符串参数字典中使用page key-value对,以获得不同页面上马的结果。

您可以通过为这些Web元素创建xpath来确定它们的目标。类似于//a的东西[contains@onclick,“+storedID+”]