Linking

Capturing Life & Tech

  • 主页
  • 随笔
  • 关于我
所有文章 外链

Linking

Capturing Life & Tech

  • 主页
  • 随笔
  • 关于我

精通Python网络爬虫-第四章-Urllib库与URLError异常处理

阅读数:次 2017-07-10
字数统计: 1.5k字   |   阅读时长≈ 8分

目录

4.1 Urllib库
4.2 快速使用
4.3 模拟浏览器 – Headers 属性
4.4 超时设置
4.5 HTTP 协议请求实战
4.6 代理服务器设置
4.7 DebugLog开启
4.8 异常处理神器 - URLError

Python2.x时代,操作URL的库是Urllib,分为Urllib和Urllib2;Python3.x时代合并到Urllib库中。

4.1 Urllib库

  • 抓取网页用的
  • 升级合并后,模块中包的位置变化较多

时代变迁下的比较:

Python2.x时代 Python3.x时代
import urllib2 import urllib.request,urllib.error
import urllib import urllib2.request,urllib.error,urllib.parse
import urlparse import urllib.parse
urllib2.urlopen urllib.request.urlopen
urllib.urlencode urllib.parse.urlencode
urllib.quote urllib.request.quote
cookielib.CookieJar http.CookieJar
urllib2.Request urllib.request.Request

4.2 快速使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//导入模块
>>> import urllib.request
//打开网页
>>> file=urllib.request.urlopen("http://baidu.com")
//读取全部内容.内容赋给字符串变量
>>> data=file.read()
//读取全部内容,内容赋给列表变量
>>> data=file.readlines()
//读取一行内容
>>> dataline=file.readline()
>>> print dataline
>>> print data
>>> print(file.info())
>>> print(file.getcode())
>>> print(file.geturl())

爬取网页并保存到本地文件:

  • 爬取网页,赋给变量
  • 变量写入本地文件,*.html
  • 关闭文件
1
2
3
>>> fhandle=open("/User/linking/Dev/Python/py-books-study/deep-in-python-web-crawler/baidu.html")
>>> fhandle.write(data)
>>> fhandle.close()

URL标准中一般只允许一部分ASCII字符,如数字、字母、部分符号等。若是特殊字符,如中文、:、或者&等,需要编码。编码格式:

1
2
>>> urllib.request.quote("http://www.baidu.com")
# out: http%3A//www.baidu.com

解码格式:

1
2
>>> urllib.request.unquote("http%3A//www.baidu.com")
# out: http://www.baidu.com

编码在Python中十分重要,因为平台及历史原因等,需要特别注意。

4.3 模拟浏览器 – Headers 属性

问题:爬取时出现 403错误,由于网页做了反爬虫设置。

解决方式:设置Headers信息,模拟浏览器登录。

浏览器检查工具可以查看到User-Agents,两种方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 法1:build_opener()修改报头
import urllib.request
url="http://blog.csdn.net/weiwei_pig/article/details/51178226"
headers=("User-Agent","Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Mobile Safari/537.36")
opener = urllib.request.build_opener()
opener.addheaders = [headers]
data = opener.open(url).read()
# print(data)
fhandle=open("/Users/linking/Dev/python/py-books-study/deep-in-python-web-crawler/csdn.html","wb")
fhandle.write(data)
fhandle.close()


# 法2: 用add_header()添加报头
import urllib.request
url="http://blog.csdn.net/weiwei_pig/article/details/51178226"
req=urllib.request.Request(url)
req.add_header("User-Agent","Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Mobile Safari/537.36")
data = urllib.request.urlopen(req).read()

4.4 超时设置

超时时间设置,服务器响应时间,速度快慢,性能考量,超时抛出异常。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import urllib.request
for i in range(1,100):
try:
file = urllib.request.urlopen("http://yum.iqianyue.com",timeout=1)
data = file.read()
print(len(data))
except Exception as e:
print("出现异常 --> " + str(e))

# out
# ...
# 14165
# 14165
# 14165
# 14165
# 出现异常 --> <urlopen error timed out>
# 14165
# 出现异常 --> <urlopen error timed out>
# 14165
# 14165
# 14165
# 14165
# 14165
# ...

上述,循环发100个请求,1秒比较频繁,出现异常。

4.5 HTTP 协议请求实战

1. GET请求

1
2
3
4
5
6
7
8
9
import urllib.request
keywd="hello"
url="http://www.baidu.com/s?wd="+keywd
//构建请求Request对象
req=urllib.request.Request(url)
data=urllib.request.urlopen(req).read()
fhandle=open("/Users/linking/Dev/python/py-books-study/deep-in-python-web-crawler/searchHello.html","wb")
fhandle.write(data)
fhandle.close()

保存的网页打开后,看到的效果与百度搜索“hello”效果一样。

这是英文,如果是搜索中文呢?如keywd=”呵呵”,出现了

1
2
3
4
# out:
# UnicodeEncodeError: 'ascii' codec can't
encode characters in position 10-12: ordinal
not in range(128)

编码出现问题,需要用前面讲到的urllib.request.quote(keywd)来编码。

1
2
3
4
5
6
7
8
9
10
import urllib.request
keywd="呵呵"
url="http://www.baidu.com/s?wd="
key_code=urllib.request.quote(keywd)
url_all=url+key_code
req=urllib.request.Request(url_all)
data=urllib.request.urlopen(req).read()
fhandle=open("/Users/linking/Dev/python/py-books-study/deep-in-python-web-crawler/searchCHNword.html","wb")
fhandle.write(data)
fhandle.close()

2. POST请求实例

1
2
3
4
5
6
7
8
9
10
11
12
13
import urllib.request
import urllib.parse
url = "http://www.iqianyue.com/mypost"
postdata = urllib.parse.urlencode({
"name":"ceo@iqianyue.com",
"pass":"aA123456"
}).encode('utf-8') # 传递的数据经urlencode处理,在encode()设置为utf-8编码
req = urllib.request.Request(url,postdata)
req.add_header('User-Agent','Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Mobile Safari/537.36')
data = urllib.request.urlopen(req).read()
fhandle = open("/Users/linking/Dev/python/py-books-study/deep-in-python-web-crawler/post.html","wb")
fhandle.write(data)
fhandle.close()

登陆后得到的数据返回跟正常登陆返回一样的。

4.6 代理服务器设置

4.7 DebugLog开启

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import urllib.request
httphd=urllib.request.HTTPHandler(debuglevel=1)
httpshd=urllib.request.HTTPSHandler(debuglevel=1)
opener=urllib.request.build_opener(httphd,httpshd)
urllib.request.install_opener(opener)
data=urllib.request.urlopen("http://edu.51cto.com")

# send: b'GET / HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: edu.51cto.com\r\nUser-Agent: Python-urllib/3.6\r\nConnection: close\r\n\r\n'
# reply: 'HTTP/1.1 200 OK\r\n'
# header: Date header: Content-Type header: Transfer-Encoding header: Connection header: Set-Cookie
# header: Server header: Vary header: Vary header: Vary header: Set-Cookie header: Set-Cookie
# header: Set-Cookie header: Load-Balancing header: Load-Balancing
# Process finished with exit code 0

4.8 异常处理神器 - URLError

1
2
3
4
5
6
7
8
9
10
11
12
import urllib.request
import urllib.error
try:
file=urllib.request.urlopen("http://blog.csdn.net")
data=file.read()
# print(data)
fhandle=open("/Users/linking/Dev/python/py-books-study/deep-in-python-web-crawler/csdndownload.html","wb")
fhandle.write(data)
fhandle.close()
except urllib.error.URLError as e:
print(e.code)
print(e.reason)

没看到403,forbidden错误;怀疑是Python最新版做了模拟浏览器登陆。

还有HTTPError这个URLError的子类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import urllib.request
import urllib.error
try:
file=urllib.request.urlopen("http://blog.csdn.net")
data=file.read()
# print(data)
fhandle=open("/Users/linking/Dev/python/py-books-study/deep-in-python-web-crawler/csdndownload.html","wb")
fhandle.write(data)
fhandle.close()
except urllib.error.HTTPError as e:
print(e.code)
print(e.reason)
except urllib.error.URLError as e: # 父类兜底
print(e.code)
print(e.reason)

有则输出,没有则忽略:

1
2
3
4
5
6
7
8
9
10
11
import urllib.request
import urllib.error
try:
file=urllib.request.urlopen("http://www.baiduddd.com")
except urllib.error.URLError as e: # 父类兜底
if hasattr(e,"code"):
print(e.code)
if hasattr(e,"reason"):
print(e.reason)

# 输出: [Errno 8] nodename nor servname provided, or not known
  • 本文作者: Linking
  • 本文链接: https://linking.fun/2017/07/10/精通Python网络爬虫-第四章-Urllib库与URLError异常处理/
  • 版权声明: 版权所有,转载请注明出处!
  • Python
  • crawler
  • Python

扫一扫,分享到微信

如何看待加班文化
付费阅读时代来了吗
  1. 1. 目录
  2. 2. 4.1 Urllib库
  3. 3. 4.2 快速使用
  4. 4. 4.3 模拟浏览器 – Headers 属性
  5. 5. 4.4 超时设置
  6. 6. 4.5 HTTP 协议请求实战
    1. 6.1. 1. GET请求
    2. 6.2. 2. POST请求实例
  7. 7. 4.6 代理服务器设置
  8. 8. 4.7 DebugLog开启
  9. 9. 4.8 异常处理神器 - URLError
© 2015-2026 Linking
GitHub:hexo-theme-yilia-plus by Litten
本站总访问量次 | 本站访客数人
  • 所有文章
  • 外链

tag:

  • weather
  • 需求
  • essay
  • basketball
  • olympic
  • nginx
  • APPScan
  • SQl盲注
  • xss
  • Ajax
  • ajax
  • ai
  • agent
  • openclaw
  • ccf
  • Nginx
  • HTML5
  • html5
  • hmtl5
  • sse
  • JavaScriptCore
  • Oracle
  • operation
  • Linux
  • deploy
  • Mac Office
  • markdown
  • ListView
  • GridView
  • MySQL
  • 慢查询
  • mongodb
  • 转置
  • thought
  • network
  • ubuntu
  • NetworkManager
  • RFKill
  • Netplan
  • avatar
  • cocoa
  • blog
  • Gitalk
  • container
  • macvlan
  • docker
  • oracle
  • cookie
  • patch
  • gitea
  • git
  • iOS
  • https
  • 多线程
  • bundle
  • 兼容性
  • HTTP
  • 绘图
  • cs
  • java
  • 效率
  • 快捷键
  • route
  • nodejs
  • pip
  • arcgis
  • arcgis 建模
  • 标识
  • redis
  • read
  • bookList
  • running
  • showdoc
  • disk
  • unit-test
  • D.Wade
  • thoughts
  • duoduo
  • Python
  • python
  • tomcat
  • 读书节
  • session
  • jdk
  • war
  • 加班
  • Android onclick事件监听
  • 正则
  • 手机品牌匹配
  • ntp
  • OpenLayers
  • Geoserver
  • wechat
  • 微信公众号
  • 爬虫
  • WeChat
  • 张靓颖
  • 动漫
  • vpn
  • PPT
  • MarkDown
  • plan
  • 朱赟
  • 极客时间专栏
  • 极客邦
  • 模块化
  • MVC
  • excel
  • NBA
  • kobe
  • team
  • crawler
  • 进度条
  • ssl
  • book
  • anti-stealing-link
  • Agentic Engineering
  • Vibe Coding
  • Software 3.0
  • Andrej Karpathy
  • LLM
  • Programming

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia-plus根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • GitHub Trending
  • OpenAI ChatGPT
  • Gitee码云
  • 简书
  • CSDN