《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 ...
随机推荐
- java中强,软,弱,虚引用 以及WeakHahMap
java中强,软,弱,虚引用 以及WeakHahMap 一:强软引用: 参考:http://zhangjunhd.blog.51cto.com/113473/53092/进行分析 packa ...
- php短信接口代码
这篇文章主要为大家详细介绍了php短信接口代码,php短信发送.php批量发送.php获取余额等代码,感兴趣的小伙伴们可以参考一下 本文实例为大家分享了几个常用的php短信接口代码,供大家参考,具体内 ...
- thinkPHP中_initialize方法实例分析
子类的_initialize方法自动调用父类的_initialize方法. 而php的构造函数construct,如果要调用父类的方法,必须在子类构造函数显示调用parent::__construct ...
- 更改dede网站地图模板样式
dedecms后台可以生成2个地图,一个是网站地图,html格式的,一个是rss地图,同样默认这2个地图生成之后也会有底部的dedecms版权声明,这个时候我们需要分别更改这2个模板才可以去掉底部的版 ...
- dede 你所上传的软件类型不在许可列表,请更改系统对扩展名限定的配置
后台,系统,系统基本参数,附件设置格式
- ios知识点总结——UITableView的展开与收缩及横向Table
UITableVIew是iOS开发中使用最为广泛的一种控件,对于UITableView的基本用法本文不做探讨,本文主要是针对UITableView的展开与收缩进行阐述,在文章的后面也会探讨一下横向ta ...
- NSDate的常用用法
1. 创建或初始化可用以下方法 用于创建NSDate实例的类方法有 + (id)date; 返回当前时间 + (id)dateWithTimeIntervalSinceNow:(NSTimeInter ...
- MySql Outer Join 简单化
查询from语句中的Outer Join可以在多种情况下被简化: 在解析阶段,右外连接操作可以被转变为等下ode值包含left join的操作,在一般情况下,转变: (T1, ...) RIGHT J ...
- shopnc IM配置过程
im配置windows下,修改chat下和data下的config,安装node,覆盖node下文件即可
- Xcode 单元测试
项目创建后自动创建的单元测试文件夹, 文件夹名为项目名+tests, xcode6后貌似没有选择是否添加测试target的选择了. 默认生成的如下图 提供的断言如下 typedefNS_ENUM(NS ...