爱学习的扛把子 发表于 2019-1-14 15:33:34

导入包了,还是报错,小白向含泪大神求助!!!

import urllib2
from cityCode import city
import json

location = raw_input('请输入你需要查询的城市名字:')

citycode=""

# 城市编码获取
try:
    citycode =city
except:
    print ("没找到")

url= "http://www.weather.com.cn/data/cityinfo/"+citycode+".html"#构造网址
# 发起查询
weather_json_string = urllib2.urlopen(url).read()

# 结果分析
weather = json.loads(weather_json_string)
print (weather)
# 结果打印
print (weather['weatherinfo']['city'])
print (weather['weatherinfo']['ptime'])
print (weather['weatherinfo']['cityid'])
print (weather['weatherinfo']['temp2'])
print (weather['weatherinfo']['temp1'])
print (weather['weatherinfo']['weather'])


报错信息:
Traceback (most recent call last):
File "C:/Users/CLARE/Desktop/tq.py", line 2, in <module>
    import urllib2
File "D:\anaconda\lib\site-packages\urllib2.py", line 220
    raise AttributeError, attr
                        ^
SyntaxError: invalid syntax

qiuyouzhi 发表于 2019-1-21 18:33:59

你这是Python2还是Python3?
页: [1]
查看完整版本: 导入包了,还是报错,小白向含泪大神求助!!!