Python Urllib 库基本使用

import urllib.request #请求模块
import urllib.parse #url解析模块
import urllib.error #异常处理模块
import socket #开往网络应用必备的功能模块

response = urllib.request.urlopen('http://www.baidu.com')
print(response.read().decode('utf-8'))  #response.read()可以获取到网页的内容


data = bytes(urllib.parse.urlencode({'word': 'hello'}), encoding='utf8')#urllib.parse,通过bytes(urllib.parse.urlencode())可以将post数据进行转换放到urllib.request.urlopen的data参数中。这样就完成了一次post请求。
print(data)
response = urllib.request.urlopen('http://httpbin.org/post', data=data) #如果我们添加data参数的时候就是以post请求方式请求,如果没有data参数就是get请求方式
print(response.read())

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

response = urllib.request.urlopen('https://www.python.org')
print(type(response))
# print(response.status)#获取状态码 【遇到提示语法错误】
print (response.getheaders())#获取响应头部信息
print (response.getheader("server"))#查找头部信息

request = urllib.request.Request('https://python.org') #request()包装请求
response = urllib.request.urlopen(request)
print(response.read().decode('utf-8'))


from urllib import request, parse

url = 'http://httpbin.org/post'
#以下设置头部信息
headers = {
    'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)',
    'Host': 'httpbin.org'
}
dict = {
    'name': 'alex'
}
data = bytes(parse.urlencode(dict), encoding='utf8')
req = request.Request(url=url, data=data, headers=headers, method='POST')
response = request.urlopen(req)
print(response.read().decode('utf-8'))



#获取cookie以及存储cookie
import http.cookiejar, urllib.request
cookie = http.cookiejar.CookieJar()#声明CookieJar对象实例来保存cookie
handler = urllib.request.HTTPCookieProcessor(cookie)#利用urllib库的HTTPCookieProcessor对象来创建cookie处理器
opener = urllib.request.build_opener(handler)#通过handler构建opener
response = opener.open('http://www.baidu.com')
for item in cookie:
    print(item.name+"="+item.value)

#获取cookie保存到文件中
import http.cookiejar, urllib.request
filename = "cookie.txt" #保存cookie的文件
cookie = http.cookiejar.MozillaCookieJar(filename)#声明一个MozillaCookieJar对象实例(cookie)来保存cookie,后面写入文件
handler = urllib.request.HTTPCookieProcessor(cookie)#还是创建处理器
opener = urllib.request.build_opener(handler)#创建支持处理HTTP请求的opener对象
response = opener.open('http://www.baidu.com')
cookie.save(ignore_discard=True, ignore_expires=True)#保存cookie到文件
#ignore_discard表示即使cookie将被丢弃也将保存下来,ignore_expires表示如果该文件中cookie已经存在,则覆盖原文件写入

#从文件中获取cookie,并访问
import http.cookiejar, urllib.request
cookie = http.cookiejar.LWPCookieJar()#声明CookieJar对象实例来保存cookie
cookie.load('cookie.txt', ignore_discard=True, ignore_expires=True)#从文件中读取内容到cookie变量中
handler = urllib.request.HTTPCookieProcessor(cookie)#处理器
opener = urllib.request.build_opener(handler)#创建支持处理HTTP请求的opener对象
response = opener.open('http://www.baidu.com')
print(response.read().decode('utf-8'))


猜你喜欢

转载自blog.csdn.net/qq_15907907/article/details/80207706