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的学习笔记的更多相关文章

  1. <Web Scraping with Python>:Chapter 1 & 2

    <Web Scraping with Python> Chapter 1 & 2: Your First Web Scraper & Advanced HTML Parsi ...

  2. Web Scraping with Python读书笔记及思考

    Web Scraping with Python读书笔记 标签(空格分隔): web scraping ,python 做数据抓取一定一定要明确:抓取\解析数据不是目的,目的是对数据的利用 一般的数据 ...

  3. 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)

  4. 阅读OReilly.Web.Scraping.with.Python.2015.6笔记---Crawl

    阅读OReilly.Web.Scraping.with.Python.2015.6笔记---Crawl 1.函数调用它自身,这样就形成了一个循环,一环套一环: from urllib.request ...

  5. 阅读OReilly.Web.Scraping.with.Python.2015.6笔记---找出网页中所有的href

    阅读OReilly.Web.Scraping.with.Python.2015.6笔记---找出网页中所有的href 1.查找以<a>开头的所有文本,然后判断href是否在<a> ...

  6. 阅读OReilly.Web.Scraping.with.Python.2015.6笔记---BeautifulSoup---findAll

    阅读OReilly.Web.Scraping.with.Python.2015.6笔记---BeautifulSoup---findAll 1..BeautifulSoup库的使用 Beautiful ...

  7. 首部讲Python爬虫电子书 Web Scraping with Python

    首部python爬虫的电子书2015.6pdf<web scraping with python> http://pan.baidu.com/s/1jGL625g 可直接下载 waterm ...

  8. $《利用Python进行数据分析》学习笔记系列——IPython

    本文主要介绍IPython这样一个交互工具的基本用法. 1. 简介 IPython是<利用Python进行数据分析>一书中主要用到的Python开发环境,简单来说是对原生python交互环 ...

  9. 《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 ...

  10. Web Scraping with Python

    Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门 https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6E ...

随机推荐

  1. 在echarts3中使用字符云

    echarts2的官方API里是带有字符云的,但到了echarts3就被从官网上移除了,想要使用的话可以从github上下载: 下载地址:https://github.com/ecomfe/echar ...

  2. 在vue-cli项目中使用echarts

    这个示例使用 vue-cli 脚手架搭建 安装echarts依赖 npm install echarts -S 或者使用国内的淘宝镜像: 安装 npm install -g cnpm --regist ...

  3. [国嵌笔记][014][Mini2440安装Linux]

    引导安装步骤 相关介绍: 开发板软件构成:Bootloader.嵌入式操作系统.嵌入式文件系统 JTAG接口常用于对flash等器件进行编程,常见的JTAG下载线有并口和USB接口两种 硬件连接: 1 ...

  4. Linux怎么查看软件安装路径 查看mysql安装在哪

    https://jingyan.baidu.com/article/86112f1378bf282737978730.html Linux系统一般都是命令行界面,对于安装的软件也是通过命令安装的.对于 ...

  5. html input验证只能输入数字,不能输入其他

    html input验证只能输入数字,不能输入其他 此方法为借鉴别人的,在此只做记录. <input type="text" onkeyup="if(!/^\d+$ ...

  6. Spring-事务配置和详解

    一.Spring事务配置 在项目开发过程中经常会使用事务来确保数据的一致性.根据网上的资料整理一下在spring中配置事务的几种方式.无论是哪种方式都需要在配置文件中配置连接池和事务管理器,代码如下. ...

  7. selenium-java web自动化测试工具

    本篇文章由来,这两天整理了下自己经常使用而且很熟练的项目,今天突然想起漏了一个,补上了,但想到还没对应的博客,那就写一个简单的 我经常使用且相对熟练的部分技术如下(不知道算不算各位大神眼中的辣鸡): ...

  8. Storm容错和高可用

    Daemon Fault Tolerance Storm有一些不同的守护进程 Nimbus负责调度workers supervisors负责运行和杀死workers log views负责访问日志 U ...

  9. Android Studio 下获取debug sha1和md5

    Open Android Studio Open Your Project Click on Gradle (From Right Side Panel, you will see Gradle Ba ...

  10. C# 值类型,引用类型区别

    值类型/引用类型 作为所有类型的基类,System.Object提供了一组方法,这些方法在所有类型中都能找到,其中包含toString方法及clone等方法. 引用类型和值类型都继承自System.O ...