如何在urllib中向URL参数添加变量

时间:2013-06-29 21:50:50

标签: python python-3.x urllib

我正在尝试访问此网址:

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>

我没有正确地进行字符串格式化吗?

2 个答案:

答案 0 :(得分:2)

您从文件名中读取的数据包括每行末尾的换行符(.readlines()不会删除它)。你应该自己删除它; str.strip()删除所有空格,包括换行符:

filename = input("Please enter file name to extract data from: ")
with open(filename) as f:
    tickers_list = f.readlines()    # .readlines() returns a list *already*

print(tickers_list[0].strip())
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" % tickers_list[0].strip()
print(url)

response = urllib.request.urlopen(url)
html = response.read()
print(html)

您无需在str()元素上调用tickers_list[0],因为从文件中读取已经产生了字符串列表。此外,%s格式化占位符将其值转换为字符串(如果它还不是字符串)。

使用下面的\n输出中的换行符(repr()字符),您会看到确切的错误:

>>> 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" % 'GOOG\n'
>>> print(repr(url))
'http://ichart.finance.yahoo.com/table.csv?s=GOOG\n&a=00&b=1&c=2011&d=05&e=28&f=2013&g=d&ignore=.csv'
>>> urllib.request.urlopen(url)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mj/Development/Libraries/buildout.python/parts/opt/lib/python3.3/urllib/request.py", line 156, in urlopen
    return opener.open(url, data, timeout)
  File "/Users/mj/Development/Libraries/buildout.python/parts/opt/lib/python3.3/urllib/request.py", line 467, in open
    req = meth(req)
  File "/Users/mj/Development/Libraries/buildout.python/parts/opt/lib/python3.3/urllib/request.py", line 1172, in do_request_
    raise URLError('no host given')
urllib.error.URLError: <urlopen error no host given>

如果您要从文件输入中仅处理一个行,请使用f.readline()读取该行并保存自己必须索引列表。你仍然需要剥离换行符。

如果要处理所有行,只需直接在输入文件上循环,分别生成每行,再次 换行:

with open(filename) as f:
    for ticker_name in f:
        ticker_name = ticker_name.strip()
        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" % ticker_name

        # etc.

答案 1 :(得分:2)

为了在python中操作url,我建议使用两个解决方案:furlURLObject。这两个库为您提供了非常好的界面来轻松操作URL。

来自furl文档的示例:

>>> from furl import furl
>>> f = furl('http://www.google.com/?one=1&two=2')
>>> f.args['three'] = '3'
>>> del f.args['one']
>>> f.url
'http://www.google.com/?two=2&three=3'