加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 10:54:46 所属栏目:Python 来源:网络整理
导读:? ? ? ? ? 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

?

?

?

?

?

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("rn *","",item.text) for item in ethtml.xpath("//table[@class=‘b‘]/tr/td") if not re.fullmatch("rn *",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())

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读