找回密码
 立即注册
搜索
热搜: Excel discuz
查看: 2107|回复: 1

[项目代码] 天气预报查询

[复制链接]

492

主题

7万

元宝

77万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
770449
发表于 2021-8-12 15:48:20 | 显示全部楼层 |阅读模式
  1. import requests
  2. from lxml import etree
  3. from pypinyin import lazy_pinyin
  4. a = input('请输入要查询的城市:')
  5. chengshi = lazy_pinyin(a,style=0)
  6. chengshi = ''.join(chengshi)
  7. url = f'https://www.tianqi.com/{chengshi}/'
  8. ua = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36'}
  9. respon = requests.get(url=url,headers =ua).text
  10. jiexi = etree.HTML(respon)
  11. city = jiexi.xpath('//dd[@class="name"]/h1/text()')[0][0:2]
  12. tianqi = jiexi.xpath('//dd[@class="weather"]//span/b/text()')[0]
  13. quantianwendu = jiexi.xpath('//dd[@class="weather"]//span/text()')[0]
  14. kongqizhiliang = jiexi.xpath('//dd[@class="kongqi"]/h5/text()')[0]
  15. pm = jiexi.xpath('//dd[@class="kongqi"]/h6/text()')[0]
  16. pm = pm.split(':')[-1]
  17. pm = pm.strip()
  18. richuriluo = jiexi.xpath('//dd[@class="kongqi"]/span/text()')
  19. richuriluo = ','.join(richuriluo)
  20. shidu = jiexi.xpath('//dd[@class="shidu"]/b/text()')
  21. shidu = ','.join(shidu)
  22. riqi = jiexi.xpath('//dd[@class="week"]/text()')[0]
  23. print('城市:',city,'日期:',riqi)
  24. print(f'天气:{tianqi}')
  25. print(f'温度:{quantianwendu}')
  26. print(f'PM:{pm}')
  27. print(f'日出日落:{richuriluo}')
  28. print(f'{shidu}')
复制代码



回复

使用道具 举报

492

主题

7万

元宝

77万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
770449
 楼主| 发表于 2021-8-12 16:55:36 | 显示全部楼层
3天预报查询,代码自己写的,有点粗糙
  1. import requests
  2. from lxml import etree
  3. from pypinyin import lazy_pinyin
  4. a = input('请输入要查询的城市:')
  5. chengshi = lazy_pinyin(a,style=0)
  6. chengshi = ''.join(chengshi)
  7. url = f'https://www.tianqi.com/{chengshi}/3'
  8. ua = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36'}
  9. respon = requests.get(url=url,headers =ua).text
  10. jiexi = etree.HTML(respon)
  11. shijian = jiexi.xpath('//div[@class="weaul_q weaul_qblue"]/span/text()')
  12. shijian = ''.join(shijian)
  13. shijian = shijian.split('天')[0:3]
  14. riqi = []
  15. for i in shijian:
  16.     j = i + '天'
  17.     riqi.append(j)
  18. tianqi = jiexi.xpath('//ul[@class="weaul"]//div[@class="weaul_z"]/text()')
  19. tianqi = tianqi[: : 3]
  20. wendu = jiexi.xpath('//div[@class="weaul_z"]//text()')
  21. a = ''.join(wendu)
  22. b = a.split('℃')[0:3]
  23. for i in range(len(tianqi)):
  24.     print(f'{riqi[i]},{b[i]}℃')
复制代码



回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|五花八门论坛 ( 豫ICP备15031300号-3 )

GMT+8, 2024-12-22 10:06 , Processed in 0.064482 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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