当前位置: 首页> 教育> 幼教 > 稳定的网站制作需要多少钱_bootstrap模板网站_谷歌seo排名优化服务_网络营销服务的特点

稳定的网站制作需要多少钱_bootstrap模板网站_谷歌seo排名优化服务_网络营销服务的特点

时间:2025/7/11 0:53:25来源:https://blog.csdn.net/lhx19373106160/article/details/142895089 浏览次数:0次
稳定的网站制作需要多少钱_bootstrap模板网站_谷歌seo排名优化服务_网络营销服务的特点

该篇将进行城市天气信息爬取实战,主要涉及到网页url解析、正则表达匹配等技术,可用作网页爬虫练手项目。

1 Python网页爬虫简介

Python是一种广受欢迎的编程语言,广泛应用于各类应用开发,网页爬虫开发也是其中之一。网页爬虫是一种自动化工具,它在互联网上进行数据搜集。Python拥有丰富的库和框架,使得构建网页爬虫变得相对简单。

网页爬虫的工作方式类似于人类浏览网页,它向网站发出请求,获取网页的HTML源码,然后分析这些代码以提取所需信息。Python的requests库和BeautifulSoup库是实现这一过程的关键组件。

一个典型的Python网页爬虫系统包括以下几个核心组件:

  1. 调度器(Scheduler):负责调度URL管理器、下载器、解析器之间的协调工作。

  2. URL管理器(URL Manager):包括待爬取的URL地址和已爬取的URL地址,防止重复抓取URL和循环抓取URL。

  3. 网页下载器(Web Downloader):负责从互联网上下载网页的HTML代码。

  4. 网页解析器(Web Parser):负责解析网页的HTML代码,提取出所需的数据。

  5. 应用程序(Application):从网页中提取的有用数据组成的一个应用。

在开发Python爬虫时,开发者需要尊重网站的robots.txt文件,以确保不违反任何规定或造成网站负担。同时,也需要妥善处理网络异常和错误,保证爬虫的稳定性和可靠性。

综上所述,Python凭借其强大的库和工具,成为开发高效、稳定网页爬虫的理想选择,使开发者能够轻松地从互联网中抓取所需数据。

2 爬虫实战

2.1 导入相关包

import requests
import pandas as pd
import re

2.2 爬取时间范围及城市设置

months = [1,2,3,4,5,6,7,8,9,10,11,12]
years = [2016,2017,2018,2019,2020,2021,2022,2023] 
citys = [59287]

此处城市代码选取‘59287’,实际操作可另选区域或多区域。

2.3 爬取信息设置

index_ = ['MaxTemp','MinTemp', 'WindDir', 'Wind', 'Weather','Aqi','AqiInfo','AqiLevel'] 
# 选取的气象要素

2.4 天气信息抓取

data = pd.DataFrame(columns=index_)  # 建立一个空dataframe
for c in citys:for y in years:for m in months:# 找到json格式数据的urlif (y<2017) or (y==2017)&(m<=11):url = "http://tianqi.2345.com/t/wea_history/js/"+str(c)+"_"+str(y)+str(m)+".js" # ?qq-pf-to=pcqq.c2celse:url = "http://tianqi.2345.com/t/wea_history/js/"+str(y)+str(m).zfill(2)+"/"+str(c)+"_"+str(y)+str(m).zfill(2)+".js"print(url)response = requests.get(url=url)if response.status_code == 200:  # 防止url请求无响应response2 = response.text.replace("'", '"')  # 这一步可以忽略#  利用正则表达式获取各个气象要素(方法不唯一)date = re.findall("[0-9]{4}-[0-9]{2}-[0-9]{2}", response2)[:-2]mintemp = re.findall('yWendu:"(.*?)℃', response2)maxtemp = re.findall('bWendu:"(.*?)℃', response2)winddir = re.findall('fengxiang:"([\u4E00-\u9FA5]+)',response2)wind = re.findall('fengli:"([\u4E00-\u9FA5]+)',response2)weather = re.findall('tianqi:"([[\u4E00-\u9FA5]+)~?', response2)aqi = re.findall('aqi:"(\d*)',response2)aqiInfo = re.findall('aqiInfo:"([\u4E00-\u9FA5]+)',response2)aqiLevel = re.findall('aqiLevel:"(\d*)',response2)data_spider = pd.DataFrame([maxtemp,mintemp, winddir, wind, weather,aqi,aqiInfo,aqiLevel]).Tdata_spider.columns = index_  # 修改列名data_spider.index = date  # 修改索引data = pd.concat((data,data_spider), axis=0)  # 数据拼接print('%s年%s月的数据抓取成功' % (y, m))else:print('%s年%s月的数据不存在' % (y, m))break

2.4 结果存储

data.to_excel('D:\\天气数据可视化\\天气数据可视化.xlsx')
print('爬取数据展示:\n', data)

2.5 效果展示

3 完整代码

import requests
import pandas as pd
import remonths = [1,2,3,4,5,6,7,8,9,10,11,12]
years = [2016,2017,2018,2019,2020,2021,2022,2023] 
citys = [59287] index_ = ['MaxTemp','MinTemp', 'WindDir', 'Wind', 'Weather','Aqi','AqiInfo','AqiLevel']  # 选取的气象要素
data = pd.DataFrame(columns=index_)  # 建立一个空dataframe
for c in citys:for y in years:for m in months:# 找到json格式数据的urlif (y<2017) or (y==2017)&(m<=11):url = "http://tianqi.2345.com/t/wea_history/js/"+str(c)+"_"+str(y)+str(m)+".js" # ?qq-pf-to=pcqq.c2celse:url = "http://tianqi.2345.com/t/wea_history/js/"+str(y)+str(m).zfill(2)+"/"+str(c)+"_"+str(y)+str(m).zfill(2)+".js"print(url)response = requests.get(url=url)if response.status_code == 200:  # 防止url请求无响应response2 = response.text.replace("'", '"')  # 这一步可以忽略#  利用正则表达式获取各个气象要素(方法不唯一)date = re.findall("[0-9]{4}-[0-9]{2}-[0-9]{2}", response2)[:-2]mintemp = re.findall('yWendu:"(.*?)℃', response2)maxtemp = re.findall('bWendu:"(.*?)℃', response2)winddir = re.findall('fengxiang:"([\u4E00-\u9FA5]+)',response2)wind = re.findall('fengli:"([\u4E00-\u9FA5]+)',response2)weather = re.findall('tianqi:"([[\u4E00-\u9FA5]+)~?', response2)aqi = re.findall('aqi:"(\d*)',response2)aqiInfo = re.findall('aqiInfo:"([\u4E00-\u9FA5]+)',response2)aqiLevel = re.findall('aqiLevel:"(\d*)',response2)data_spider = pd.DataFrame([maxtemp,mintemp, winddir, wind, weather,aqi,aqiInfo,aqiLevel]).Tdata_spider.columns = index_  # 修改列名data_spider.index = date  # 修改索引data = pd.concat((data,data_spider), axis=0)  # 数据拼接print('%s年%s月的数据抓取成功' % (y, m))else:print('%s年%s月的数据不存在' % (y, m))break
data.to_excel('D:\\天气数据可视化\\天气数据可视化.xlsx')
print('爬取数据展示:\n', data)

Python学习资料(项目源码、安装包、激活码、电子书、视频教程)已经打包好啦!

需要的小伙伴【点击领取】哦!或者下方扫码拿走!

 

关键字:稳定的网站制作需要多少钱_bootstrap模板网站_谷歌seo排名优化服务_网络营销服务的特点

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: