我一直在尝试从给定的链接下载csv和zip文件:
** https://nseindia.com/content/fo/fo.zip ** https://nseindia.com/archives/nsccl/sett/FOSett_prce_17052019.csv
以下代码给出错误,如HTTP错误403:禁止
import urllib.request
csv_url = 'https://nseindia.com/archives/nsccl/sett/FOSett_prce_17052019.csv'
urllib.request.urlretrieve(csv_url, '17_05.csv')
答案 0 :(得分:1)
您的问题是因为网站服务器阻止了Python-urllib的默认User-Agent(Python-urllib/3.7
)。但是,您可以通过更改User-Agent标头来绕过阻塞:
import urllib.request
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
urllib.request.install_opener(opener)
csv_url = 'https://nseindia.com/archives/nsccl/sett/FOSett_prce_17052019.csv'
urllib.request.urlretrieve(csv_url, '17_05.csv')
答案 1 :(得分:0)
在这里您可以获取CSV文件的内容,并且可以编写CSV文件。
import csv
import requests
CSV_URL = 'https://nseindia.com/archives/nsccl/sett/FOSett_prce_17052019.csv'
with requests.Session() as s:
download = s.get(CSV_URL)
decoded_content = download.content.decode('utf-8')
cr = csv.reader(decoded_content.splitlines(), delimiter=',')
my_list = list(cr)
for row in my_list:
print(row)
答案 2 :(得分:0)
安装软件包requests
。
pip install requests
然后,使用requests.get
api下载文件,然后将其写入所需的文件。
import requests
csv_url = 'https://nseindia.com/archives/nsccl/sett/FOSett_prce_17052019.csv'
r = requests.get(csv_url, allow_redirects=True)
open('test.csv', 'wb').write(r.content)