我正在尝试访问此 URL:
http://ichart.finance.yahoo.com/table.csv?s=GOOG&a=05&b=20&c=2013&d=05&e=28&f=2013&g=d&ignore=.csv
但不是总是 GOOG,而是变量 ticker_list 中输入的任何内容,如下所示:
当我这样做时,它会起作用:
URL = urllib.request.urlopen("http://ichart.finance.yahoo.com/table.csv?s=GOOG&a=05&b=20&c=2013&d=05&e=28&f=2013&g=d&ignore=.csv")
html = URL.read()
print (html)
但如果我这样做:
filename = input("Please enter file name to extract data from: ")
with open(filename) as f:
data = f.readlines() # Read the data from the file
tickers_list = []
for line in data:
tickers_list.append(line) # Separate tickers into individual elements in list
print (tickers_list[0]) # Check if printing correct ticker
url = "http://ichart.finance.yahoo.com/table.csv?s=%s&a=00&b=1&c=2011&d=05&e=28&f=2013&g=d&ignore=.csv" % str(tickers_list[0])
print (url) # Check if printing correct URL
URL = urllib.request.urlopen(url)
html = URL.read()
print (html)
并给我这个错误:
urllib.error.URLError: <urlopen error no host given>
我没有正确地进行字符串格式化吗?