如何详细解析Python urllib.request模块发送HTTP请求的步骤?

2026-05-05 08:341阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计808个文字,预计阅读时间需要4分钟。

如何详细解析Python urllib.request模块发送HTTP请求的步骤?

pythonimport urllib.request

def fetch_python_org(): request=urllib.request.Request('https://python.org') response=urllib.request.urlopen(request) return response.read().decode('utf-8')

调用函数并打印结果print(fetch_python_org())

1.Request()的参数

import urllib.request

request=urllib.request.Request('python.org')
response=urllib.request.urlopen(request)
print(response.read().decode('utf-8'))

通过构造这个数据结构,一方面可以我们可以将请求独立成一个对象,另一方面可以更加丰富和灵活地配置参数。

它的构造方法如下:

class.urllib.request.Request(url,data=None,headers={},origin_rep_host=None,unverifiable=False,method=None)

参数:

1.url必传参数

2.data,必须传bytes类型。如果是字典,先使用urllib.parse里的urlencode()

3.headers,是一个字典,请求头,直接构造或者用add_header()方法添加

4.origin_rep_host,请求方的名称或者ip地址

5.unverifiable,默认为false,表示这个请求是否无法验证。如果没有抓取的权限,此时值就是true。

如何详细解析Python urllib.request模块发送HTTP请求的步骤?

6.method,用来指示请求使用的方法。

尝试传入多个参数构建请求:

from urllib import request,parse url='localhost:5000/' p=HTTPPasswordMgrWithDefaultRealm() p.add_password(None,url,username,password)#添加用户名和密码,建立了一个处理验证的Handler auth_handler=HTTPBasicAuthHandler(p)#基本认证 opener=build_opener(auth_handler)#利用Handler构建一个Opener try: result=opener.open(url)#打开链接 html=result.read().decode('utf-8') print(html)#结果打印html源码内容 except URLError as e: print(e.reason)

代理:

添加代理,在本地搭建一个代理,运行在9743端口上。

代码:

from urllib.request import ProxyHandler,build_opener from urllib.error import URLError proxy_handler=ProxyHandler({ '127.0.0.1:9743', '127.0.0.1:9743' })#构建一个Handler opener=build_opener(proxy_handler)#构建一个Opener try: response=opener.open('www.baidu.com') print(response.read().decode('utf-8')) except URLError as e: print(e.reason)

Cookies:

将网站的Cookies获取下来:

代码:

import www.baidu.com') for item in cookie: print(item.name+"="+item.value)

运行结果:

将Cookie输出成文件格式:

代码:

import www.baidu.com')
cookie.save(ignore_discard=True,ignore_expires=True)

运行结果:

# Netscape HTTP Cookie File # curl.haxx.se/rfc/cookie_spec.html # This is a generated file! Do not edit. .baidu.com TRUE / FALSE 1638359640 BAIDUID 9BB1BA4FDD840EBD956A3D2EFB6BF883:FG=1 .baidu.com TRUE / FALSE 3754307287 BIDUPSID 9BB1BA4FDD840EBD25D00EE8183D1125 .baidu.com TRUE / FALSE H_PS_PSSID 1445_33119_33059_31660_33099_33101_26350_33199 .baidu.com TRUE / FALSE 3754307287 PSTM 1606823639 www.baidu.com FALSE / FALSE BDSVRTM 7 www.baidu.com FALSE / FALSE BD_HOME 1

LWP格式:

#LWP-Cookies-2.0 Set-Cookie3: BAIDUID="DDF5CB401A1543ED614CE42962D48099:FG=1"; path="/"; domain=".baidu.com"; path_spec; domain_dot; expires="2021-12-01 12:04:18Z"; comment=bd; version=0 Set-Cookie3: BIDUPSID=DDF5CB401A1543ED00860C3997C3282C; path="/"; domain=".baidu.com"; path_spec; domain_dot; expires="2088-12-19 15:18:25Z"; version=0 Set-Cookie3: H_PS_PSSID=1430_33058_31254_33098_33101_33199; path="/"; domain=".baidu.com"; path_spec; domain_dot; discard; version=0 Set-Cookie3: PSTM=1606824257; path="/"; domain=".baidu.com"; path_spec; domain_dot; expires="2088-12-19 15:18:25Z"; version=0 Set-Cookie3: BDSVRTM=0; path="/"; domain="www.baidu.com"; path_spec; discard; version=0 Set-Cookie3: BD_HOME=1; path="/"; domain="www.baidu.com"; path_spec; discard; version=0

以LWP格式的文件为示例,展示读取和利用的方法:

代码:

import www.baidu.com')
print(response.read().decode('utf-8'))

运行结果:输出网页源代码。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计808个文字,预计阅读时间需要4分钟。

如何详细解析Python urllib.request模块发送HTTP请求的步骤?

pythonimport urllib.request

def fetch_python_org(): request=urllib.request.Request('https://python.org') response=urllib.request.urlopen(request) return response.read().decode('utf-8')

调用函数并打印结果print(fetch_python_org())

1.Request()的参数

import urllib.request

request=urllib.request.Request('python.org')
response=urllib.request.urlopen(request)
print(response.read().decode('utf-8'))

通过构造这个数据结构,一方面可以我们可以将请求独立成一个对象,另一方面可以更加丰富和灵活地配置参数。

它的构造方法如下:

class.urllib.request.Request(url,data=None,headers={},origin_rep_host=None,unverifiable=False,method=None)

参数:

1.url必传参数

2.data,必须传bytes类型。如果是字典,先使用urllib.parse里的urlencode()

3.headers,是一个字典,请求头,直接构造或者用add_header()方法添加

4.origin_rep_host,请求方的名称或者ip地址

5.unverifiable,默认为false,表示这个请求是否无法验证。如果没有抓取的权限,此时值就是true。

如何详细解析Python urllib.request模块发送HTTP请求的步骤?

6.method,用来指示请求使用的方法。

尝试传入多个参数构建请求:

from urllib import request,parse url='localhost:5000/' p=HTTPPasswordMgrWithDefaultRealm() p.add_password(None,url,username,password)#添加用户名和密码,建立了一个处理验证的Handler auth_handler=HTTPBasicAuthHandler(p)#基本认证 opener=build_opener(auth_handler)#利用Handler构建一个Opener try: result=opener.open(url)#打开链接 html=result.read().decode('utf-8') print(html)#结果打印html源码内容 except URLError as e: print(e.reason)

代理:

添加代理,在本地搭建一个代理,运行在9743端口上。

代码:

from urllib.request import ProxyHandler,build_opener from urllib.error import URLError proxy_handler=ProxyHandler({ '127.0.0.1:9743', '127.0.0.1:9743' })#构建一个Handler opener=build_opener(proxy_handler)#构建一个Opener try: response=opener.open('www.baidu.com') print(response.read().decode('utf-8')) except URLError as e: print(e.reason)

Cookies:

将网站的Cookies获取下来:

代码:

import www.baidu.com') for item in cookie: print(item.name+"="+item.value)

运行结果:

将Cookie输出成文件格式:

代码:

import www.baidu.com')
cookie.save(ignore_discard=True,ignore_expires=True)

运行结果:

# Netscape HTTP Cookie File # curl.haxx.se/rfc/cookie_spec.html # This is a generated file! Do not edit. .baidu.com TRUE / FALSE 1638359640 BAIDUID 9BB1BA4FDD840EBD956A3D2EFB6BF883:FG=1 .baidu.com TRUE / FALSE 3754307287 BIDUPSID 9BB1BA4FDD840EBD25D00EE8183D1125 .baidu.com TRUE / FALSE H_PS_PSSID 1445_33119_33059_31660_33099_33101_26350_33199 .baidu.com TRUE / FALSE 3754307287 PSTM 1606823639 www.baidu.com FALSE / FALSE BDSVRTM 7 www.baidu.com FALSE / FALSE BD_HOME 1

LWP格式:

#LWP-Cookies-2.0 Set-Cookie3: BAIDUID="DDF5CB401A1543ED614CE42962D48099:FG=1"; path="/"; domain=".baidu.com"; path_spec; domain_dot; expires="2021-12-01 12:04:18Z"; comment=bd; version=0 Set-Cookie3: BIDUPSID=DDF5CB401A1543ED00860C3997C3282C; path="/"; domain=".baidu.com"; path_spec; domain_dot; expires="2088-12-19 15:18:25Z"; version=0 Set-Cookie3: H_PS_PSSID=1430_33058_31254_33098_33101_33199; path="/"; domain=".baidu.com"; path_spec; domain_dot; discard; version=0 Set-Cookie3: PSTM=1606824257; path="/"; domain=".baidu.com"; path_spec; domain_dot; expires="2088-12-19 15:18:25Z"; version=0 Set-Cookie3: BDSVRTM=0; path="/"; domain="www.baidu.com"; path_spec; discard; version=0 Set-Cookie3: BD_HOME=1; path="/"; domain="www.baidu.com"; path_spec; discard; version=0

以LWP格式的文件为示例,展示读取和利用的方法:

代码:

import www.baidu.com')
print(response.read().decode('utf-8'))

运行结果:输出网页源代码。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。