Urllib2 <urlopen error =“”[errno =“”-5] =“”no =“”address =“”associated =“”with =“”hostname =“”> </urlopen>

时间:2013-10-06 15:43:26

标签: python api csv urllib2 yahoo

我有一个使用多个API的python程序,其中两个来自Yahoo!。然而,我遇到了麻烦。它总是给我错误<urlopen error [Errno -5] No address associated with hostname>以下是各种终端命令的输出,试图从“http://download.finance.yahoo.com/d/quotes.csv?s=GOOG&f=nsl1l2op&e=.csv”获取数据:

我的代码:

def stocks():
    lcd.clear()
    lcd.message('RasPi Stocks\nUsing Yahoo!')
    time.sleep(2)
    val = 1
    for i in stockslst:
#           host = urllib2.socket.gethostbyname('finance.yahoo.com')
            req = urllib2.Request('http://download.finance.yahoo.com/d/quotes.csv?s=' + i + '&f=nsl1l2op&e=.csv$
            req.add_header('Host:', 'finance.yahoo.com')
            mdata = urllib2.urlopen(req)
            csvdata = [row for row in csv.reader(mdata)]
            sname = csvdata[0][0]
            last1 = float(csvdata[0][2])
            last2 = float(csvdata[0][3])
            if last1 > last2:
                    r = 0
            if last1 < last2:
                    r = 1
            if last1 == last2:
                    r = 126
            lcd.clear()
            lcd.message(sname + '\n' + chr(r) + str(last1))
            time.sleep(2)

我的节目:

pi@raspberrypi ~ $ sudo python lcddisplayrpi.py
Traceback (most recent call last):
File "lcddisplayrpi.py", line 132, in <module>
stocks()
File "lcddisplayrpi.py", line 113, in stocks
mdata = urllib2.urlopen(req)
File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 401, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 419, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 379, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1211, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1181, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno -5] No address associated with hostname>

卷曲有问题的地址:

curl "http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=nsl1l2op&e=.csv"
<HEAD><TITLE>Redirect</TITLE></HEAD>
<BODY BGCOLOR="white" FGCOLOR="black">
<FONT FACE="Helvetica,Arial"><B>
 "<em>http://download.finance.yahoo.com/d/quotes.csv?s=GOOG&f=nsl1l2op&e=.csv</em>".<p></B></FONT>

<!-- default "Redirect" response (301) -->
</BODY>

找到有问题的地址:

 wget "http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=nsl1l2op&e=.csv"
--2013-10-06 09:01:30--  http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=nsl1l2op&e=.csv
Resolving finance.yahoo.com (finance.yahoo.com)... failed: No address associated with hostname.
wget: unable to resolve host address `finance.yahoo.com'

我可以在同一台计算机上的Web浏览器中获取csv数据。正如在curl命令中暗示的那样,我尝试将其更改为download.finance.yahoo.com,但是没有运气的python程序。我再次尝试卷曲:

curl "http://download.finance.yahoo.com/d/quotes.csv?s=GOOG&f=nsl1l2op&e=.csv"
"Google Inc.","GOOG",872.35,-,876.00,876.09

这是我想要的数据。我不知道我的python程序有什么问题。

0 个答案:

没有答案