python网络爬虫开发:技术基础之urllib模块+requests模块

2022-08-0710:30:49云计算与物联网Comments1,089 views字数 1854阅读模式

使用爬虫的时候离不开URL地址和下载页面,首先我们就来了解一下URL。它的语法格式一般为:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26697.html

1
protocol :// hostname[:port] / path / [;parameters][?query]#fragment

URL由三部分组成,第一部分是协议,有http、https、ftp等,第二部分存放资源的服务器的域名或IP地址,第三部分为资源的具体地址。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26697.html

我们在进行网络请求的时候通常采用三种方式:urllib、urllib3和requests,下面我们就来介绍一下urllib和requests。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26697.html

1. urllib模块文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26697.html

urllib是Python系统库中存在的一个模块,它提供了多个子模块:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26697.html

1) urllib.request文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26697.html

提供打开和阅读URL的方法和类文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26697.html

2) urllib.error文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26697.html

包含异常类文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26697.html

3) urllib.parse文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26697.html

解析和引用URL文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26697.html

4) urllib.robotparser文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26697.html

解析robots.txt文件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26697.html

我们使用最多的是第一个子模块中的方法,其中包含了对服务器请求的发出、跳转、代理等。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26697.html

当我们向网页发送请求的时候,采用urllib.request.urlopen()方法。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26697.html

例如:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26697.html

1
2
3
4
import urllib.request#引入模块
response = urllib.request.urlopen('http://www.baidu.com/')
html = response.read().decode('utf-8')#以utf-8格式读取网页的内容
print(html)#输出内容

输出结果为网页中的内容(html格式):文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26697.html

python网络爬虫开发:技术基础之urllib模块+requests模块文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26697.html

它还包含data参数,data参数主要是字节流编码格式的内容,即bytes类型,通过bytes()方法来进行转换。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26697.html

1
2
3
4
5
import urllib.parse
import urllib.request
data = bytes(urllib.parse.urlencode({'word''hello'}), encoding='utf8')
response = urllib.request.urlopen('http://httpbin.org/post', data=data)
print(response.read())

这个例子传递了一个参数word,它的值为hello,它会被转换为bytes类型,第二个参数为编码格式,我们常选择utf8。data参数有很多功能,这一点在后续会慢慢了解到。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26697.html

还可以通过timeout参数来设置超时时间,它是直接加在urlopen括号中其余不常用参数就不作过多介绍,有兴趣的可以在Python官方文档中进行查阅。https://docs.python.org/3/library/urllib.request.html文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26697.html

2. requests模块文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26697.html

requests是一种第三方模块,主要用于发送请求,它在使用的时候比urllib模块要简洁方便很多,我们可以在命令操作符里通过pip install requests来安装,也可以在Pycharm中直接进行安装。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26697.html

python网络爬虫开发:技术基础之urllib模块+requests模块文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26697.html

我们可以通过requests模块的get方法打印多种请求信息,代码如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26697.html

1
2
3
4
5
6
7
import requests
= requests.get('https://www.douban.com/')
print(r.status_code)#输出状态码
print(r.encoding)#输出编码格式
print(r.headers)#输出头部文件
print(r.cookies)#输出cookie信息
print(r.content)#输出字节流形式网页源码

输出结果如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26697.html

1
2
418None{'Date''Wed, 19 Feb 2020 10:10:22 GMT''Content-Length''0''Connection''keep-alive',
 'Keep-Alive''timeout=30''Server''dae'}<RequestsCookieJar[]>b''

requests模块也可以使用post、put、delete、hand等来发送请求文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26697.html

3. 总结文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26697.html

本节我们主要介绍了Python发送网络请求的方式,在第一节介绍爬虫流程的时候提到过,发送请求是爬虫操作的关键步骤,但是我们在使用的时候会发现,无论是如何发送请求都会被服务器拒绝,因此我们需要请求headers处理,下一节我们来学习请求headers处理。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunda/26697.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/yunda/26697.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定