《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 ...
随机推荐
- ionic2 安装与cordova打包
1.安装: cnpm install -g cordova ionic ionic start name cd name cnpm install 2.环境配置: http://www.cnblo ...
- 在Sql Server Intergration Service中设置Catalog下所部署所有项目的参数值
在Sql Server 2012开始,微软给SSIS添加了Project Model这种新的项目类型,与之对应的是在Sql Server数据库引擎中引入了Intergration Services C ...
- Spark算子--SortByKey
转载请标明出处http://www.cnblogs.com/haozhengfei/p/076a31e7caab1316b07990c02ac65e9c.html SortByKey--Transf ...
- Oracle_复杂查询综合
Oracle_复杂查询综合 -- 1.列出所有员工的年工资,按年薪从低到高排序. select,) income from emp order by income; -- 2.列出薪金比" ...
- 【编程技巧】java不使用第三个变量处理两个变量的交换
public class SwapNum {public static void main(String[] args) {doSwapNum();}public static void doSwap ...
- IOS学习笔记27—使用GDataXML解析XML文档
http://blog.csdn.net/ryantang03/article/details/7868246
- python基础7之python3的内置函数
官方介绍: python3:https://docs.python.org/3/library/functions.html?highlight=built#ascii python2:https:/ ...
- postgres的initdb解析——从一次插件升级失败说起
我们公司基于postgres开发了一款数据库产品,不用说我们对OSS的源码做了改动,并且也集成和自己编写了一些插件.因此,当postgresql和相关插件升级时,我们也需要将升级反应到自己的产品中去, ...
- assembly 基础
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- CSS深入理解学习笔记之overflow
1.Overflow基本属性 overflow:visible(默认)/hidden/scroll/auto/inherit; visible:超出部分可见. hidden:超出部分隐藏. scrol ...