Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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 循环通过<;a>;使用selenium标记网页_Python_Selenium_Loops - Fatal编程技术网

Python 循环通过<;a>;使用selenium标记网页

Python 循环通过<;a>;使用selenium标记网页,python,selenium,loops,Python,Selenium,Loops,我正在使用selenium用python构建一个webcrawler。这是im从以下位置爬网数据的网页: 正如您所看到的,当我们单击用数字标识的按钮时,此页面内的表格会发生变化。这是一份巴西基金报告“前”表示“上一个”,而“seguinte”表示下一个。我想遍历所有这些数字,但我尝试过的都不起作用。我试图用click()方法从selenium点击标识Seguinte按钮的标签 我所尝试的 任何关于如何遍历这些数字的提示或建议都是非常受欢迎的。使用按钮单击“下一步”按钮后。单击()DOM将刷

我正在使用
selenium
用python构建一个webcrawler。这是im从以下位置爬网数据的网页:

正如您所看到的,当我们单击用数字标识的按钮时,此页面内的表格会发生变化。这是一份巴西基金报告“前”表示“上一个”,而“seguinte”表示下一个。我想遍历所有这些数字,但我尝试过的都不起作用。我试图用
click()
方法从
selenium
点击标识
Seguinte
按钮的
标签

我所尝试的
任何关于如何遍历这些数字的提示或建议都是非常受欢迎的。

使用
按钮单击“下一步”按钮后。单击()
DOM将刷新,而
按钮将过时

您需要在每次迭代中更新
按钮
。请尝试以下代码:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

browser = Firefox()

while True:
    try:
        button = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID, "tblDocumentosEnviados_next")))
        page = browser.find_element_by_xpath('//a[contains(@class, "paginate_button current")]').text
        print(page)
        button.click()
    except TimeoutException:
        break

正如您所看到的,两个按钮之间的区别只是

//*[@id="tblDocumentosEnviados_paginate"]/span/a[1]
//*[@id="tblDocumentosEnviados_paginate"]/span/a[2]
如果您想遍历它,可以简单地更改xpath中的数字

比如说

 for i in range(1, numberOfPages):
browser.find_element_by_xpath("//[@id="tblDocumentosEnviados_paginate"]/span/a["+str(i)+"]")

希望这能解决您的问题

我分析了网站发出的请求,注意到您可以通过一个请求将所有数据作为json内容获取:

import requests as r
from bs4 import BeautifulSoup
import json
url = "https://fnet.bmfbovespa.com.br/fnet/publico/abrirGerenciadorDocumentosCVM?cnpjFundo=11026627000138&idCategoriaDocumento=6&idTipoDocumento=45"

res = r.get("http://fnet.bmfbovespa.com.br/fnet/publico/pesquisarGerenciadorDocumentosDados?d=0&s=0&l=200&"+url.split("?")[1])
json_result = json.loads(res.text)
print(json_result)

注意:如果“recordsTotal”超过200,您需要使用参数“?d=0&s=200&l=200”发出另一个请求,s代表开始,l代表限制,始终保持200以在一个请求中达到最大值。

您确定selenium是webcrawler的正确工具吗?为此目的打开浏览器相当昂贵。@DMart,我不知道,你认为它太贵了吗?请分享你的想法
import requests as r
from bs4 import BeautifulSoup
import json
url = "https://fnet.bmfbovespa.com.br/fnet/publico/abrirGerenciadorDocumentosCVM?cnpjFundo=11026627000138&idCategoriaDocumento=6&idTipoDocumento=45"

res = r.get("http://fnet.bmfbovespa.com.br/fnet/publico/pesquisarGerenciadorDocumentosDados?d=0&s=0&l=200&"+url.split("?")[1])
json_result = json.loads(res.text)
print(json_result)