Python爬虫入门:使用urllib.request中的Handler类来构建Opener
2019-07-22 10:36
495 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_36365528/article/details/96831245
读取并利用Cookies
Python爬虫入门:使用urllib.request中的Handler类来构建Opener
概述
在请求的时候,经常会遇到登录验证、处理Cookies以及设置代理等,这时候就需要Handler登场了。urllib.request模块的BaseHandler是所有Handler类的父类,提供了最基本的方法,如default_open(),protocol_request()等。
基本的Hander类如下:
类 | 作用 |
---|---|
HTTPDefaultErrorHandler | 处理HTTP响应错误,会抛出HTTPError异常 |
HTTPRedirectHandler | 处理重定向 |
HTTPCookieProcessor | 用于处理Cookies |
ProxyHandler | 用于设置代理,默认代理为空 |
HTTPPasswordMgr | 用于管理密码,维护用户名和密码的表 |
HTTPBasicAuthHandler | 用于管理认证 |
验证
from urllib.request import HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler, build_opener from urllib.error import URLError username = 'username' password = 'password' url = 'https://www.baidu.com' p = HTTPPasswordMgrWithDefaultRealm() p.add_password(None, url, username, password) auth_handler = HTTPBasicAuthHandler(p) opener = build_opener(auth_handler) try: result = opener.open(url) html = result.read().decode('utf-8') print(html) except URLError as e: print(e.reason)
代理
from urllib.error import URLError from urllib.request import ProxyHandler, build_opener proxy_handler = ProxyHandler({ 'http': '113.128.8.9:9999', 'https': '113.128.8.9:9999' }) opener = build_opener(proxy_handler) try: response = opener.open('https://www.baidu.com') print(response.read().decode('utf-8')) except URLError as e: print(e.reason)
Cookies
获取Cookies
import http.cookiejar, urllib.request cookie = http.cookiejar.CookieJar() handler = urllib.request.HTTPCookieProcessor(cookie) opener = urllib.request.build_opener(handler) response = opener.open('http://www.baidu.com') for item in cookie: print(item.name+" = "+item.value)
存储获取的Cookies
保存为普通格式
import http.cookiejar, urllib.request filename = 'cookies.txt' cookie = http.cookiejar.MozillaCookieJar(filename) handler = urllib.request.HTTPCookieProcessor(cookie) opener = urllib.request.build_opener(handler) response = opener.open('http://www.baidu.com') cookie.save(ignore_discard=True, ignore_expires=True)
保存为LWP格式
import http.cookiejar, urllib.request filename = 'cookies.txt' cookie = http.cookiejar.LWPCookieJar(filename) handler = urllib.request.HTTPCookieProcessor(cookie) opener = urllib.request.build_opener(handler) response = opener.open('http://www.baidu.com') cookie.save(ignore_discard=True, ignore_expires=True)
读取并利用Cookies
import http.cookiejar, urllib.request cookie = http.cookiejar.LWPCookieJar() cookie.load('cookies.txt', ignore_discard=True, ignore_expires=True) handler = urllib.request.HTTPCookieProcessor(cookie) opener = urllib.request.build_opener(handler) response = opener.open('http://www.baidu.com') print(response.read().decode('utf-8'))
相关文章推荐
- Python爬虫入门之Urllib库的基本使用
- 在python3中使用urllib.request编写简单的网络爬虫
- 在python3中使用urllib.request编写简单的网络爬虫
- Python爬虫入门:Urllib库使用详解(模拟CSDN登录)
- Python爬虫入门1之urllib库的使用
- Python爬虫入门之Urllib库的基本使用 (三)
- Python-爬虫03:urllib.request模块的使用
- Python3爬虫入门之Request库的使用
- Python爬虫中urllib库的使用入门--基本请求的实现与文本转码
- 在python3中使用urllib.request编写简单的网络爬虫
- 数据爬虫(二):python爬虫中urllib库详解,parse和request使用方法
- python3爬虫攻略(1):urllib.request使用(1)
- Python爬虫入门一之Urllib库的基本使用
- python3网络爬虫一《使用urllib.request发送请求》
- python爬虫入门——beautifulsoup初使用
- Python爬虫----爬虫入门(4)---urllib2 模拟登陆
- Python自动化构建工具scons使用入门笔记
- Python爬虫—1入门_3_cookie的使用
- Python爬虫入门(6):Cookie的使用
- python网络应用入门:网络爬虫的使用