vs2012建设空网站,网站开发能申请软件著作权吗,淄博网站制作优化推广,湖北营销型网站建设多少钱1、urllib库的介绍
可以实现HTTP请求#xff0c;我们要做的就是指定请求的URL、请求头、请求体等信息
urllib库包含如下四个模块
request#xff1a;基本的HTTP请求模块#xff0c;可以模拟请求的发送。error#xff1a;异常处理模块。parse#xff1a;工具模块#x…1、urllib库的介绍
可以实现HTTP请求我们要做的就是指定请求的URL、请求头、请求体等信息
urllib库包含如下四个模块
request基本的HTTP请求模块可以模拟请求的发送。error异常处理模块。parse工具模块如拆分、解析、合并robotparse主要用来识别网站的rebots.txt文件。用的较少
2、发送请求
2.1、urlopen urllib.request模块提供了基本的构造HTTP请求的方法可以模拟浏览器的请求发起过程同时还具有处理授权验证Authentication、重定向Redireaction、浏览器Cookie以及一些其他功能。
import urllib.requestresponse urllib.request.urlopen(https://www.baidu.com)
print(response.read().decode(utf-8))
print(type(response)) #输出响应的类型 HTTPResponse类型
print(response.status) #输出响应的状态码 200
print(response.getheaders()) #输出响应的头信息
print(response.getheader(Server)) #调用getheader方法传参server得其值
2.1.1、data参数 data参数是可选的。在添加该参数时需要使用bytes方法将参数转化为字节流编码格式的内容即bytes类型。如果传递了这个参数那么它的请求方式就不再是GET而是POST。
import urllib.parse
import urllib.request# www.httpbin.org可以提供HTTP请求测试
data bytes(urllib.parse.urlencode({name:germey}),encodingutf-8)
response urllib.request.urlopen(https://www.httpbin.org/post,datadata)
print(response.read().decode(utf-8))# {...form:{name:germey...}}表明是模拟表单提交
# 这里传递了一个参数name值是germey需要将其转码成bytes类型。转码时采用了bytes方法该方法的第一个参数得是str类型因此用urllib.parse模块李的urlencode方法将字典参数转化为字符串第二个参数用于指定编码格式。
2.1.2、timeout参数 timeout参数用于设置超时时间单位为秒意思是如果请求超出了设置的这个时间还没有得到响应就会抛出异常。如果不指定该参数则会使用全局默认时间。这个参数支持HTTP、HTTPS、FTP请求。
import urllib.requestresponse urllib.request.urlopen(https://www.httpbin.org/get,timeout0.1)
print(response.read())# urllib.error.URLError: urlopen error timed out 超时
# 通过设置此超时时间实现当一个网页长时间未响应时跳过对它的抓取。
2.1.3、其他参数
context参数该参数必须是ss1.SSLContext类型用来指定SSL的设置。cafile和capath这两个参数分别用来指定CA证书和其路径这两个在请求HTTPS链接会有用cadefault参数已经弃用默认值为False
2.2、Request 通过构造Request类型的对象一方面可以将请求独立成一个对象另一方面可更加丰富灵活的配置参数。
import urllib.requestrequest urllib.request.Request(https://python.org)
response urllib.request.urlopen(request)
print(response.read().decode(utf-8)) 构造方法
class urllib.request.Request(url,dataNone,headers{},origin_req_hostNone,unverifiableFalse,methodNone)
第一个参数 url用于请求URL这是必传参数其他都是可选参数。第二个参数data如果要传参数必须是bytes类型的。如果数据是字典先用urllib.parse模块里的urlencode方法进行编码。第三个参数headers是一个字典这就是请求头构造请求时既可以通过headers参数直接狗仔此项也可以通过调用请求实例的add_header方法添加。添加请求头最常见的方法就是修改User-Agent来伪装成浏览器。默认为Python-usrllib。若伪装成火狐浏览器则设置User-Agent为Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36第四个参数origin_req_host值的是请求方的host名称或IP地址。第五个参数unverifiable表示请求是否是无法验证的默认为False,意思是用户没有足够的权限来接受这个请求的结果。第六个参数method是一个字符串用来指示请求使用的方法例GET、POST、PUT等。
2.3、高级用法 各种处理器Handler。会有各种Handler子类继承BaseHandler类
HTTPDefaultErrorHandler用来处理HTTP响应错误所以错误类型都会抛出HTTPRrror类型的异常。HTTPRedirectHandler用于处理重定向。HTTPCookieProcessor用于处理Cookie。ProxyHandler用于设置代理代理默认为空。HTTPPasswordMgr用于管理密码它维护用户名密码对照表。HTTPBasicAuthHandler用于管理认证比如一个链接在打开时需要认证。 另一个重要类OpenerDirector简称opener、Opener类可以提供open方法。利用Handler类来构建Opener类。
2.3.1、验证*
# 处理访问时需要用户名密码登录验证的方法。
from urllib.request import HTTPPasswordMgrWithDefaultRealm,HTTPBasicAuthHandler,build_opener
from urllib.error import URLErrorusername admin
password admin
url https://ssr3.scrape.center/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)# 首先实例化了一个HTTPBasicAuthHandler对象auth_handler其参数是HTTPPasswordMgrWithDefaultRealm对象它利用add_password方法添加用户名和密码。这样就建立了用来处理验证的Handler类。
2.3.2、代理*
# 添加代理
from urllib.error import URLError
from urllib.request import ProxyHandler,build_openerproxy_handler ProxyHandler({http:http//127.0.0.1:8080,https:https://127.0.0.1:8080
})
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)
2.3.3、Cookie*
# 1、获取网站的Cookie
import http.cookiejar,urllib.requestcookie http.cookiejar.CookieJar() # 声明Cookiejar对象
handler urllib.request.HTTPCookieProcessor(cookie) # 构建一个handler
opener urllib.request.build_opener(handler) # 构建opener
response opener.open(https://www.baidu.com)
for item in cookie:print(item.name item.value) # 2、输出文件格式的内容cookie
import urllib.request,http.cookiejarfilename cookie.txt
cookie http.cookiejar.MozillaCookieJar(filename)
# 若要保存LWP格式修改为
cookie http.cookiejar.LWPCookieJar(filename)handler urllib.request.HTTPCookieProcessor(cookie)
opener urllib.request.build_opener(handler)
response opener.open(https://www.baidu.com)
cookie.save(ignore_discardTrue,ignore_expiresTrue)# 3、读取内容、以LWP格式为例
import urllib.request,http.cookiejarcookie http.cookiejar.LWPCookieJar()
cookie.load(cookie.txt,ignore_discardTrue,ignore_expiresTrue) # 获取cookie内容
handler urllib.request.HTTPCookieProcessor(cookie) # 构建handler类和opener类
opener urllib.request.build_opener(handler)
response opener.open(https://www.baidu.com)
print(response.read().decode(utf-8))
3、异常处理 urllib库中的error模块定义了由request模块产生的异常。出现问题request则抛出error模块定义的异常。
3.1、URLError URLError类来自urllib库中的error模块继承自OSError类是error异常模块处理的基类由request模块产生的异常都可以来处理。 具有一个属性reason返回错误的原因。
from urllib import request,error
try:response request.urlopen(https://cuiqingcai.com/404)
except error.URLError as e:print(e.reason) # Not Found
# 捕获异常避免程序中止
3.2、HTTPError HTTPError是URLError的子类专门来处理HTTP请求的错误例如认证请求失败等有三个属性
code返回HTTP状态码。reason返回错误原因。headers返回请求头。
from urllib import request,error
try:response request.urlopen(https://cuiqingcai.com/404)
except error.HTTPError as e:print(e.reason,e.code,e.headers,sep\n)# 这里捕获了HTTPError的异常输出了reason、code和headers属性。
# 有时、reason属性返回的不一定是字符串也可能是一个对象。
4、解析链接-parse模块 urllib库里还提供parse模块此模块定义了处理URL的标准接口例如实现URL各部分的抽取、合并以及链接转换。
4.1、urlparse* 该方法可以实现URL的识别和分段。
from urllib.parse import urlparse
result urlparse(htps://www.baidu.com/index.html;user?id5#comment)
print(type(result))
print(result)# class urllib.parse.ParseResult
# ParseResult(schemehtps, netlocwww.baidu.com, path/index.html;user, params, queryid5, fragmentcomment)
# 解析结果是一个ParseResult类型的对象包含六部分:scheme、netloc、path、params、query和fragment。 urlparse的API用法
urllib.parse.urlparse(urlstring,scheme,allow_fragmentTrue)
urlstring待解析的URL。scheme默认的协议例如http或https。allow_fragment是否忽略fragment。
4.2、urlunparse* 对立方法urlunparse用于构造URL。接收的参数是一个可迭代对象其长度必须是6否则抛出参数数量不足或过多的问题。
from urllib.parse import urlunparsedata [https,www.baidu.com,index.html,user,a6,comment] #列表类型也可其它
print(urlunparse(data)) # https://www.baidu.com/index.html;user?a6#comment
# 成功构造URL
4.3、urlsplit* 此方法和urlparse方法相似不再单独解析params这一部分params会合并到path中返回5个结果。
from urllib.parse import urlsplitresult urlsplit(https://www.baidu.com/index.html;user?id5#comment)
print(result) # SplitResult(schemehttps, netlocwww.baidu.com, path/index.html;user, queryid5, fragmentcomment)# 也是一个元组可以用属性名和索引获取
print(result.scheme,result[0]) # https https
4.5、urlunsplit* 将链接各部分组成完整链接。传入的参数为可迭代对象例如列表、元组参数长度为5
from urllib.parse import urlunsplitdata [https,www.baidu.com,index.html,a6,comment]
print(urlunsplit(data)) # https://www.baidu.com/index.html?a6#comment
4.6、urljoin 生成链接方法。先提供一个base_url基础链接作为第一个参数将新的链接作为第二个参数。urljoin会解析base_url的scheme、netloc和path这三个内容。
from urllib.parse import urljoinprint(urljoin(https://www.baidu.com,FAQ.html))
print(urljoin(https://www.baidu.com,https://cuiqingcai.com/FAQ.html))
print(urljoin(https://www.baidu.com/about.html,https://cuiqingcai.com/FAQ.html))
print(urljoin(https://www.baidu.com/about.html,https://cuiqingcai.com/FAQ.html?question2))
print(urljoin(https://www.baidu.com,https://cuiqingcai.com/FAQ.html/index.php))
print(urljoin(https://www.baidu.com,?category2#comment))
print(urljoin(www.baidu.com,?category2#comment))
print(urljoin(www.baidu.com?#comment,?category2))#若干新链接不存在base_url中的三项就予以补充如果存在就用新链接里的base_url是不起作用的。
4.7、urlencode urlencode构造GET请求参数。
from urllib.parse import urlencodeparams {name:germey,age:25
}
base_url https://www.baidu.com?
url base_url urlencode(params)
print(url) # https://www.baidu.com?namegermeyage25 成功将字段类型转化为GET请求参数
4.8、parse_qs 反序列化。可以将一串GET请求参数转回字典。
from urllib.parse import parse_qsquery namegermyage25
print(parse_qs(query))#{name: [germy], age: [25]} 4.9、parse_qsl 将参数转化为由元组组成的列表
from urllib.parse import parse_qslquery namegermyage25
print(parse_qsl(query))# [(name, germy), (age, 25)]
4.10、quote 将内容转化为URL编码格式。当URL中带有中文参数时有可能导致乱码问题此时用quote方法可以将中文字符转为URL编码。
from urllib.parse import quotekeyword 壁纸
url https://www.baidu.com/?wd quote(keyword)
print(url)# https://www.baidu.com/?wd%E5%A3%81%E7%BA%B8
4.11、unquote 进行URL解码
url https://www.baidu.com/?wd%E5%A3%81%E7%BA%B8
print(unquote(url))
# https://www.baidu.com/?wd壁纸
5、分析Robots协议
5.1、Robots协议 Robots协议也称作爬虫协议、机器人协议全名为网**络爬虫排除标准**用来告诉爬虫和搜索引擎哪些页面可以抓取哪些不可以通常叫做robots.txt文件在网站根目录下。 搜索爬虫在访问一个网站时先检查这个站点根目录下是否存在robots.txt文件若存在则根据其中定义的爬取范围来爬取。若无则访问能访问到的页面。
# robots.txt文件样例
# 限定搜索爬虫只能爬取public目录
User-agent:* # 搜索爬虫名称*代表此文件对所有爬虫有效
Disallow:/ # 不允许爬虫爬取的目录/代表不允许爬取所有页面
Allow:/public/ # 允许爬取的页面和Disallow一起使用用来排除限制。此表示可爬取public目录# 禁止所有爬虫访问所有目录
User-agent:*
Disallow:/# 允许所有爬虫访问所有目录 robots.txt文件留空也是可以的
User-agent:*
Disallow:# 禁止所有爬虫访问网站某些目录
User-agent:*
Disallow:/private/
Disallow:/tmp/# 只允许某一个爬虫访问所有目录
User-agent:WebCrawler
Disallw:
User-agent:*
Disallw:/
5.2、爬虫名称 其实爬虫有固定名字。例如
爬虫名称网站名称BaiduSpider百度Googlebot谷歌360Spider369搜索YodaoBot有道ia_archiverAlexaScooteraltavistaBingbot必应
5.3、rebotparser模块 rebotparser模块解析robots.txt文件。该模块提供一个类RobotFileParser它可以根据网站的robots.txt文件判断是否有权限爬取。
# 用法在构造方法里串robots.txt文件链接即可
urllib.rebotparser.RobotFileParser(url) 下面列出RobotFileParser类的几个常用方法
set_url设置tobots.txt的文件链接read读取robots.txt文件进行分析。记得调用否则接下来的判断都为Falseparse解析tobots.txt文件can_fetch有两个参数第一个是User-agent第二个是要抓取的URL。返回结果True或False。表示User-agent指示的搜索引擎是否可以抓取这个URLmtime返回上次抓取和分析robots.txt文件的时间这对长时间分析和抓取robots.txt文件的搜索爬虫很有必要。modified对于长时间分析和抓取的搜索爬虫重要可以将当前时间设置为上次抓取和分析robots.txt文件的时间。
from urllib.robotparser import RobotFileParserrp RobotFileParser()
rp.set_url(https://www.baidu.com/robots.txt)
rp.read()
print(rp.can_fetch(Baiduspider,https://www.baidu.com)) # True
print(rp.can_fetch(Baiduspider,https://www.baidu.com/homepage/)) # True
print(rp.can_fetch(Googlebot,https://www.baidu.com/homepage/)) # False