我正在嘗試使用 ChromeDriver Selenium for Python 下載檔案。 這是檔案所在的頁面。
要訪問該檔案,我必須單擊最新日期(例如今天是 22/06/2022),然后單擊鏈接“Baixar Arquivo”。
我正在嘗試將 Selenium 與 ChromeWebDriver 一起使用。但是,到目前為止我嘗試的所有方法都有例外。問題是頁面有很多嵌套元素,并且 div 具有相同的名稱。我不知道如何解決這個問題。到目前為止我已經嘗試過:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
import traceback
service = ChromeService(executable_path=ChromeDriverManager().install())
op = webdriver.ChromeOptions()
op.add_argument('headless')
try:
driver = webdriver.Chrome(service=service, options=op)
driver.get('https://www.b3.com.br/pt_br/market-data-e-indices/servicos-de-dados/market-data/consultas/boletim-diario/dados-publicos-de-produtos-listados-e-de-balcao/')
container = driver.find_element(by=By.XPATH, value='/html/body/div/div/div/div/div[2]/div[1]/div/div[1]/a/div/div/div') # didn't work
container = driver.find_element(by=By.CSS_SELECTOR, value='html body div#wrapper div div div.container div.row div.col-lg-8.list div.accordion div.card div#collapse.collapse.show div.card-block.col-12 div.list-avatar.two-line div.list-avatar-row.tamanho div.content p.fonte.secondary-text a') #didn't work either
container = driver.find_element(by=By.LINK_TEXT , value='Baixar Arquivo') # didn't work either
print(container)
except Exception:
print(traceback.format_exc())
finally:
driver.close()
每次我得到同樣的錯誤(沒有這樣的元素):
Traceback (most recent call last):
File "/tmp/ipykernel_182073/1979137013.py", line 8, in <cell line: 5>
container = driver.find_element(by=By.LINK_TEXT , value='Baixar Arquivo')
File "/home/guilherme/miniconda3/envs/investment/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 1251, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "/home/guilherme/miniconda3/envs/investment/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 430, in execute
self.error_handler.check_response(response)
File "/home/guilherme/miniconda3/envs/investment/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Baixar Arquivo"}
(Session info: headless chrome=103.0.5060.53)
Stacktrace:
#0 0x561b67a2db13 <unknown>
#1 0x561b67834688 <unknown>
#2 0x561b6786bcc7 <unknown>
#3 0x561b6786be91 <unknown>
#4 0x561b6789ee34 <unknown>
#5 0x561b678898dd <unknown>
#6 0x561b6789cb94 <unknown>
#7 0x561b678897a3 <unknown>
#8 0x561b6785f0ea <unknown>
#9 0x561b67860225 <unknown>
#10 0x561b67a752dd <unknown>
#11 0x561b67a792c7 <unknown>
#12 0x561b67a5f22e <unknown>
#13 0x561b67a7a0a8 <unknown>
#14 0x561b67a53bc0 <unknown>
#15 0x561b67a966c8 <unknown>
#16 0x561b67a96848 <unknown>
#17 0x561b67ab0c0d <unknown>
#18 0x7f9208c4c609 <unknown>
我的想法不多了。PS:我在不同的站點嘗試了這些策略,并且奏效了。我假設問題出在嵌套元素中。我該如何解決這個問題?
uj5u.com熱心網友回復:
通過查看請求,您可以使用Python 的 requests。分析它,您有 3 個請求。第一個將回傳一個包含所有檔案的串列(第一個是最新的)。對特定檔案的令牌請求,您可以使用該令牌請求該檔案。
import requests
import json
# this URL return a list with all files
page_url = "https://arquivos.b3.com.br/api/channels/34dcaaeb-0306-4f45-a83e-4f66a23b42fa/subchannels/cc188e40-03be-408e-aa86-501926b97a76/publications?&lang=pt"
# We get the list in json format
page_request = requests.get(page_url)
page_response = json.loads(page_request.content)
# You could loop through page_response to access all files,
# this is for the latest one
latest_file = page_response[0]
# We extract the file name and the date to use in the next request
file_name = latest_file["fileName"].split("File")[0]
date = latest_file["dateTime"].split("T")[0]
# We add the extracted info in this new url to get the token
token_url = f"https://arquivos.b3.com.br/api/download/requestname?fileName={file_name}&date={date}&recaptchaToken="
token_request = requests.get(token_url)
token_response = json.loads(token_request.content)
# We extract the token value from the response
token = token_response["redirectUrl"].split("?token=")[1]
# We call this URL to get the file we want
file_url = f"https://arquivos.b3.com.br/api/download/?token={token}"
file_request = requests.get(file_url)
file_response = file_request.content
# This response is the direct CSV content, so we can just save it directly
csv_file = open(latest_file["fileName"], "wb")
csv_file.write(file_response)
csv_file.close()
您可以在 OOP 中添加 try 和 except 甚至 make。使用該串列檔案,您還可以映射它并根據日期和其他選擇檔案。
希望這可以幫助 :)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/495377.html
