|
- # -*- coding: UTF-8 -*-
- #=================================================
- # language : Python3.4
- # IDLE : pycharm
- # Library needed : re,requests.urlib
- # Date : 05/02/2019
- #=================================================
- import re
- import requests
- from urllib.parse import quote
- class Tianqi2345():
- def __init__(self, city):
- self.city = city
- self.bm_url = 'https://tianqi.2345.com/t/searchCity.php?pType=local&q='
- self.ua = 'Mozilla/5.0 '
- self.ref = 'https://tianqi.2345.com/'
- self.headers = {'User-Agent': self.ua,'Accept': 'application/json, text/javascript, */*; q=0.01', 'Referer': self.ref }
- self.query_headers = {'User-Agent': self.ua,'Referer': self.ref,
- 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' }
- def get_city_code(self):
- try:
- r = requests.get(url=self.bm_url + quote(self.city), headers= self.headers)
- r.raise_for_status()
- r.encoding = r.apparent_encoding
- data = r.json()
- if data['res'][0]['href']:
- return data['res'][0]['href']
- else:
- print('没有'+ self.city + '的天气信息')
- except requests.ConnectionError as e:
- print('Erro')
- def get_html(self):
- url = 'https://tianqi.2345.com' + self.get_city_code()
- try:
- r = requests.get(url=url, headers= self.query_headers)
- r.raise_for_status()
- r.encoding = 'GBK'
- return r.text
- except requests.ConnectionError as e:
- print('Error')
- def parsed_data(self):
- text = self.get_html()
- r = re.compile(r'<p><strong>([^<]+)\(<font.*?>([^<]+)</font>\).*?<b>([^<]+)</b><i><font class="blue">'\
- '([^<]+)</font>~<font class="red">([^<]+)</font><br.*?((?<=>)[^<]+)</i>', re.M | re.S)
- result = re.findall(r, text)
- fbsj= 'From:2345天气预报'
- print(' -----' + self.city+ '天气预报' + '(未来'+ str(len(result))+'天)' + fbsj + '-----')
- print('-'*60)
- for p in result:
- p0, p1, p2, p3, p4, p5 = p
- print(" {0:^6}\t {1:^2}\t{2:{6}<6}\t{3:{6}<4}\t{4:{6}^3}\t{5:{6}<6}".format(p0, p1, p2, p3, p4, p5.replace(' ' , ''), chr(12288)))
- print('-'*60)
- if __name__ == '__main__':
- city= input("输入要查询的城市:")
- Tianqi2345(city).parsed_data()
复制代码
|
|