Python帮助 - 解析xml

时间:2013-11-17 07:11:28

标签: python xml

嘿我正在尝试使用python解析yahoo weather xml,这是代码:

#!/usr/bin/env python

import subprocess

import urllib

from xml.dom import minidom


WEATHER_URL = 'http://weather.yahooapis.com/forecastrss?w=55872649&u=c'

WEATHER_NS = 'http://xml.weather.yahoo.com/ns/rss/1.0'

dom = minidom.parse(urllib.urlopen(WEATHER_URL))

ycondition = dom.getElementsByTagNameNS(WEATHER_NS, 'condition')[0]

CURRENT_OUTDOOR_TEMP = ycondition.getAttribute('temp')

print(CURRENT_OUTDOOR_TEMP)

为什么我在 IIS7上出现此错误?

Traceback (most recent call last): File "C:\inetpub\wwwroot\22.py", line 16, in dom = minidom.parse(urllib.urlopen(WEATHER_URL))
File "C:\Python27\lib\urllib.py", line 86, in urlopen return opener.open(url)
File "C:\Python27\lib\urllib.py", line 207, in open return getattr(self, name)(url)
File "C:\Python27\lib\urllib.py", line 344, in open_http h.endheaders(data)
File "C:\Python27\lib\httplib.py", line 954, in endheaders self._send_output(message_body)
File "C:\Python27\lib\httplib.py", line 814, in _send_output self.send(msg)
File "C:\Python27\lib\httplib.py", line 776, in send self.connect()
File "C:\Python27\lib\httplib.py", line 757, in connect self.timeout, self.source_address)
File "C:\Python27\lib\socket.py", line 571, in create_connection
raise err IOError: [Errno socket error] [Errno 10061] No connection could be made because the target machine actively refused it

请帮忙...... 感谢

3 个答案:

答案 0 :(得分:0)

看起来您的防火墙阻止了对weather.yahooapis.com的访问。

  1. 检查防火墙日志
  2. 允许访问域weather.yahooapis.com

答案 1 :(得分:0)

可能有以下几个原因:

  • 你落后于代理,所以你应该提供首先连接到代理然后urlopen()
  • 的代码
  • 您的PC或网关上的某些防火墙禁止连接到该网站。
  • 您的防病毒软件对您的程序产生怀疑。罕见但可能。
  • 网站检测到你是机器人,而不是浏览器。所以关闭连接。例如来自用户代理等
  • 确保服务器不仅仅是ssl。

希望它可以帮助你解决问题。

答案 2 :(得分:-1)

import json
import codecs
import urllib , cStringIO
import string
from Tkinter import *

weather_api =urllib.urlopen('https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22nagercoil%2C%20IND%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys')
weather_json = json.load(weather_api)
data_location = weather_json["query"]['results']['channel']['location']
place = data_location['city']

data_item = weather_json["query"]['results']['channel']['item']
fore_cast = data_item['forecast'][0]['text']
temp = data_item['condition']['temp']
date = data_item['condition']['date']
data_image= weather_json["query"]['results']['channel']['image']
root = Tk()
f = Frame(width=800, height=546, bg='green', colormap='new')


w0 = Label(root, fg='#FF6699', text=fore_cast, font=("Helvetica",15))
w = Label(root, fg='blue', text=temp + u'\N{DEGREE SIGN}'+'F', font=("Helvetica",56))
w1 = Label(root, fg='#3399CC', text=place, font=("Helvetica",15))
w0.pack()
w.pack()
w1.pack()

root.mainloop()

使用python2.7的简单天气预报应用程序。   从此网址https://developer.yahoo.com/weather/中选择api,然后只需解码json对象。 json对象包含大量数据。然后使用Tkinter final output

轻松显示数据
相关问题