Python爬取天气网历史天气数据

我的第一篇博客,哈哈哈,记录一下我的Python进阶之路!

今天写了一个简单的爬虫。

使用python的requests 和BeautifulSoup模块,Python 2.7.12可在命令行中直接使用pip进行模块安装。爬虫的核心是利用BeautifulSoup的select语句获取需要的信息。

pip install requests
pip install bs4

以武汉市2017年5~7月的历史为例爬取天气网中武汉市的历史天气数据。 
7月对应的网址为http://lishi.tianqi.com/wuhan/201707.html

1.requests模块获取网页内容

url='http://lishi.tianqi.com/wuhan/201707.html'
response = requests.get(url)                          
soup = BeautifulSoup(response.text, 'html.parser')    

2.利用.select语句找到网页中天气数据所在的div

weather_list = soup.select('div[class="tqtongji2"]') 

天气数据所在的class

3.找出日期、最高气温、最低气温、天气等数据,用li.string获取li中的信息。

ul_list = weather.select('ul')
for ul in ul_list:
    li_list= ul.select('li') for li in li_list: li.string.encode('utf-8') #具体的天气信息

详细天气信息获取

具体代码实现如下:

#encoding:utf-8                                                                      
import requests                                                                      
from bs4 import BeautifulSoup                                                        

urls = ["http://lishi.tianqi.com/wuhan/201707.html",                                 
        "http://lishi.tianqi.com/wuhan/201706.html",                                 
        "http://lishi.tianqi.com/wuhan/201705.html"] file = open('wuhan_weather.csv','w') for url in urls: response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') weather_list = soup.select('div[class="tqtongji2"]') for weather in weather_list: weather_date = weather.select('a')[0].string.encode('utf-8') ul_list = weather.select('ul') i=0 for ul in ul_list: li_list= ul.select('li') str="" for li in li_list: str += li.string.encode('utf-8')+',' if i!=0: file.write(str+'\n') i+=1 file.close() 

最后的结果: 
武汉5~7月天气

相比正则表达式,使用select语句爬取数据就是这么简单啦! 
正则表达式还不是很理解,等理清楚,再来写总结。

从csdn搬过来,体验一下http://blog.csdn.net/haha_point/article/details/77197230

转载于:https://www.cnblogs.com/haha-point/p/7467221.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您想了解如何用 Python 爬取历史天气数据,是吗? 首先,需要确定您想要爬取哪个地区的历史天气数据。一般来说,可以通过查询该地区的气象局或气象站获取历史天气数据。 接着,可以使用 Python 中的 requests 库来发送 HTTP 请求,获取页源代码。然后,可以使用 BeautifulSoup 库来解析页源代码,提取出所需的天气数据。 以下是一个简单的示例代码,用于爬取中国天气上北京市 2019 年 1 月份的历史天气数据: ``` python import requests from bs4 import BeautifulSoup url = 'http://www.weather.com.cn/weather/101010100.shtml#dt' headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} r = requests.get(url, headers=headers) r.encoding = 'utf-8' soup = BeautifulSoup(r.text, 'html.parser') weather_list = soup.select('.t li') for weather in weather_list: date = weather.select('.time')[0].text weather_detail = weather.select('.temp')[0].text.strip() print(date, weather_detail) ``` 以上代码中,我们首先通过 requests 库发送了一个 GET 请求,获取了北京市天气页源代码。然后,使用 BeautifulSoup 库解析页源代码,提取出了 2019 年 1 月份的天气数据,并打印输出了日期和天气详情。 需要注意的是,不同的页结构不同,需要根据具体情况修改代码。同时,需要注意站的 robots.txt 文件,不要过度访问站,以免被封 IP 或其他限制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值