|
- import requests
- from lxml import etree
- from pypinyin import lazy_pinyin
- a = input('请输入要查询的城市:')
- chengshi = lazy_pinyin(a,style=0)
- chengshi = ''.join(chengshi)
- url = f'https://www.tianqi.com/{chengshi}/'
- 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'}
- respon = requests.get(url=url,headers =ua).text
- jiexi = etree.HTML(respon)
- city = jiexi.xpath('//dd[@class="name"]/h1/text()')[0][0:2]
- tianqi = jiexi.xpath('//dd[@class="weather"]//span/b/text()')[0]
- quantianwendu = jiexi.xpath('//dd[@class="weather"]//span/text()')[0]
- kongqizhiliang = jiexi.xpath('//dd[@class="kongqi"]/h5/text()')[0]
- pm = jiexi.xpath('//dd[@class="kongqi"]/h6/text()')[0]
- pm = pm.split(':')[-1]
- pm = pm.strip()
- richuriluo = jiexi.xpath('//dd[@class="kongqi"]/span/text()')
- richuriluo = ','.join(richuriluo)
- shidu = jiexi.xpath('//dd[@class="shidu"]/b/text()')
- shidu = ','.join(shidu)
- riqi = jiexi.xpath('//dd[@class="week"]/text()')[0]
- print('城市:',city,'日期:',riqi)
- print(f'天气:{tianqi}')
- print(f'温度:{quantianwendu}')
- print(f'PM:{pm}')
- print(f'日出日落:{richuriluo}')
- print(f'{shidu}')
复制代码
|
|