温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在python项目中使用urllib.request模块

发布时间:2021-03-17 15:42:22 来源:亿速云 阅读:193 作者:Leah 栏目:开发技术

今天就跟大家聊聊有关如何在python项目中使用urllib.request模块,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

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对象;

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() 返回服务器返回的原始数据;

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()方法来解码。

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;

  • 创建一个测试页

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获取的是重定向后的页面

@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);
  	  	  }
  	      
  	    }
import urllib.request
url = 'http://localhost:8088/ls/index.html'

req = urllib.request.urlopen(url)

print(req.geturl())

如何在python项目中使用urllib.request模块

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

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() 返回页面的状态码;

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等

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模块有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI