[Python] 纯文本查看 复制代码
import re
import requests
import time
def get_url():
try:
num = input('请输入你要查询的城市:')
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18363',
'Cookie': 'Hm_lpvt_ab6a683aa97a52202eab5b3a9042a8d2=1593475715; Hm_lvt_ab6a683aa97a52202eab5b3a9042a8d2=1593475648; UM_distinctid=173028cca6411d1-01f7d541a39b96-71415a3a-1fa400-173028cca65a72; CNZZDATA1277722738=1358689083-1593473370-https%253A%252F%252Fwww.tianqi.com%252F%7C1593473370; CNZZDATA1275796416=466240679-1593471302-https%253A%252F%252Fwww.baidu.com%252F%7C1593471302; CNZZDATA1268732535=1963896862-1593474962-https%253A%252F%252Fwww.tianqi.com%252F%7C1593474962'
}
url = 'https://www.tianqi.com/' + num + '/'
global response
response = requests.post(url,headers = headers)
reg = r'<h2>(.*?)</h2>'
refind = re.findall(reg,response.text)
reg_time = r'<dd class="week">(.*?)</dd>'
refind_time = re.findall(reg_time,response.text)
reg_c = r'<p class="now"><b>(.*?)</b><i>.</i></p>'
refind_C = re.findall(reg_c,response.text)
reg_wter = r'<span><b>(.*?)</b>.*?</span>'
refind_wter = re.findall(reg_wter,response.text)
reg_wter_2 = r'<span><b>.*?</b>(.*?)</span>'
refind_wter_2 = re.findall(reg_wter_2,response.text)
reg_all = r'<dd class="shidu"><b>(.*?)</b><b>(.*?)</b><b>(.*?)</b></dd>'
refind_all = re.findall(reg_all,response.text)
reg_sun = r'<dd class="kongqi" ><h5 style="background-color:#79b800;">(.*?)</h5><h6>(.*?)</h6><span>(.*?)<br />(.*?)</span></dd>'
refind_sun = re.findall(reg_sun,response.text)
print('\n您现在看到的城市是:',refind[0],'\n')
print('\t日期:',refind_time[0],time.asctime())
print('\t温度:',refind_C[0],'℃','\t 最低温/最高温:',refind_wter_2[0])
print('\t天气状况:',refind_wter[0])
print('\t',refind_all[0][0])
print('\t',refind_all[0][1])
print('\t',refind_all[0][2])
print('\t',refind_sun[0][0],'\n\t',refind_sun[0][1],'\n\t',refind_sun[0][2],'\n\t',refind_sun[0][3])
except IndexError:
print('请输入具体城市名,不要输入省份!')
get_url()
def week():
global response
week1 = r'<li><b>(.*?)</b><span>(.*?)</span><img src=.*?></li>'
find_wek = re.findall(week1,response.text)
week2 = r'<li><span>(.*?)</span><b>(.*?)</b></li>'
find_wek2 = re.findall(week2,response.text)
week3 = r'<ul class="txt txt2"> *?(.*?)'
find_wek3 = re.findall(week3,response.text)
print('\t',find_wek[0][0],find_wek[0][1],'最高温:',find_wek2[0][0],'最低温:',find_wek2[0][1])
print('\t',find_wek[1][0],find_wek[1][1],'最高温:',find_wek2[1][0],'最低温:',find_wek2[1][1])
print('\t',find_wek[2][0],find_wek[2][1],'最高温:',find_wek2[2][0],'最低温:',find_wek2[2][1])
print('\t',find_wek[3][0],find_wek[3][1],'最高温:',find_wek2[3][0],'最低温:',find_wek2[3][1])
print('\t',find_wek[4][0],find_wek[4][1],'最高温:',find_wek2[4][0],'最低温:',find_wek2[4][1])
print('\t',find_wek[5][0],find_wek[5][1],'最高温:',find_wek2[5][0],'最低温:',find_wek2[5][1])
print('\t',find_wek[6][0],find_wek[6][1],'最高温:',find_wek2[6][0],'最低温:',find_wek2[6][1])
print(find_wek3)
if __name__ == '__main__':
print('\n','*'*16,'实时天气查询程序,输入时请输入该城市的拼音,没有查询到该城市则默认北京','*'*16,'\n')
print('例如: 北京 ---> beijing ')
get_url()
w = input('\n查询未来一周的天气?(是/否)')
if w == '是':
week()
else:
input('请按任意键退出...')
input('请按任意键退出...')