TypeError:Python中应为类似字符串或字节的对象
我现在正在休息。这是什么TypeError:Python中应为类似字符串或字节的对象,python,Python,我现在正在休息。这是什么类型错误,如何解决?规范中需要哪些必要的修改 from urllib.request import urlretrieve stoxxeu600_url = urllib.request.urlretrieve('https://www.stoxx.com/document/Indices/Current/HistoricalData/hbrbcpe.txt') vstoxx_url = urllib.request.urlretrieve('https://www.s
类型错误
,如何解决?规范中需要哪些必要的修改
from urllib.request import urlretrieve
stoxxeu600_url = urllib.request.urlretrieve('https://www.stoxx.com/document/Indices/Current/HistoricalData/hbrbcpe.txt')
vstoxx_url = urllib.request.urlretrieve('https://www.stoxx.com/document/Indices/Current/HistoricalData/h_vstoxx.txt')
data_folder = 'data/' #Save file to local target destination.
stoxxeu600_filepath = data_folder + "stoxxeu600.txt"
vstoxx_filepath = data_folder + "vstoxx.txt"
urlretrieve(stoxxeu600_url,stoxxeu600_filepath)
这是输出:
File "/home/aryabhatta/anaconda3/lib/python3.6/urllib/parse.py", line 938, in splittype
match = _typeprog.match(url)
TypeError: expected string or bytes-like object
urlretrieve
需要一个字符串作为其第一个参数。所以stoxxeu600\u url
应该是一个字符串
from urllib.request import urlretrieve
stoxxeu600_url = 'https://www.stoxx.com/document/Indices/Current/HistoricalData/hbrbcpe.txt'
data_folder = 'data/' #Save file to local target destination.
stoxxeu600_filepath = data_folder + "stoxxeu600.txt"
urlretrieve(stoxxeu600_url, stoxxeu600_filepath)
从文档中可以看到,该方法返回一个元组
(文件名,标题)
在代码中,首先调用urlretrieve()
并将其存储到stoxxeu600\uURL
stoxxeu600_url = urllib.request.urlretrieve('https://www.stoxx.com/document/Indices/Current/HistoricalData/hbrbcpe.txt')
stoxxeu600\u url
现在有(文件名、标题)
由urlretrieve()返回
然后使用stoxxeu600\u url
再次调用urlretrieve()
,这是一个元组,而不是方法所期望的str/byte对象。因此,导致类型错误
urlretrieve(stoxxeu600_url,stoxxeu600_filepath)
要修复它,只需将stoxxeu600\u url
设置为url,然后调用该方法
from urllib.request import urlretrieve
stoxxeu600_url = 'https://www.stoxx.com/document/Indices/Current/HistoricalData/hbrbcpe.txt'
stoxxeu600_filepath = "stoxxeu600.txt"
urlretrieve(stoxxeu600_url, filename=stoxxeu600_filepath)
请包含完整的错误消息。谢谢您的建议。@HimanshuDoneria mark as已回答,如果这正是您所寻找的