脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python urllib.request模块的使用详解

python urllib.request模块的使用详解

2021-09-24 00:26可爱的黑精灵 Python

这篇文章主要介绍了python urllib.request模块的使用详解,帮助大家更好的理解和学习使用python,感兴趣的朋友可以了解下

python的urllib模块提供了一系列操作url的功能,可以让我们通过url打开任意资源。其中比较常用的就是request模块,本篇主要介绍requset模块。

urllib子模块

  • urllib.request 打开或请求url
  • urllib.error 捕获处理请求时产生的异常
  • urllib.parse 解析url
  • urllib.robotparser 用于解析robots.txt文件

robots.txt是一种存放于网站根目录下文本文件,用来告诉网络爬虫服务器上的那些文件可以被查看。又被成为robots协议,是一种约定俗成的协议。

request模块

function request.urlopen()

urlopen方法用来打开资源url,常用带参数形式urlopen(url,data=none),url:资源url,data:携带的数据。

方法的返回值始终为一个对象,并可以调用相应的方法获取返回的信息。其中对于http及https的url来说会返回一个http.client.httpresponse对象;

?
1
2
3
4
5
6
7
import urllib.request
# 我们用本地的一个简单html文件来测试
url = 'http://127.0.0.1:8848/chenjy/test.html'
 
req = urllib.request.urlopen(url)
 
print(req)

python urllib.request模块的使用详解

1. read() 返回服务器返回的原始数据;

?
1
2
3
4
5
6
7
import urllib.request
 
url ='http://127.0.0.1:8848/chenjy/test.html'
 
req = urllib.request.urlopen(url)
 
print(req.read())

python urllib.request模块的使用详解

我们可以再调用decode()方法来解码。

?
1
2
3
4
5
6
7
import urllib.request
 
url = 'http://127.0.0.1:8848/chenjy/test.html'
 
req = urllib.request.urlopen(url)
 
print(req.read().decode())

python urllib.request模块的使用详解

2.geturl() 返回获取资源的url;

  • 创建一个测试页
?
1
2
3
4
5
6
import urllib.request
url = 'http://127.0.0.1:8848/chenjy/test.html'
 
req = urllib.request.urlopen(url)
 
print(req.geturl())

python urllib.request模块的使用详解

  • 前端重定向

我们在页面中添加js脚本重定向页面window.location.href='http://127.0.0.1:8848/chenjy/test2.html';,会发现访问的时候会重定向到test2,但是geturl还是获取的重定向前的

python urllib.request模块的使用详解

  • 后端重定向

我们启动一个项目并添加一个拦截器当访问index.html的时候重定向到/ls/html/list.html页面,geturl获取的是重定向后的页面

?
1
2
3
4
5
6
7
8
@override
        public void handle(string target, httpservletrequest request, httpservletresponse response, boolean[] ishandled) {
          int index = target.lastindexof("index.html");
          if (index != -1){
                handlerkit.redirect("/ls/html/list.html",request,response,ishandled);
          }
          
        }
?
1
2
3
4
5
6
import urllib.request
url = 'http://localhost:8088/ls/index.html'
 
req = urllib.request.urlopen(url)
 
print(req.geturl())

python urllib.request模块的使用详解

3.info() 返回页面的元信息;

?
1
2
3
4
5
6
import urllib.request
url = 'http://127.0.0.1:8848/chenjy/test.html'
 
req = urllib.request.urlopen(url)
 
print(req.info())

python urllib.request模块的使用详解

4.getcode() 返回页面的状态码;

?
1
2
3
4
5
6
import urllib.request
url = 'http://127.0.0.1:8848/chenjy/test.html'
 
req = urllib.request.urlopen(url)
 
print(req.getcode())

python urllib.request模块的使用详解

class request.request

url请求类 request(url, data=none, headers={}, origin_req_host=none, unverifiable=false, method=none)

  • url:请求url
  • data:请求传参;bytes字节流
  • headers:请求头
  • origin_req_host:请求原始主机;不带端口
  • unverifiable:是否不可验证;
  • method :请求方法;如get、post、put等
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import urllib.request
 
# 模拟iphone5请求百度手机版页面
url = 'https://www.baidu.com/'
 
user_agent = 'mozilla/5.0 (iphone; cpu iphone os 10_3_1 like mac os x) applewebkit/603.1.30 (khtml, like gecko) version/10.0 mobile/14e304 safari/602.1'
headers = {
  'user-agent': user_agent
}
 
# 抓取page信息
req = urllib.request.request(url, headers=headers,method='get')
page = urllib.request.urlopen(req).read().decode('utf-8')
 
print(page)

python urllib.request模块的使用详解

以上就是python urllib.request模块的使用详解的详细内容,更多关于python urllib.request模块的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/chenjy1225/p/11743847.html

延伸 · 阅读

精彩推荐