从Python 3.6中的URL下载数据

时间:2018-08-09 05:18:11

标签: python python-3.x dataframe python-requests

我想将数据从https://download.bls.gov/pub/time.series/ln/ln.data.1.AllData下载到数据框。

我尝试了以下脚本,但未能成功。

import requests, io
import pandas as pd

URL = 'https://download.bls.gov/pub/time.series/ln/ln.data.1.AllData'

#1
urlData = requests.get(URL).content
rawData = pd.read_csv(io.StringIO(urlData.decode('utf-8')))
print(len(rawData))

Error: Python IDLE Got Stuck


#2
r = requests.get(URL)  
urlData = pd.read_csv(io.StringIO(r))
print(len(urlData))

Error:-
urlData = pd.read_csv(io.StringIO(r))
TypeError: initial_value must be str or None, not Response

#3
urlData = pd.read_csv(URL, header=None)
print(len(urlData))

3 个答案:

答案 0 :(得分:3)

我与之合作

import requests, io
import pandas as pd

URL = 'https://download.bls.gov/pub/time.series/ln/ln.data.1.AllData'

#1
urlData = requests.get(URL).content
rawData = pd.read_csv(io.StringIO(urlData.decode('utf-8')), sep="\t")
print(rawData.head())
print(rawData.info())

答案 1 :(得分:2)

最简单的方法是使用urllib2。

import urllib2
url_name = 'http://abc.pdf'
response = urllib2.urlopen(url_name)
file = open(url_name.split('//')[1], 'w')
file.write(response.read())
file.close()

答案 2 :(得分:0)

我试图通过URL下载数据,但这确实需要很长时间。我建议您通过func areaOfRectangle(length: Int, width: Int) { print(“length * width”) } areaOfRectangle(length: 0, width: 0) 下载并进行处理。脚本本身看起来不错。