python网络爬虫开发:抓取天气信息

2022-08-0711:08:57云计算与物联网Comments954 views字数 2503阅读模式

问题:获取苏州8-15天的天气信息,包含: 日期、天气、温度、风力等信息,然后将数据存入一个文档中。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26711.html

    1. 问题分析文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26711.html

首先我们进入天气网,然后开始对页面进行分析。右键页面检查网页源代码或者F12或者Ctrl+Shift+F等进入当前页面,有html学习基础的可以直接在网页源码中找相应的信息标签,当然也可以直接点击左上方的按钮,开启快速查找,开启后可以点击网页中的信息及可迅速定位到该信息的网页源码。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26711.html

python网络爬虫开发:抓取天气信息文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26711.html

解析方式:我们通过BeautifulSoup中的方法来锁定信息,先找到对应的id和class,然后再找到‘ul’中‘class’为‘t clearfix’,然后找到所有的‘li’标签。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26711.html

1
weathers = soup.find(id="7d",class_="c7d").find('ul',class_="t clearfix").find_all('li')

python网络爬虫开发:抓取天气信息文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26711.html

1
2
3
4
5
res = requests.get('http://www.weather.com.cn/weather15d/101190401.shtml')
res.encoding = 'utf-8'
html =res.text
soup = BeautifulSoup(html,'html.parser')
weathers = soup.find(id="7d",class_="c7d").find('ul',class_="t clearfix").find_all('li')

当前的weathers锁定了weather区域,我们再通过BeatifulSoup来进行数据的解析,我们把weathers中的日期、天气、温度、风力的信息通过class名字获取到。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26711.html

python网络爬虫开发:抓取天气信息文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26711.html

1
2
3
4
5
6
7
8
9
10
for weather in weathers:
    weather_date = weather.find('span',class_="time")
    weather_wea = weather.find('span',class_="wea")
    weather_tem = weather.find('span',class_="tem")
    weather_wind = weather.find('span',class_="wind")
    weather_wind1 = weather.find('span',class_="wind1")
    result = '日期:'+weather_date.text,'天气:'+weather_wea.text,'温度:'+weather_tem.text,
    '风力:'+weather_wind.text+weather_wind1.text
    print(result)
    print(result,file=mylog)

采用遍历的方式每次获取一个标签,最后输出相应的内容,然后存放在文档中。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26711.html

    2. 完整代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26711.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import requests
from bs4 import BeautifulSoup
qy = open('C:/Users/轻烟/Desktop/db.txt',mode='a',encoding='utf-8')
res = requests.get('http://www.weather.com.cn/weather15d/101190401.shtml')
res.encoding = 'utf-8'
html = res.text
soup = BeautifulSoup(html,'html.parser')#解析文档
weathers = soup.find(id="15d",class_="c15d").find('ul',class_="t clearfix").find_all('li')
for weather in weathers:
    weather_date = weather.find('span',class_="time")
    weather_wea = weather.find('span',class_="wea")
    weather_tem = weather.find('span',class_="tem")
    weather_wind = weather.find('span',class_="wind")
    weather_wind1 = weather.find('span',class_="wind1")
    result = '日期:'+weather_date.text,'天气:'+weather_wea.text,'温度:'+weather_tem.text,'风力:'+weather_wind.text+weather_wind1.text
    print(result)#输出
    print(result,file = qy)#保存到文档中

    3. 爬取结果文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26711.html

控制台:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26711.html

1
2
3
4
5
6
7
8
('日期:周五(28日)''天气:雨''温度:12℃/6℃''风力:东风转北风3-4级')
('日期:周六(29日)''天气:阴''温度:11℃/3℃''风力:北风转东风<3级')
('日期:周日(1日)''天气:阴转多云''温度:12℃/5℃''风力:东南风转东北风<3级')
('日期:周一(2日)''天气:雨''温度:12℃/5℃''风力:北风<3级')
('日期:周二(3日)''天气:晴转多云''温度:9℃/3℃''风力:北风3-4级转<3级')
('日期:周三(4日)''天气:晴''温度:9℃/1℃''风力:北风<3级')
('日期:周四(5日)''天气:晴转多云''温度:8℃/2℃''风力:东北风转东南风<3级')
('日期:周五(6日)''天气:晴''温度:11℃/5℃''风力:东风<3级')

文档中:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26711.html

python网络爬虫开发:抓取天气信息文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26711.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/yunda/26711.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定