Urllib2<urlopen error[Errno 5]没有与主机名关联的地址>

2024-10-03 06:27:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python程序,它使用了几个api,包括两个来自Yahoo!的api!。不过,我遇到了麻烦。它总是给我一个错误<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)

我的计划:

^{pr2}$

卷曲有问题的地址:

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有问题的地址:

 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命令中暗示的那样,我尝试将其更改为下载.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程序出了什么问题。在


Tags: csv程序comhttplcdaddressdownloadyahoo