各位,我現在要批量下載上市公司財務報表,碰到問題,以下是源代碼:
# -*- coding: UTF-8 -*-
import requests
from urllib.request import urlopen
url='http://basic.10jqka.com.cn/api/stock/export.php?export=main&type=report&code=000001'
def getFile(url):
file_name = url.split('=')[-1]
response = requests.get(url).content
with open(file_name+'.xls', 'wb') as f:
f.write(response)
print ("Sucessful to download" + " " + file_name)
getFile(url)
執行以上代碼,只能生成一個0kb的檔案,如果把url直接拷貝到瀏覽器地址欄,回車后,會彈出一個保存視窗,點擊確定后,能夠保存到完整的檔案。
試過wget一樣沒效果。
也試過urllib.request.urlretrieve方法
import urllib.request
url='http://basic.10jqka.com.cn/api/stock/export.php?export=main&type=report&code=000001'
urllib.request.urlretrieve(url, '000001'+'.xls')
也一樣達不到效果。
請問,問題出在哪里?
uj5u.com熱心網友回復:
網站有反爬機制,請發送模擬請求頭uj5u.com熱心網友回復:
確實需要模擬頭,謝謝轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/110836.html
上一篇:計算機網路技術學習的思維導圖
下一篇:一文解讀VR/AR/MR (轉)
