《Web Scraping With Python》Chapter 1的学习笔记
urllib
urllib是python library自带的库,可以直接用。
urlopen
from urllib.request import urlopen
html = urlopen("http://pythonscraping.com/pages/page1.html")
read= html.read()
print(type(html))
print(type(read))
运行结果为:
<class 'http.client.HTTPResponse'>
<class 'bytes'>
urlopen(url)打开一个网页并读取相关内容,html.read()返回的是网页的html code。
关于urllib和urllib2
urllib or urllib2?
If you’ve used the urllib2 library in Python 2.x, you might have
noticed that things have changed somewhat between urllib2 and
urllib. In Python 3.x, urllib2 was renamed urllib and was split into
several submodules: urllib.request, urllib.parse, and url
lib.error. Although function names mostly remain the same, you
might want to note which functions have moved to submodules
when using the new urllib.
BeautifulSoup
“Beautiful Soup, so rich and green,
Waiting in a hot tureen!
Who for such dainties would not stoop?
Soup of the evening, beautiful Soup!”
很有趣的一个名字,BeautifulSoup取名于爱丽丝梦游仙境的同名诗歌。
在windows上为python3安装BeautifulSoup十分简单,不再赘述。
BeautifulSoup其实就是一个类,这个类会把html的内容组织成一个特定的结构,如:
html → ......
— head →
— body →An Int...
Lorem ip...— h1 →
An Interesting Title
— div →
Lorem Ipsum dolor...
下面看一段代码:
from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://www.pythonscraping.com/pages/page1.html")
bsObj = BeautifulSoup(html.read())
print(bsObj.h1)
结合上文,它的输出结果应该是:
<h1>An Interesting Title</h1>
ExceptionHandling
先简单理解一下URL的组成。
protocol://serverIP/path
我们通常用的是http协议,所以大多数网站都是以http开头的,接下来的serverIP就是主机或者叫服务器的ip地址,path是具体路径,可以省略。
HTTPError
The page is not found on the server (or there was some error in retrieving it)
简单的来说,当一个特定服务器没有你想要的相关网页时(也可以理解为服务器没有你要的那个文件),HTTPError就会被抛出。
try:
html = urlopen("http://www.pythonscraping.com/pages/page1.html")
except HTTPError as e:
print(e)
#return null, break, or do some other "Plan B"
else:
#program continues. Note: If you return or break in the
#exception catch, you do not need to use the "else" statement
URLError
The server is not found
简单的来说,如果服务器宕机了或者是根本就没有这个服务器,那么URLError就会被抛出。
书上其实没有提到URLError,它说的是如果找不到服务器,那么html会是一个None值,接着用if语句判断html是否为None,当我在运行书上的程序时发现如果找不到服务器,直接就会抛出
URLError,if语句是不会执行的。
AttributeError
如果我们在网页找不到具体的tag时,AttributeError就会被抛出。
例如print(bsObj.nonExistentTag.someTag)中的someTag不存在,此时会抛出AttributeError。
《Web Scraping With Python》Chapter 1的学习笔记的更多相关文章
- <Web Scraping with Python>:Chapter 1 & 2
<Web Scraping with Python> Chapter 1 & 2: Your First Web Scraper & Advanced HTML Parsi ...
- Web Scraping with Python读书笔记及思考
Web Scraping with Python读书笔记 标签(空格分隔): web scraping ,python 做数据抓取一定一定要明确:抓取\解析数据不是目的,目的是对数据的利用 一般的数据 ...
- Web scraping with Python (part II) « Jean, aka Sig(gg)
Web scraping with Python (part II) « Jean, aka Sig(gg) Web scraping with Python (part II)
- 阅读OReilly.Web.Scraping.with.Python.2015.6笔记---Crawl
阅读OReilly.Web.Scraping.with.Python.2015.6笔记---Crawl 1.函数调用它自身,这样就形成了一个循环,一环套一环: from urllib.request ...
- 阅读OReilly.Web.Scraping.with.Python.2015.6笔记---找出网页中所有的href
阅读OReilly.Web.Scraping.with.Python.2015.6笔记---找出网页中所有的href 1.查找以<a>开头的所有文本,然后判断href是否在<a> ...
- 阅读OReilly.Web.Scraping.with.Python.2015.6笔记---BeautifulSoup---findAll
阅读OReilly.Web.Scraping.with.Python.2015.6笔记---BeautifulSoup---findAll 1..BeautifulSoup库的使用 Beautiful ...
- 首部讲Python爬虫电子书 Web Scraping with Python
首部python爬虫的电子书2015.6pdf<web scraping with python> http://pan.baidu.com/s/1jGL625g 可直接下载 waterm ...
- $《利用Python进行数据分析》学习笔记系列——IPython
本文主要介绍IPython这样一个交互工具的基本用法. 1. 简介 IPython是<利用Python进行数据分析>一书中主要用到的Python开发环境,简单来说是对原生python交互环 ...
- 《Web Scraping With Python》Chapter 2的学习笔记
You Don't Always Need a Hammer When Michelangelo was asked how he could sculpt a work of art as mast ...
- Web Scraping with Python
Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门 https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6E ...
随机推荐
- 数据库01创建表和DML语言
楼主用的数据库时mysql,用的时navacat for mysql. 数据库层面: 1.显示所有数据库 show databases; 2.创建数据库,名字叫lyh,编码为utf-8 create ...
- linux服务器,svn认证失败,配置问题,防火墙等等
之前自己还真没设置过SVN,今天亲自动手,错误百出,真是够头疼的.在网上随便找了一篇文章,就按照文章介绍开始安装.怎么安装和设置我就不说了,这里主要记录遇到的问题. 1.不知道该怎么设置 svn:// ...
- dede 内容页文章标题显示不全的更改方法
找到include/taglib/arclist.lib.php 1.$titlelen = AttDef($titlelen,30);换成$titlelen = AttDef($titlelen,2 ...
- Mac OS启动服务优化高级篇
一.Mac下的启动服务主要三个可配置的地方 1.系统偏好设置->帐户->登陆项 2./System/Library/StartupItems 和 /Library/StartupItems ...
- 1.MAVEN项目的创建与问题的解决
一.创建一个maven-webapp.(环境:mac和15版本的IDEA) 二.next--->填写groupId(公司单位的名字,你组织的名字)和ArtifactID(有关tomcat,以后用 ...
- shopnc验证码显示不了
data/config文件编码问题,要utf-8无bom
- CDN页面刷新接口定义[高升]
一 . 任务 分发 工作流程步骤 1. 合作方按照高升定义的 json 数据格式向高升分发接口 post 任务,高升分发接口会根据接收情况即时反馈接收成功还是失败的结果.二 . 高升 分发 接口 定义 ...
- springboot之fastjson
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifac ...
- python_如何在循环引用中管理内存?
案例: python中通过引用计数来回收垃圾对象,在某些环形数据结构(树,图--),存在对象间的循环引用,比如树的父节点引用子节点,子节点同时引用父节点,此时通过del掉引用父子节点,两个对象不能被立 ...
- JS的for循环小例子
1.输出1-100的和 var sum = 0; for(var i=1;i<=100;i++){ sum = sum + i; } document.write(sum); 2.输出1-100 ...