爬虫练习案例:爬取天气并可视化

前言

    爬取各省会城市的气温,并用pyecharts库进行可视化

    数据来源:中国天气网 www.weather.com.cn

 

代码

import urllib.request
from urllib.request import quote
from bs4 import BeautifulSoup
import re
from pyecharts import Geo

''' 省会城市+直辖市 '''
name0 = ['石家庄','沈阳','哈尔滨','杭州','福州','济南','广州','武汉','成都','昆明','兰州','台北','南宁','银川','太原','长春','南京','合肥','南昌','郑州','长沙','海口','贵阳','西安','西宁','呼和浩特','拉萨','乌鲁木齐','上海','北京','重庆','天津']
  
lowTemp = []  # 记录最高气温
highTemp = [] # 记录最低气温

for i in range(len(name0)):
    name = quote(name0[i]) # URL编码

    '''获取城市代号编码'''
    url0 = 'http://toy1.weather.com.cn/search?cityname={}'.format(name)
    html0 = urllib.request.urlopen(url0)
    html0 = html0.read().decode('utf-8')
    code = re.search("\d{9}" , html0)

    url = "http://www.weather.com.cn/weather/{}.shtml".format(code[0])

    header = ("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36")
    opener = urllib.request.build_opener()
    opener.addheaders = [header]        
    request = urllib.request.Request(url)   
    response = urllib.request.urlopen(request) 
    html = response.read().decode('utf-8')

    bs = BeautifulSoup(html,"html.parser")
    body = bs.body
    data = body.find('div',{'id':'7d'})
    ul = data.find('ul')
    li = ul.find_all('li')[0]  # 获取今日天气li
    inf = li.find_all('p')  # 找到li中的所有p标签

    if inf[1].find('span') is None:
        temperature_highest = None # 天气预报可能没有当天的最高气温(到了傍晚会出现这种情况)
    else:
        temperature_highest = inf[1].find('span').string # 找到最高温度
        temperature_highest = temperature_highest.replace('℃', '') # 到了晚上网站会变,最高温度后面也有个℃
    temperature_lowest = inf[1].find('i').string  # 找到最低温度
    temperature_lowest = temperature_lowest.replace('℃', '')  # 最低温度后面有个℃,去掉这个符号
    
    highTemp.append(temperature_highest)
    lowTemp.append(temperature_lowest)

    print('\r成功获取{0}的天气情况...'.format(name0[i]),end='')


geo = Geo("全国省会城市最低气温", "Temperature", 
    title_color="#fff", title_pos="center",width=1200, height=600, 
    background_color='#404a59')
attr, value = name0, lowTemp
geo.add("", attr, value, visual_range=[-30, 30], 
    visual_text_color="#fff", symbol_size=15, is_visualmap=True,
    type="effectScatter",effect_scale=3)
geo.render('全国省会城市最低气温图.html')

 

效果

# type = "effectScatter"
type = "heatmap"

# 添加属性实现数值显示
is_label_show = True

 

案例

    爬虫练习案例:交通路况

    爬虫练习案例:使用代理IP

    爬虫练习案例:爬取天气定时发送至微信/邮箱

    爬虫练习案例:豆瓣电影Top250

 

其他

    pyecharts库安装底图

pip install echarts-countries-pypkg

pip install echarts-china-provinces-pypkg

pip install echarts-china-cities-pypkg

    geo.add()属性表

# 数据项 (坐标点名称)
names:list
 
# 数据项 (坐标点值)
value: list
 
# Geo 图类型,有 scatter, effectScatter, heatmap, lines 4 种
type: 默认"scatter",

# 是否选中图例
is_selected: 默认True,
 
# 标记图形形状,circle,pin,rect,diamon,roundRect,arrow,triangle
symbol: 默认circle,

# 标记的大小
symbol_size: 默认12,

# 系列 label 颜色
color: 默认None,

# 是否是多段线,在画 lines 图情况下
is_polyline: 默认 False,

# 是否启用大规模线图的优化,在数据图形特别多的时候(>=5k)可以开启
is_large: 默认 False,

# 特效尾迹的长度。取从 0 到 1 的值,数值越大尾迹越长。
trail_length:默认 0.2

# 地图选择,如广东、广州、china
maptype = "china"

#是否开启鼠标缩放和平移漫游。'scale'缩放、'move'平移、'True'都开启
is_roam:默认True

#显示图例条
is_visualmap:默认True

#图例条范围
visual_range = [-20,20]

#图例条颜色
visual_text_color

#常态下地图的颜色
geo_normal_color

#触发下地图的颜色(鼠标放在地图上)
geo_emphasis_color

#涟漪的多少,当type="effectScatter"时才有效
effect_scale

#显示标签
is_label_show

#标签颜色
label_text_color

#标签位置(inside,top,bottom,left,right)
label_pos:默认"inside"

#边界颜色
border_color

⭐希望大家多多点赞、收藏!

  • 11
    点赞
  • 92
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值