亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Selenium 循環瀏覽網頁的 <a> 標簽

使用 Selenium 循環瀏覽網頁的 <a> 標簽

翻閱古今 2024-01-16 15:42:48
我正在使用 Selenium 使用 Python 構建一個網絡爬蟲。這是我正在從中抓取數據的網頁:https://fnet.bmfbovespa.com.br/fnet/publico/abrirGerenciadorDocumentosCVM?cnpjFundo=11026627000138&idCategoriaDocumento=6&idTipoDocumento=45正如您所看到的,當我們單擊標有數字的按鈕時,該頁面內的表格會發生變化。該頁面是巴西基金報告?!癆nterior” 的意思是“前一個”,“seguinte” 的意思是“下一個”。我想遍歷所有這些數字,但我嘗試過的方法都不起作用。我正在嘗試使用 Selenium 的方法單擊標識<a>按鈕的標簽。Seguinteclick()我嘗試過的browser = Firefox()button = browser.find_element_by_id('tblDocumentosEnviados_next')while button.is_enabled():                    page = browser.find_element_by_xpath('//a[contains(@class, "paginate_button current")]').text    print(page)    button = browser.find_element_by_id('tblDocumentosEnviados_next')    time.sleep(3)    button.click()我如何迭代這些數字?
查看完整描述

3 回答

?
躍然一笑

TA貢獻1826條經驗 獲得超6個贊

我分析了網站發出的請求,發現您可以通過一個請求獲取所有數據,即 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代表start,l代表limit,始終保持200以一次抓取最大值要求。


查看完整回答
反對 回復 2024-01-16
?
鴻蒙傳說

TA貢獻1865條經驗 獲得超7個贊

正如你所看到的,兩個按鈕之間的區別只是按鈕的數量


//*[@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)+"]")

希望這能解決您想要做的事情


查看完整回答
反對 回復 2024-01-16
?
哆啦的時光機

TA貢獻1779條經驗 獲得超6個贊

正如你所看到的,兩個按鈕之間的區別只是按鈕的數量


//*[@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)+"]")

希望這能解決您想要做的事情


查看完整回答
反對 回復 2024-01-16
  • 3 回答
  • 0 關注
  • 264 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號