[英]Read csv file directly from a website in Python 3
我試圖直接從網站(從可下載的鏈接)讀取CSV文件,然后將其列之一作為列表獲取,以便我可以進一步使用它。 我無法正確編碼。 我到達的最近的是
import csv
import urllib.request as urllib
import urllib.request as urlRequest
import urllib.parse as urlParse
url = "https://www.nseindia.com/content/indices/ind_nifty50list.csv"
# pretend to be a chrome 47 browser on a windows 10 machine
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"}
req = urlRequest.Request(url, headers = headers)
# open the url
x = urlRequest.urlopen(req)
sourceCode = x.read()
您非常接近目標。
只需按行分割讀取的CSV數據,然后將其傳遞給csv.reader():
import csv
import urllib.request as urllib
import urllib.request as urlRequest
import urllib.parse as urlParse
url = "https://www.nseindia.com/content/indices/ind_nifty50list.csv"
# pretend to be a chrome 47 browser on a windows 10 machine
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"}
req = urlRequest.Request(url, headers = headers)
# open the url
x = urlRequest.urlopen(req)
sourceCode = x.read()
cr = csv.DictReader(sourceCode.splitlines())
l = [row['Series'] for row in cr]
但是請注意, x.read()
返回bytearray
,因此,如果csv包含非ASCII符號,請不要忘記添加:
x.read().decode('utf-8') # or another encoding you need
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.