python爬虫之静态网页——全国空气质量指数(AQI)爬取
首先爬取地址:http://www.air-level.com/
利用的python库,最近最流行的requests,BeautifulSoup。
requests:用于下载html
BeautifulSoup:用于解析
下面开始分析:要获取所有城市的aqi,就要进入每个城市的单独链接,而这些链接可以从主页中获取
打开主网页,查看源代码,可以看到,所有的城市链接都在id=‘citylist’里面
把所有链接爬下来存在一个列表里面,然后依次爬取每个城市的单个链接,附代码:
def get_all_city(): # 爬取城市链接
url = "http://www.air-level.com"
try:
kv = {'user-agent': 'Mozilla/5.0'} # 伪装成浏览器,headers
r = requests.get(url, headers=kv)
r.raise_for_status()
r.encoding = r.apparent_encoding
except:
print("爬取城市链接失败")
demo = r.text
soup = BeautifulSoup(demo, "html.parser")
time = soup.find('h4').string
print(time)
for it in soup.find(id="citylist").children:
if isinstance(it, bs4.element.Tag): # 检测it的类型,得是一个bs4.element.Tag类型
for its in it.find_all('a'):
clist.append(its.get('href')) # 加入列表当中去
cnlist.append(its.string)
之后就是每个城市的单独链接的信息爬取,以北京为例,查看源代码可知:
附爬取每个城市代码:
def get_one_page(city): # 获得HTML 爬取城市信息
url = "http://www.air-level.com"+city
if city in cwlink:
aqilist.append("异常链接")
else:
try:
kv = {'user-agent': 'Mozilla/5.0'} # 伪装成浏览器,headers
r = requests.get(url, headers=kv)
r.raise_for_status()
r.encoding = r.apparent_encoding
except:
print("爬取失败")
demo = r.text
soup = BeautifulSoup(demo, "html.parser")
s = soup.find("span")
aqilist.append(s.string)
但是在爬取的过程中会发现问题,有的一些城市网站用浏览器打不开,也就爬取不了,所以要做处理,
在上面可以看到,本人用cwlist存储了所有异常链接,跳过去,不爬取。
附完整代码:
import requests
from bs4 import BeautifulSoup
import bs4
aqilist = [] # 储存城市AQI
clist = [] # 储存城市链接
cnlist = [] # 储存城市名字
cwlink = ["/air/changdudiqu/", "/air/kezilesuzhou/", "/air/linzhidiqu/", "/air/rikazediqu/",
"/air/shannandiqu/", "/air/simao/", "/air/xiangfan/", "/air/yilihasake/"] # 异常链接
def get_one_page(city): # 获得HTML 爬取城市信息
url = "http://www.air-level.com"+city
if city in cwlink:
aqilist.append("异常链接")
else:
try:
kv = {'user-agent': 'Mozilla/5.0'} # 伪装成浏览器,headers
r = requests.get(url, headers=kv)
r.raise_for_status()
r.encoding = r.apparent_encoding
except:
print("爬取失败")
demo = r.text
soup = BeautifulSoup(demo, "html.parser")
s = soup.find("span")
aqilist.append(s.string)
def get_all_city(): # 爬取城市链接
url = "http://www.air-level.com"
try:
kv = {'user-agent': 'Mozilla/5.0'} # 伪装成浏览器,headers
r = requests.get(url, headers=kv)
r.raise_for_status()
r.encoding = r.apparent_encoding
except:
print("爬取城市链接失败")
demo = r.text
soup = BeautifulSoup(demo, "html.parser")
time = soup.find('h4').string
print(time)
for it in soup.find(id="citylist").children:
if isinstance(it, bs4.element.Tag): # 检测it的类型,得是一个bs4.element.Tag类型
for its in it.find_all('a'):
clist.append(its.get('href')) # 加入列表当中去
cnlist.append(its.string)
def main():
get_all_city()
print("共爬取了{}个城市".format(len(clist)))
for it in range(len(clist)):
get_one_page(clist[it])
print("{} {}".format(cnlist[it], aqilist[it]))
main()
简单的静态爬取就实现了
python爬虫之静态网页——全国空气质量指数(AQI)爬取的更多相关文章
- Python爬虫小实践:寻找失踪人口,爬取失踪儿童信息并写成csv文件,方便存入数据库
前两天有人私信我,让我爬这个网站,http://bbs.baobeihuijia.com/forum-191-1.html上的失踪儿童信息,准备根据失踪儿童的失踪时的地理位置来更好的寻找失踪儿童,这种 ...
- Python爬虫入门教程 12-100 半次元COS图爬取
半次元COS图爬取-写在前面 今天在浏览网站的时候,忽然一个莫名的链接指引着我跳转到了半次元网站 https://bcy.net/ 打开之后,发现也没有什么有意思的内容,职业的敏感让我瞬间联想到了 c ...
- Python爬虫入门教程 3-100 美空网数据爬取
美空网数据----简介 从今天开始,我们尝试用2篇博客的内容量,搞定一个网站叫做"美空网"网址为:http://www.moko.cc/, 这个网站我分析了一下,我们要爬取的图片在 ...
- Python 爬虫入门(三)—— 寻找合适的爬取策略
写爬虫之前,首先要明确爬取的数据.然后,思考从哪些地方可以获取这些数据.下面以一个实际案例来说明,怎么寻找一个好的爬虫策略.(代码仅供学习交流,切勿用作商业或其他有害行为) 1).方式一:直接爬取网站 ...
- Python爬虫入门教程: 半次元COS图爬取
半次元COS图爬取-写在前面 今天在浏览网站的时候,忽然一个莫名的链接指引着我跳转到了半次元网站 https://bcy.net/ 打开之后,发现也没有什么有意思的内容,职业的敏感让我瞬间联想到了 c ...
- 小白学 Python 爬虫(16):urllib 实战之爬取妹子图
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- Python爬虫:设置Cookie解决网站拦截并爬取蚂蚁短租
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: Eastmount PS:如有需要Python学习资料的小伙伴可以加 ...
- Python爬虫入门教程 11-100 行行网电子书多线程爬取
行行网电子书多线程爬取-写在前面 最近想找几本电子书看看,就翻啊翻,然后呢,找到了一个 叫做 周读的网站 ,网站特别好,简单清爽,书籍很多,而且打开都是百度网盘可以直接下载,更新速度也还可以,于是乎, ...
- Python爬虫入门教程 10-100 图虫网多线程爬取
图虫网-写在前面 经历了一顿噼里啪啦的操作之后,终于我把博客写到了第10篇,后面,慢慢的会涉及到更多的爬虫模块,有人问scrapy 啥时候开始用,这个我预计要在30篇以后了吧,后面的套路依旧慢节奏的, ...
随机推荐
- 远程shell脚本执行工具类
/** * 远程shell脚本执行工具类 */public class RemoteShellExecutorUtils { private static final Logger logger = ...
- 将DataRow拷贝到另一个DataRow
DataRow dr = dtPadFluid.Rows[gvPadFluid.FocusedRowHandle]; foreach (DataColumn dc in _dr.Table.Colum ...
- Edge-assisted Traffic Engineering and applications in the IoT
物联网中边缘辅助的流量工程和应用 本文为SIGCOMM 2018 Workshop (Mobile Edge Communications, MECOMM)论文. 笔者翻译了该论文.由于时间仓促,且笔 ...
- Array.from();Object.keys();Array.map()
Array.from():方法从一个类似数组或可迭代对象创建一个新的数组形式: const bar = ["a", "b", "c"]; A ...
- js操作DOM对象
js操作DOM对象 (Document Object Model)文档对象模型 nodeType返回值 1:元素节点 2:属性节点 3:文本节点 8:注释节点 9: 文档节点 nodeName 节点 ...
- netsh winsock reset命令
公司一台电脑无法浏览网页,其他基本正常,鼓捣了一个多小时,依然无法解决.. 一开始按照正常思路,感觉是dns的问题,查看了下DNS,真是自定义的,于是改成自动获取,无效 重启了网卡,无效 重启电脑,无 ...
- [Swift]LeetCode409. 最长回文串 | Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [Swift]LeetCode707. 设计链表 | Design Linked List
Design your implementation of the linked list. You can choose to use the singly linked list or the d ...
- [Swift]LeetCode899. 有序队列 | Orderly Queue
A string S of lowercase letters is given. Then, we may make any number of moves. In each move, we c ...
- 学生选课系统 c语言
/********************************* *主题:学生选修课程系统设计 * *设计要求: *1.添加功能:程序能够任意添加课程和学生记录,可提供选择界面供用户选择所需要添加 ...