鱼C论坛

 找回密码
 立即注册
查看: 2311|回复: 9

[已解决]031-永久存储-课件中天气查询

[复制链接]
发表于 2019-12-3 17:23:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
按照小甲鱼的步骤将城市数据保存为pkl文件,之后运行主程序,报错:

天气查询出错

天气查询出错

403应该是访问被拒,是不是因为这个爬虫代码太久之前的,所以无论访问哪一个网站几乎都会被拒绝(除过百度)?


最佳答案
2019-12-4 11:50:24
  1. import urllib.request
  2. import pickle
  3. import time
  4. import json
  5. pickle_file = open('city_data.pkl', 'rb')
  6. city = pickle.load(pickle_file)

  7. password = input('请输入城市:')
  8. cityid = city[password]

  9. url = r'http://d1.weather.com.cn/weather_index/{}.html?_={}'.format(cityid, str(int(time.time() * 1000)))
  10. headers = {
  11.     'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3947.100 Safari/537.36',
  12.     'Referer': 'http://www.weather.com.cn/weather1d/{}.shtml'.format(cityid)
  13. }
  14. request = urllib.request.Request(url=url, headers=headers)
  15. res = urllib.request.urlopen(request)
  16. js = res.read().decode('utf-8')
  17. print(js)
  18. #分析数据自己写吧
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-12-3 17:27:39 From FishC Mobile | 显示全部楼层
拿代码说话
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-3 20:16:52 | 显示全部楼层
代码呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-4 09:46:01 | 显示全部楼层
本帖最后由 亚瑟与精灵 于 2019-12-4 09:50 编辑

  1. import urllib.request
  2. import json
  3. import pickle

  4. pickle_file = open('D:\日志\code\city_data.pkl','rb')
  5. city = pickle.load(pickle_file)
  6. password=input('请输入城市:')
  7. name1=city[password]
  8. File1 =urllib.request.urlopen('http://m.weather.com.cn/data/'+name1+'.html')#打开url
  9. weatherHTML= File1.read().decode('utf-8')#读入打开的url
  10. weatherJSON = json.JSONDecoder().decode(weatherHTML)#创建json
  11. weatherInfo = weatherJSON['weatherinfo']
  12. #打印信息
  13. print ( '城市:', weatherInfo['city'])
  14. print ('时间:', weatherInfo['date_y'])
  15. print ( '24小时天气:')
  16. print ('温度:', weatherInfo['temp1'])
  17. print ('天气:', weatherInfo['weather1'])
  18. print ('风速:', weatherInfo['wind1'])
  19. print ('紫外线:', weatherInfo['index_uv'])
  20. print ('穿衣指数:', weatherInfo['index_d'])
  21. print ('48小时天气:')
  22. print ('温度:', weatherInfo['temp2'])
  23. print ('天气:', weatherInfo['weather2'])
  24. print ('风速:', weatherInfo['wind2'])
  25. print ('紫外线:', weatherInfo['index48_uv'])
  26. print ('穿衣指数:', weatherInfo['index48_d'])
  27. print ('72小时天气:')
  28. print ('温度:', weatherInfo['temp3'])
  29. print ('天气:', weatherInfo['weather3'])
  30. print ('风速:', weatherInfo['wind3'])
  31. input ('按任意键退出:')
复制代码

city_data.zip

27.31 KB, 下载次数: 102

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-4 09:48:19 | 显示全部楼层
本帖最后由 亚瑟与精灵 于 2019-12-4 09:50 编辑


代码已经贴出来了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-4 11:07:20 | 显示全部楼层
网址变了,还加了防爬措施
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-4 11:50:24 | 显示全部楼层    本楼为最佳答案   
  1. import urllib.request
  2. import pickle
  3. import time
  4. import json
  5. pickle_file = open('city_data.pkl', 'rb')
  6. city = pickle.load(pickle_file)

  7. password = input('请输入城市:')
  8. cityid = city[password]

  9. url = r'http://d1.weather.com.cn/weather_index/{}.html?_={}'.format(cityid, str(int(time.time() * 1000)))
  10. headers = {
  11.     'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3947.100 Safari/537.36',
  12.     'Referer': 'http://www.weather.com.cn/weather1d/{}.shtml'.format(cityid)
  13. }
  14. request = urllib.request.Request(url=url, headers=headers)
  15. res = urllib.request.urlopen(request)
  16. js = res.read().decode('utf-8')
  17. print(js)
  18. #分析数据自己写吧
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-4 16:11:50 | 显示全部楼层

可以讲解一下您url;headers;request吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-4 16:36:49 | 显示全部楼层
本帖最后由 kaohsing 于 2019-12-4 16:40 编辑

url是通过抓包获得的。
至于urllib.request.Request,三言两语,讲不清,请百度
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-27 21:26:11 | 显示全部楼层

谢谢,这是我的分析数据

  1. import urllib.request
  2. import pickle
  3. import time
  4. import json
  5. pickle_file = open('F:\\TestRoad\\2021-04--小甲鱼Python学习\\47天气预报\\city_data.pkl','rb')
  6. city = pickle.load(pickle_file)

  7. password = input('请输入城市:')
  8. cityid = city[password]

  9. url = r'http://d1.weather.com.cn/weather_index/{}.html?_={}'.format(cityid, str(int(time.time() * 1000)))
  10. headers = {
  11.     'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3947.100 Safari/537.36',
  12.     'Referer': 'http://www.weather.com.cn/weather1d/{}.shtml'.format(cityid)
  13. }
  14. request = urllib.request.Request(url=url, headers=headers)
  15. res = urllib.request.urlopen(request)
  16. js = res.read().decode('utf-8')
  17. #js = res.read().decode('unicode_escape')
  18. weather_info = js.split(';',1)[0]
  19. weather_info = weather_info.split('=', 1)[1]
  20. weather_dict = json.loads(weather_info)
  21. print('天气:', weather_dict.get('weatherinfo').get('weather'))

  22. #print(weather_dict)


复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 08:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表