Dotcpp  >  编程教程  >  Python爬虫  >  Python爬虫入门抓取天气信息

Python爬虫入门抓取天气信息

点击打开在线编译器,边学边练

问题:获取苏州8-15天的天气信息,包含: 日期、天气、温度、风力等信息,然后将数据存入一个文档中,网址为:http://www.weather.com.cn/weather/101190401.shtml

1. 问题分析

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

 python爬虫15

解析方式:我们通过BeautifulSoup中的方法来锁定信息,先找到对应的id和class,然后再找到‘ul’中‘class’为‘t clearfix’,然后找到所有的‘li’标签。

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

     python爬虫16

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名字获取到。

python爬虫17

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)

采用遍历的方式每次获取一个标签,最后输出相应的内容,然后存放在文档中。

2. 完整代码

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. 爬取结果

控制台:

('日期:周五(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级')

文档中:

         python爬虫18



本文固定URL:https://www.dotcpp.com/course/324

上一课:

BeautifulSoup(2)

Python教程
第一章 Python入门
第二章 Python基础语法
第三章 Python入门语法
第四章 Python核心语法
第五章 Python函数
第六章 Python面向对象编程
第七章 Python模块
第八章 Python异常处理和程序调试
第九章 Python文件及目录操作
第十章 PythonGUI编程
第十一章 Python进程和线程
第十二章 Python数据库管理
第十三章 Python算法
第十四章 Python爬虫
第十五章 Python实战项目
第十六章 后记
Dotcpp在线编译      (登录可减少运行等待时间)