Datehoer

Datehoer 关注TA

zjzdmc.top

Datehoer

Datehoer

关注TA

zjzdmc.top

  •  未知
  • 未知
  • 写了5,902字

该文章投稿至Nemo社区   Python  板块 复制链接


python爬取天气

发布于 2020/11/08 22:11 16,549浏览 0回复 5,873

获取天气情况

用到的模块有 :
re,requests,time

大体思路:(1)首先找到目标网站,向该网站发送请求,得到响应内容
(2)通过re模块对获得的响应内容进行筛选(也可以是美汤)
(3)一周后查找天气情况,再次筛选


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>(.*?)</dd>'
        refind_time = re.findall(reg_time,response.text)
 
        reg_c = r'<p><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><b>(.*?)</b><b>(.*?)</b><b>(.*?)</b></dd>'
        refind_all = re.findall(reg_all,response.text)
 
        reg_sun = r'<dd ><h5>(.*?)</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> *?(.*?)'
    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('请按任意键退出...')
本文标签
 {{tag}}
点了个评