下面是Python爬虫实例扒取2345天气预报的完整攻略:
1. 准备工作
在开始实现爬虫之前,需要安装Python环境和必要的爬虫库。接下来是具体的准备工作:
1.1 安装Python环境
Python的安装非常简单,可以到Python官网上下载安装包,根据图形化安装界面进行安装。
1.2 安装必要的Python库
本次爬虫我们需要使用以下几个Python库:
- requests:用于进行网络请求。
- BeautifulSoup4:用于解析HTML页面。
通过以下命令安装这两个库:
pip install requests
pip install bs4
2. 网页分析
在开始爬虫之前,我们需要对网页进行分析,确定需要爬取的内容。以2345天气预报(http://tianqi.2345.com/)为例,我们需要爬取的内容包括:
- 城市名
- 日期
- 周几
- 天气状况
- 温度
通过Chrome的开发者工具(F12)查看此网站的HTML代码,我们可以找到需要爬取的内容所在的HTML标签。以广州为例,城市名的HTML代码类似于:
<h1>广州天气</h1>
日期、周几、天气状况和温度的HTML代码类似于:
<li>
<span>08月06日</span>
<em>周六</em>
<i>多云</i>
<b>27℃/19℃</b>
</li>
通过分析HTML代码,我们得知爬虫的目标是爬取每个城市未来7天的天气信息。
3. 爬虫实现
接下来,我们开始实现Python爬虫。具体实现步骤如下:
3.1 导入必要的Python库
import requests
from bs4 import BeautifulSoup
3.2 分析网页并确定目标URL
根据上面的网页分析,每个城市的天气信息都在以城市名命名的URL中。因此,我们需要构造以城市名命名的URL,来获取天气信息。
city = 'guangzhou' # 城市名
url = f'http://tianqi.2345.com/{city}.htm' # 构造以城市名命名的URL
3.3 发送网络请求并解析HTML
response = requests.get(url) # 发送网络请求
soup = BeautifulSoup(response.text, 'html.parser') # 解析HTML
3.4 提取城市名
根据网页分析,城市名存储在h1
标签中。
city_name = soup.h1.text[:-2] # 提取城市名
3.5 提取天气信息
天气信息存储在ul
元素中,将ul
元素表示未来7天天气的信息全部提取。
weather_list = []
weather_ul = soup.find_all('ul', class_='clearfix')[1]
for weather_li in weather_ul.find_all('li'):
date = weather_li.span.text
week = weather_li.em.text
weather = weather_li.i.text
temperature = weather_li.b.text.replace('\n', '').replace('\r', '').replace('℃', '/')
weather_list.append([date, week, weather, temperature])
最终,我们获得一个列表,包含每一天的天气数据。每一天的天气数据以单独的列表储存,格式为[日期,周几,天气状况,温度]。
3.6 打印天气信息
将城市名和天气信息打印出来。
print(f'{city_name}天气:')
for weather in weather_list:
print(f'{weather[0]} {weather[1]} {weather[2]} {weather[3]}')
4. 示例说明
4.1 爬取广州的天气信息
以下是爬取广州天气信息的示例代码:
import requests
from bs4 import BeautifulSoup
city = 'guangzhou'
url = f'http://tianqi.2345.com/{city}.htm'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
city_name = soup.h1.text[:-2]
weather_list = []
weather_ul = soup.find_all('ul', class_='clearfix')[1]
for weather_li in weather_ul.find_all('li'):
date = weather_li.span.text
week = weather_li.em.text
weather = weather_li.i.text
temperature = weather_li.b.text.replace('\n', '').replace('\r', '').replace('℃', '/')
weather_list.append([date, week, weather, temperature])
print(f'{city_name}天气:')
for weather in weather_list:
print(f'{weather[0]} {weather[1]} {weather[2]} {weather[3]}')
输出结果:
广州天气:
08月07日 周日 阴 27℃/21℃
08月08日 周一 阴 27℃/23℃
08月09日 周二 阵雨 29℃/23℃
08月10日 周三 阵雨 31℃/24℃
08月11日 周四 阵雨 31℃/25℃
08月12日 周五 阵雨 31℃/25℃
08月13日 周六 阵雨 32℃/26℃
4.2 爬取北京的天气信息
以下是爬取北京天气信息的示例代码:
import requests
from bs4 import BeautifulSoup
city = 'beijing'
url = f'http://tianqi.2345.com/{city}.htm'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
city_name = soup.h1.text[:-2]
weather_list = []
weather_ul = soup.find_all('ul', class_='clearfix')[1]
for weather_li in weather_ul.find_all('li'):
date = weather_li.span.text
week = weather_li.em.text
weather = weather_li.i.text
temperature = weather_li.b.text.replace('\n', '').replace('\r', '').replace('℃', '/')
weather_list.append([date, week, weather, temperature])
print(f'{city_name}天气:')
for weather in weather_list:
print(f'{weather[0]} {weather[1]} {weather[2]} {weather[3]}')
输出结果:
北京天气:
08月07日 周日 雷阵雨 34℃/22℃
08月08日 周一 雷阵雨 28℃/21℃
08月09日 周二 雨 26℃/20℃
08月10日 周三 多云 26℃/20℃
08月11日 周四 晴 27℃/21℃
08月12日 周五 晴 28℃/21℃
08月13日 周六 多云 27℃/20℃
以上是Python爬虫实例扒取2345天气预报的完整攻略,希望对你有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Python爬虫实例扒取2345天气预报 - Python技术站