如何通过urllib库高效操作Python中的URL?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1231个文字,预计阅读时间需要5分钟。
一、操作URL+urllib提供了一系列用于操作URL的功能。分类讲解相关内容。
二、Get()函数+urllib的request模块可以非常方便地抓取URL内容,也是发送一个GET请求到指定页面,然后返回HTTP响应。
urllib提供了一系列用于操作URL的功能。分类讲解相关内容。
urllib的request模块可以非常方便地抓取URL内容,也就是发送一个GET请求到指定的页面,然后返回HTTP的响应:
例如,对豆瓣的URLapi.growingio.com/v2/22c937bbd8ebd703f2d8e9445f7dfd03/web/pv?stm=1593747087078进行抓取,并返回响应:
from urllib import request with request.urlopen('api.growingio.com/v2/22c937bbd8ebd703f2d8e9445f7dfd03/web/pv?stm=1593747087078') as f: data = f.read() print('Status:', f.status, f.reason) for k, v in f.getheaders(): print('%s: %s' % (k, v)) print('Data:', data.decode('utf-8'))登录后复制
可以看到HTTP响应的头和JSON数据:
如果要想模拟浏览器发送GET请求,就需要使用Request对象,通过往Request对象添加HTTP头,就可以把请求伪装成浏览器。例如,模拟iPhone 6去请求豆瓣首页:
from urllib import request req = request.Request('www.douban.com/') req.add_header('User-Agent', 'Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/8.0 Mobile/10A5376e Safari/8536.25') with request.urlopen(req) as f: print('Status:', f.status, f.reason) for k, v in f.getheaders(): print('%s: %s' % (k, v)) print('Data:', f.read().decode('utf-8'))登录后复制
这样豆瓣会返回适合iPhone的移动版网页:
如果要以POST发送一个请求,只需要把参数data以bytes形式传入。
模拟一个微博登录,先读取登录的邮箱和口令,然后按照weibo.cn的登录页的格式以username=xxx&password=xxx的编码传入:
from urllib import request, parse print('Login to weibo.cn...') #电子邮件 email = input('Email: ') #密码 passwd = input('Password: ') #相关的参数 login_data = parse.urlencode([ ('username', email), ('password', passwd), ('entry', 'mweibo'), ('client_id', ''), ('savestate', '1'), ('ec', ''), ('pagerefer', 'passport.weibo.cn/signin/welcome?entry=mweibo&r=passport.weibo.cn/sso/login') req.add_header('Origin', 'passport.weibo.cn') #构造User-Agent req.add_header('User-Agent', 'Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/8.0 Mobile/10A5376e Safari/8536.25') req.add_header('Referer', 'passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=www.baidu.com/") # 1. 如果这么写,只有使用opener.open()方法发送请求才使用自定义的代理,而urlopen()则不使用自定义代理。 response = opener.open(request) # 2. 如果这么写,就是将opener应用到全局,之后所有的,不管是opener.open()还是urlopen() 发送请求,都将使用自定义代理。 # urllib.request.install_opener(opener) # response = urllib.request.urlopen(request) # 获取服务器响应内容 html = response.read().decode("utf-8") # 打印结果 print(html)登录后复制
如果代理成功返回网址的信息。
如果网址出错或者代理地址有误,返回下面界面。
使用Python语言,能够帮助大家更好的学习Python。urllib提供的功能就是利用程序去执行各种HTTP请求。如果要模拟浏览器完成特定功能,需要把请求伪装成浏览器。伪装的方法是先监控浏j览器发出的请求,再根据浏览器的请求头来伪装,User-Agent头就是用来标识浏览器的。
本文共计1231个文字,预计阅读时间需要5分钟。
一、操作URL+urllib提供了一系列用于操作URL的功能。分类讲解相关内容。
二、Get()函数+urllib的request模块可以非常方便地抓取URL内容,也是发送一个GET请求到指定页面,然后返回HTTP响应。
urllib提供了一系列用于操作URL的功能。分类讲解相关内容。
urllib的request模块可以非常方便地抓取URL内容,也就是发送一个GET请求到指定的页面,然后返回HTTP的响应:
例如,对豆瓣的URLapi.growingio.com/v2/22c937bbd8ebd703f2d8e9445f7dfd03/web/pv?stm=1593747087078进行抓取,并返回响应:
from urllib import request with request.urlopen('api.growingio.com/v2/22c937bbd8ebd703f2d8e9445f7dfd03/web/pv?stm=1593747087078') as f: data = f.read() print('Status:', f.status, f.reason) for k, v in f.getheaders(): print('%s: %s' % (k, v)) print('Data:', data.decode('utf-8'))登录后复制
可以看到HTTP响应的头和JSON数据:
如果要想模拟浏览器发送GET请求,就需要使用Request对象,通过往Request对象添加HTTP头,就可以把请求伪装成浏览器。例如,模拟iPhone 6去请求豆瓣首页:
from urllib import request req = request.Request('www.douban.com/') req.add_header('User-Agent', 'Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/8.0 Mobile/10A5376e Safari/8536.25') with request.urlopen(req) as f: print('Status:', f.status, f.reason) for k, v in f.getheaders(): print('%s: %s' % (k, v)) print('Data:', f.read().decode('utf-8'))登录后复制
这样豆瓣会返回适合iPhone的移动版网页:
如果要以POST发送一个请求,只需要把参数data以bytes形式传入。
模拟一个微博登录,先读取登录的邮箱和口令,然后按照weibo.cn的登录页的格式以username=xxx&password=xxx的编码传入:
from urllib import request, parse print('Login to weibo.cn...') #电子邮件 email = input('Email: ') #密码 passwd = input('Password: ') #相关的参数 login_data = parse.urlencode([ ('username', email), ('password', passwd), ('entry', 'mweibo'), ('client_id', ''), ('savestate', '1'), ('ec', ''), ('pagerefer', 'passport.weibo.cn/signin/welcome?entry=mweibo&r=passport.weibo.cn/sso/login') req.add_header('Origin', 'passport.weibo.cn') #构造User-Agent req.add_header('User-Agent', 'Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/8.0 Mobile/10A5376e Safari/8536.25') req.add_header('Referer', 'passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=www.baidu.com/") # 1. 如果这么写,只有使用opener.open()方法发送请求才使用自定义的代理,而urlopen()则不使用自定义代理。 response = opener.open(request) # 2. 如果这么写,就是将opener应用到全局,之后所有的,不管是opener.open()还是urlopen() 发送请求,都将使用自定义代理。 # urllib.request.install_opener(opener) # response = urllib.request.urlopen(request) # 获取服务器响应内容 html = response.read().decode("utf-8") # 打印结果 print(html)登录后复制
如果代理成功返回网址的信息。
如果网址出错或者代理地址有误,返回下面界面。
使用Python语言,能够帮助大家更好的学习Python。urllib提供的功能就是利用程序去执行各种HTTP请求。如果要模拟浏览器完成特定功能,需要把请求伪装成浏览器。伪装的方法是先监控浏j览器发出的请求,再根据浏览器的请求头来伪装,User-Agent头就是用来标识浏览器的。

