
以爬去深圳城市七天天气为例,BeautifulSoup爬去天气数据,echart绘制最高气温与最低气温折线图。
1、目标网站
https://www.tianqi.com/shenzhen/2、依赖库
import requests
from bs4 import BeautifulSoup
from pyecharts.charts import Line
from pyecharts import options3、完整代码
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File : WeatherSpider.py
@Time : 2020/01/02 20:18:47
@Author : MrYong
@Version : 1.0
@Contact : m.yong@foxmail.com
@License : (C)Copyright 2018-2019, MR YONG
@Desc : 城市天气爬去并绘制图表
'''
# here put the import lib
import requests
from bs4 import BeautifulSoup
from pyecharts.charts import Line
from pyecharts import options
class WeatherSpider(object):
def __init__(self):
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}
self.proxies = {
'http':'http://web-proxy.tencent.com:8080/',
'https':'http://web-proxy.tencent.com:8080/'
}
self.url = 'https://www.tianqi.com/shenzhen/'
def run(self):
page = self.getPage()
data = self.parse(page)
self.draw(data)
def draw(self, data):
attr = data['week']
chart = Line()
chart.set_global_opts(title_opts=options.TitleOpts(title="深圳一周温度趋势"))
chart.add_xaxis(attr)
chart.add_yaxis('最高气温', data['high'])
chart.add_yaxis('最低气温', data['low'])
chart.render('深圳天气.html')
def parse(self, html):
soup = BeautifulSoup(html, 'lxml')
time_html = soup.select('ul.week li')
#print(time_html)
week_list = []
date_list = []
#解析时间轴
for item in time_html:
date = item.select('li b')[0].get_text()
week = item.select('li span')[0].get_text()
week_list.append(week)
date_list.append(date)
#print(week)
#解析最高最低气温
temp = soup.select('.zxt_shuju ul li')
high_temp = []
low_temp = []
for item in temp:
high = item.select('li span')[0].get_text()
low = item.select('li b')[0].get_text()
high_temp.append(high)
low_temp.append(low)
res = {
'week':week_list,
'date':date_list,
'high':high_temp,
'low':low_temp
}
return res
def getPage(self):
response = requests.get(self.url, headers=self.headers, proxies=self.proxies)
text = response.content.decode('utf-8')
return text
ws = WeatherSpider();
ws.run()
评论
暂无评论
评论(2)