首页 > 编程语言 > 详细

python爬虫爬取各个城市历史天气及数据可视化

时间:2019-08-21 23:15:46      阅读:660      评论:0      收藏:0      [点我收藏+]

 

数据抓取

 

import asyncio
import aiohttp
from lxml import etree
import re
from collections import namedtuple

Args = namedtuple(Args,[city,year,month])
    
async def work(args):
    url = "http://www.tianqihoubao.com/lishi/%s/month/%d%02d.html" % (args.city, args.year, args.month)
    headers = {User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36}
    async with aiohttp.ClientSession() as session:
        async with session.get(url, headers=headers, timeout=1000, verify_ssl=False) as response:
            html = await response.text()
            ethtml = etree.HTML(html)
            result = [re.sub("\r\n *","",item.text)                         for item in ethtml.xpath("//table[@class=‘b‘]/tr/td") if not re.fullmatch("\r\n *",item.text)]
            return result

loop = asyncio.get_event_loop()
tasks = [asyncio.ensure_future(work(Args(wuhan, 2018, month))) for month in range(1,6)]
loop.run_until_complete(asyncio.wait(tasks))

for task in tasks:
    print(task.result())

 

python爬虫爬取各个城市历史天气及数据可视化

原文:https://www.cnblogs.com/plyonfly/p/11391848.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!