基本库urllib的使用

基本库urllib的使用

import  urllib.request

response= urllib.request.urlopen( ' https://www.python.org')
print(response. read(). decode (' utf-8')) #抓取python官网
# 返回结果# HTML网页
print(type(response)) #查看返回类型
#返回结果#	<class 'http.client.HTTPResponse'>
'''HTTPResposne类型的对象,可以使用read(),readinto(),getheader(name),getheaders(),fileno()等方法
举例:
print(response.status)#返回服务器代码200
print(response.getheaders())#返回请求头
print(response.getheader('Server'))返回请求头中的Server
'''

使用urllib.parse 将字典转化成字符串,使用POST方法传输给服务器

import urllib.parse
import urllib.request

data=bytes(urllib.parse.urlencode({'word':'hello'}),encoding='utf8') #编码
response=urllib.request.urlopen('http://httpbin.org/post',data=data)
print(response.read())

使用timeou参数

import urllib.request

response=urllib.request.urlopen('http://httpbin.org/get',timeout=0.1)
print(response.read())
#运行结果#urllib.error.URLError: <urlopen error timed out>
# 通过try except语句
import urllib.request
import socket
import urllib.error

try:
    response=urllib.request.urlopen('http://httpbin.org/get',timeout=0.1)
except urllib.error.URLError as e:
    if isinstance(e.reason,socket.timeout):
        print('TIME OUT')

Request

class urllib. request. Request ( ur 1, data=None, headers={}, origin_req_host=None, unverifiable=False, method=None)

第四个参数origin_red_host 指的是请求方的host名称或者ip地址

第五个参数unverifiable 表示这个请求是否是无法验证的,默认False.

例如,我们请求 HTML 文档中的图片,但是我们没有向动抓取图像的权限,这时unverifiable 的值就是True

urlopen可以实现最近基本的请求发起,如果请求需要加入参数,就需要使用Request

from urllib import request,parse

url='http://httpbin.org/post'
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36',
         'Host':'httpbin.org'
}
dict={'name':'Germey'
}
data=bytes(parse.urlencode(dict),encoding='utf-8')
req=request.Request(url=url,data=data,headers=headers,method='post')
response=request.urlopen(req)
print(response.read().decode('utf-8'))

高级用法

在上面过程中,我们虽然可以构造请求,但是对于一些更高级的操作(Cookie处理,代理设置)搞不了

urllib.request模块里的BasehandIer类,它是所有其他 Handler 的父类,它提

供了最基本的方法,例如 default_open ()、 protocol_request ()等

HITPDefaultErrorHandler :用于处理HTTP响应错误,错误都会抛出 HTTP Error 类型的异常

HTTPRedirectHandler :用于处理重定向

HTTPCookieProcessor 用于处理 Cookies

ProxyHandler :用于设置代理 默认 理为空

HπPPasswordMgr :用于管理密码,它维护了用户名和密码的表

HTTPBasicAuthHandler 用于管理认证,如果一个链接打开时需要认证,那么可以用它来解

决认证问题.

详情参考python官网文档

如果有一个网页需要登录后查看,需要借助HTTPBasicAuthhandler完成 ,构建更底层的方法.

from urllib.request import
HTTPPasswordMgrWithDefaultRealm,HTTPBasicAuthHandler,build_opener
from urllib.error import URLError

username='username'
password='password'
url='http://localhost:5000/'

p=HTTPPasswordMgrWithDefaultRealm() 
p.add_password(None,url,username,password)
auth_handler=HTTPBasicAuthHandler(p)
opener=build_opener(auth_handler) #使用build_opener()方法构建一个Opener,这个Opener在发送请求就已经成功了
try:
    result=opener.open(url)#利用Opener的open方法打开链接,完成验证
    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':'http://127.0.0.1:9743',
    'https':'https://127.0.0.1:9743'
})
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
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.txt文件

import http.cookiejar,urllib.request

filename='cookies.txt'
cookies=http.cookiejar.MozillaCookieJar(filename)
hanlder=urllib.request.HTTPCookieProcessor(cookies)
opener=urllib.request.build_opener(hanlder)
response=opener.open('http://www.baidu.com')
cookies.save(ignore_discard=True,ignore_expires=True)
#利用保存的文件

import http.cookiejar,urllib.request

cookies=http.cookiejar.LWPCookieJar()
cookies.load('cookies.txt',ignore_discard=True,ignore_expires=True)
handler=urllib.request.HTTPCookieProcessor(cookies)
opener=urllib.request.build_opener(handler)
response=opener.open('http://www.baidu.com')
print(response.read().decode('utf-8'))
#得到百度网页的源代码

cookie = http. cookiejar. LWPCookieJar (filename) 声明成LWP文件格式

用这个方法保存和使用cookies文件

处理异常

# URLerror
from urllib import request,error

try:
    response=request.urlopen('https://cuiqingcai.com/index.htm')
except error.URLError as e:
    print(e.reason)
#HTTPerror
'''code返回HTTP状态码,404(网页不存在),500(服务器内部错误)
   reason返回错误理由
   headers返回请求头
'''

from urllib import request,error

try:
    response=request.urlopen('https://cuiqingcai.com/index.htm')
except error.HTTPError as e:
    print(e.reason,e.code,e.headers,sep='\n')
 
'''
Not Found
404
Server: nginx/1.10.3 (Ubuntu)
Date: Sun, 27 Oct 2019 08:52:11 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: close
Set-Cookie: PHPSESSID=p27up8p6j12usjoc5ls0hk0ma7; path=/
Pragma: no-cache
Vary: Cookie
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Link: <https://cuiqingcai.com/wp-json/>; rel="https://api.w.org/"
'''
#代码更好的写法,先去捕获子类的错误,再看父类的错误
from urllib import request,error

try:
    response=request.urlopen('https://cuiqingcai.com/index.htm')
except error.HTTPError as e:
    print(e.reason,e.code,e.headers,sep='\n')
except error.URLError as e:
    print(e.reason)
else:
    print('Request Sucessfully')
    
    
    #reason的值不一定是字符串,也可以是对象
    import socket
import urllib.request
import urllib.error

try:
    response=urllib.request.urlopen('https://www.baidu.com',timeout=0.01)
except urllib.error.URLError as e:
    print(type(e.reason))
    if isinstance(e.reason,socket.timeout):
        print('TIME OUT')
    ''' 
    reason 属性的结果是 socket.timeout 所以,这里我们可以用 is instance ()方法来
判断它的类型,作出更详细的异常判断
'''
#解析链接
from urllib.parse import urlparseresult=urlparse('http://www.baidu.com/index.html:user?id=5#comment')
print(type(result),result)
'''
<class 'urllib.parse.ParseResult'>
ParseResult(scheme='http', netloc='www.baidu.com', path='/index.html:user', params='', query='id=5', fragment='comment')

包含有六个部分
:// 前面的是scheme,代表协议
第一个/符号前面代表netloc,域名,后面是path,即访问路径
params,代表参数
?后面代表查询条件query 一般用作GET类型的URL
#后面是锚点,用于定位页面格式
fragment
'''
urllib.parse.urlparse(urlstring,scheme='',allow_fragments=True)
urlstring:待解析的URL
scheme:默认的协议
allow_fragements:是否忽略frag.如果设置为False,fragment的部分就会忽略,它会被解析未path,parameters或query的一部分,而fragment部分为空.


  #返回结果ParseResult实际上是一个元组  
from urllib.parse import urlparse

result=urlparse('http://www.baidu.com/index.html:user?id=5#comment')
print(result.scheme,result[0],result.netloc,result[1])
'''
http http www.baidu.com www.baidu.com

'''
2.urlunparse()
有了urlparse(),相应就有了它的对应方法urlunparse().它接受参数是一个可送代的对象,但是它的长度必须是6,否则会抛出参数数量不足或者过多的问题.
from urllib.parse import urlunparse

data=['http111111','www.baidu.com','index.html','user','a=6','comment']
print(urlunparse(data))

'''
http111111111111://www.baidu.com/index.html;user?a=6#comment
'''
3.urlsplit()
这个方法和urlparse()方法非常相似,只不过不再单独解析params这部分,只返回五个结果.
from urllib.parse import urlsplit

result=urlsplit('http://www.baidu.com/index.html;user?id=%#comment')
print(result)
'''
SplitResult(scheme='http', netloc='www.baidu.com', path='/index.html;user', query='id=%', fragment='comment')
'''

4.urlunspilit()

from urllib.parse import urlunsplit

data=['http','www.baidu.com','index.html','a=6','comment']

print(urlunsplit(data))

'''
http://www.baidu.com/index.html?a=6#comment

'''

5.urljoin
有了urlunparse()和urlunsplit()方法,可以完成连接的合并,不过前提必须要有特定的长度的对象,连接的每一部分都要清晰分开.
此外,生成连接还有一种方法,那就是urljoin()方法.提供一个base_url(基础链接)作为第一个参数,将新的链接作为第二个参数,该方法会分析base_url的schme,netloc和path这三个内容并对新链接缺失的部分进行补充最后返回结果.


from urllib.parse import urljoin

print(urljoin('http://www.baidu.com','FAQ.html'))
print(urljoin('http://www.baidu.com','https://cuiqingcai.com/FAQ.html'))
#差不多的了
'''
http://www.baidu.com/FAQ.html
https://cuiqingcai.com/FAQ.html
'''
可以发现,base_url提供三项内容scheme,netloc,path.如果这三项在新的链接里不存在,就给予补充;如果新链接存在,就使用新的链接部分.而base_url中的params,query,fragment是不起作用的.
6.urlencode()
#在构造GET请求参数的时候非常有用!! 

from urllib.parse import urlencode

params={
    'name':'germey'
    ,'age':'18'
}
base_url='http://www.baidu.com?'
url=base_url+urlencode(params)
print(urlencode(params))
print(url)
'''
name=germey&age=18
http://www.baidu.com?name=germey&age=18
'''
将字典类型转化GET请求参数
7.parse_qs()
from urllib.parse import parse_qs

params={
    'name':'germey'
    ,'age':'18'
}
query='name=germey&age=18'
print(parse_qs(query))

'''
{'name': ['germey'], 'age': ['18']}
'''
8.parse_qsl
将参数转化为元组组成的列表
from urllib.parse import parse_qsl

params={
    'name':'germey'
    ,'age':'18'
}
query='name=germey&age=18'
print(parse_qsl(query))
'''
[('name', 'germey'), ('age', '18')]
'''
9.quote()
该方法可以将内容转化为URL编码的格式.URL中带有中文参数时,有时候可能或导致乱码的问题,此时使用这个方法可以将中文字符转化为URL编码.

from urllib.parse import quote

keyword='壁纸'
url='https:www.baidu.com/s?wd='+quote(keyword)
print(quote(keyword))
print(url)
'''
%E5%A3%81%E7%BA%B8
https:www.baidu.com/s?wd=%E5%A3%81%E7%BA%B8
'''
10.unquote()
from urllib.parse import unquote

url='https:www.baidu.com/s?wd=%E5%A3%81%E7%BA%B8'
print(unquote(url))
'''
https:www.baidu.com/s?wd=壁纸
'''
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值