python网络数据采集——初见网络爬虫
2019-04-21 12:19
381 查看
一、一个简单爬虫:
from urllib import request html = request.urlopen("http://baidu.com") print(html.read())
二、BeautifulSoup
- 简单示例:
from urllib import request from bs4 import BeautifulSoup html = request.urlopen("https://blog.csdn.net/qq_34908167/article/details/78849590") bs = BeautifulSoup(html.read(),"html.parser") print(bs.a)
三、返回异常:
网络是复杂的,每个网站的格式也有所不同,网络数据采集会发生各种各样的情况,为了避免做无用功,我们需要在程序中添加返回异常的代码,以便及时观察到代码运行状况。
一般会返回三种异常:
1.网页不存在,或获取页面时发生错误
2.服务器不存在
3.服务器成功获取,但程序的需求标签错误。
- 发生异常是,可以使用如下方法处理:
from urllib.request import urlopen from urllib.error import HTTPError from urllib.error import URLError try: html = urlopen("https://pythonscrapingthisurldoesnotexist.com") except HTTPError as e: print("The server returned an HTTP error") except URLError as e: print("The server could not be found!") else: print(html.read())
四、最终版代码;
from urllib.request import urlopen from urllib.error import HTTPError from bs4 import BeautifulSoup def getTitle(url): try: html = urlopen(url) except HTTPError as e: return None try: bsObj = BeautifulSoup(html.read(), "lxml") title = bsObj.body.h1 except AttributeError as e: return None return title title = getTitle("http://www.pythonscraping.com/pages/page1.html") if title == None: print("Title could not be found") else: print(title)
相关文章推荐
- python—网络数据采集------初见网络爬虫
- Python网络数据采集1:初见网络爬虫
- Python网络数据采集一书中的错误
- python网络数据采集-如何实现文件上传
- Python网络数据采集 中文版
- python网络数据采集-处理重定向问题
- Python网络数据采集学习笔记1
- 初识python爬虫 Python网络数据采集1.0 BeautifulSoup通过网站css爬取信息
- O'Reilly精品图书推荐:Python网络数据采集
- python网络数据采集学习笔记:第二章
- Python网络数据采集4(译者:哈雷)
- python网络数据采集
- Python网络数据采集5:存储数据
- [Python]网络数据采集概述(1)—页面访问及页面元素的解析
- [Python]网络数据采集概述(2)—存储数据及读取文档
- python网络数据采集-处理格式规范的文字
- Python网络数据采集Urllib库的基本使用
- 『python学习』python 网络数据采集笔记
- Python网络数据采集笔记第二章
- python网络爬虫系列教程——python网络数据爬虫误区,让你的爬虫更像人类