我一直在嘗試為它的 excel 檔案抓取一個網站。我計劃對其資料存檔部分中包含的大量資料執行一次此操作。我已經能夠使用 urllib 請求一次下載一個檔案,并手動在幾個不同的檔案上進行嘗試。但是當我嘗試創建一個函式來下載所有這些時,我收到了一些錯誤。發生的第一個錯誤只是將 http 檔案地址作為串列獲取。我將驗證更改為 false(出于安全原因,這不是最佳做法)以解決它給我的認證 ssl 錯誤并且它有效。然后我再次嘗試通過報廢并將其下載到特定檔案夾來進一步。我以前用一個類似的專案做過這個,并且幾乎沒有遇到認證錯誤 ssl 的困難。
import requests
from bs4 import BeautifulSoup
import os
os.chdir(r'C:\ The out put path were it will go\\')
url = 'https://pages.stern.nyu.edu/~adamodar/pc/archives/'
reqs = requests.get(url, verify=False)
soup = BeautifulSoup(reqs.text, 'html.parser')
file_type = '.xls'
urls = []
for link in soup.find_all('a'):
file_link = link.get('href')
if file_type in file_link:
print(file_link)
with open(link.text, 'wb') as file:
response = requests.get(url file_link)
file.write(response.content)
這是即使在驗證錯誤之后也一直給我的錯誤,這似乎在生成串列之前解決了問題。每次嘗試都會抓取第一個檔案,但不會回圈到下一個檔案。
requests.exceptions.SSLError: HTTPSConnectionPool(host='pages.stern.nyu.edu', port=443): Max retries exceeded with url: /~adamodar/pc/archives/BAMLEMPBPUBSICRPIEY19.xls (由 SSLError(SSLCertVerificationError(1 , '[SSL: CERTIFICATE_VERIFY_FAILED] 證書驗證失敗:無法獲取本地頒發者證書 (_ssl.c:1129)')
我錯過了什么?我以為我解決了驗證問題。
uj5u.com熱心網友回復:
verify=False您在獲取檔案時忘記設定
urls = []
for link in soup.find_all('a'):
file_link = link.get('href')
if file_type in file_link:
print(file_link)
with open(link.text, 'wb') as file:
response = requests.get(url file_link, verify=False) # <-- This is where you forgot
file.write(response.content)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/496225.html
下一篇:從多個URL中提取p個標簽
