《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 ...
随机推荐
- vue实现侧边栏手风琴效果
模板 代码如下 html <template> <div class="header"> <ul> <!-- 循环数据在点击调用chang ...
- UE4 保存为bitmap
TArray<FColor> colorData; colorData.Init(FColor(0, 0, 255, 255), 1920 * 1080); for (int ...
- 【JavaScript_轮播图】
今天给大家带来的是我自己做的一个轮播图效果,让我们一起来学习一下吧. 这是我的页面所有代码: <!DOCTYPE html> <html> <head> <m ...
- PHP操作MySQL对表增加一列(一个字段)
2014-03-19 16:59 1471人阅读 评论(0) 收藏 举报 分类: MySQL(12) 对于已经建立好的数据库,在一个已经有字段的表内新加字段可用以下方法: mysql_query(&q ...
- console.log 用法
转自http://www.cnblogs.com/ctriphire/p/4116207.html 大家都有用过各种类型的浏览器,每种浏览器都有自己的特色,本人拙见,在我用过的浏览器当中,我是最喜欢C ...
- Navicat如何进行搜索筛选
分类: Navicat Navicat提供的"在数据库或模式中查找"功能用于一个数据库和/或模式内搜索表和视图的记录.Navicat"对象筛选"功能可以让用户在 ...
- 第一章 用HTML5中的结构元素构建网站
1.当一个容器需要直接定义样式或通过脚本定义行为时,推荐使用div元素而非section元素. 2.section是需要标题的,而nav或aside没有标题也是可以的. 3.html5轮廓工具 htt ...
- 【Android】版本的名称
http://www.cnblogs.com/imlucky/archive/2011/10/21/2220596.html
- Javascript学习--时间
digit = [ [ [0,0,1,1,1,0,0], [0,1,1,0,1,1,0], [1,1,0,0,0,1,1], [1,1,0,0,0,1,1], [1,1,0,0,0,1,1], [1, ...
- 007-declare 声明变量的类型
declare [+/-] [选项] 变量名 - 给变量设定类型 + 取消变量的类型 -a 将变量声明为数组型 -i 将变量声明为整形 -x 将变量声明成环境变量 -r 将变量声明为只读变量 -p 显 ...