关于python爬虫:python爬取中国天气网7天天气并保存至本地

28次阅读

共计 2041 个字符,预计需要花费 6 分钟才能阅读完成。

1. 中国天气网
http://www.weather.com.cn/weather/101010100.shtml

2. 剖析页面

3. 浏览器 -F12- 定位查看元素嵌套关系

4. 导入须要的库

import requests
from bs4 import BeautifulSoup
import re

5. 代码局部

result_list_wt = []

def get_page(url):
    try:
        kv = {'user-agent':'Mozilla/5.0'}
        r = requests.get(url,headers = kv)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except:
        return 'error'

def parse_page(html, return_list):
    soup = BeautifulSoup(html, 'html.parser')
    day_list = soup.find('ul', 't clearfix').find_all('li')
    for day in day_list:
        date = day.find('h1').get_text()
        wea = day.find('p',  'wea').get_text()
        if day.find('p', 'tem').find('span'):
                hightem = day.find('p', 'tem').find('span').get_text()
        else:
                hightem = ''lowtem = day.find('p','tem').find('i').get_text() 
        win = re.findall('(?<= title=").*?(?=")', str(day.find('p','win').find('em')))
        wind = '-'.join(win)
        level = day.find('p', 'win').find('i').get_text()
        return_list.append([date, wea, lowtem, hightem, wind, level])

def print_res(return_list):
    tplt = '{0:<10}\t{1:^10}\t{2:^10}\t{3:{6}^10}\t{4:{6}^10}\t{5:{6}^5}' 
    result_list_wt.append(tplt.format('日期', '天气', '最低温', '最低温', '风向', '风力',chr(12288))+"\n") 
    for i in return_list:
        result_list_wt.append(tplt.format(i[0], i[1],i[2],i[3],i[4],i[5],chr(12288))+"\n")
        
def main():
    # 城市 - 城市码 txt
    files = open('city_list.txt',"r")
    city_name_id = files.readlines()
    try:
        # 获取 txt-list
        for line in city_name_id:
            name_id = line.split('-')[1].replace("['","").replace("\n","")
            url = 'http://www.weather.com.cn/weather/'+name_id+'.shtml' 
            city_name = line.split('-')[0].replace("['","").replace("\n","")
            city_china = "\n"+"城市名 :"+city_name+"\n"
            result_list_wt.append(city_china)
            html = get_page(url)
            wea_list = []
            parse_page(html, wea_list)
            print_res(wea_list)
        files.close()
    except:
        print("error")
    # 将获取后果写入到文件内    
    msgs = ''.join(result_list_wt)
    print(msgs)
    with open('weather.China.txt',"w+") as file:
        file.write(msgs)
        
if __name__ == '__main__':
    main()

6.city_list.txt

 上海 -101020100
苏州 -101190401
无锡 -101190201
南京 -101190101
镇江 -101190301
宜兴 -101190203
扬州 -101190601
常州 -101191101
杭州 -101210101
宁波 -101210401
义乌 -101210904
温州 -101210701
台州 -101210601
湖州 -101210201
金华 -101210901
绍兴 -101210507

7. 用处

1. 推送到企业微信
2. 推送到叮叮
3. 可定制 @固定人员或推送到指定群组
4. 变成机器人揭示
5. 定时抓取判断以后城市的天气情况, 利用到不同的业务场景

8. 后果

正文完
 0