urllib提供了一系列用于操作URL的功能:
使用urllib发送get请求
import urllib.request
with urllib.request.urlopen("http://www.baidu.com") as response:
html = response.read()
print(html.decode()) #获得html页面内容
print(response.code) #获得响应码 200
print(response.headers) #获得头信息
使用urllib添加信息头
import urllib.request
url = "http://www.douban.com"
headers = {
"User-Agen":"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) "
"Gecko/20100101 Firefox/61.0“
}
#创建一个请求对象
req = urllib.request.Request(url,headers=headers)
#发送请求对象
with urllib.request.urlopen(req) as response:
html = response.read()
with open("douban.html",'wb') as f:
f.write(html)
使用urllib发送post请求
import urllib.request
import urllib.parse
url = "http://127.0.0.1:8000/login"
valuses = {
"username":"daiyu",
"password":"nicai1993"
}
#对数据进行编码
data = urllib.parse.urlencode(values).encode('utf-8')
#创建一个对象
req = urllib.request.Request(url,data=data,method="POST")
with urllib.request.urlopen(req) as response:
html = response.read()
with open("login.html","wb") as f:
f.write(html)
如何获取网站cookie信息
from http import cookiejar
import urllib.request
#创建一个cookie对象
cookie = cookiejar.MozillaCookieJar("cookie.txt")
#创建一个cookie处理器
cookie_process = urllib.request.HTTPCookieProcessor(cookie)
#创建一个opener
opener = urllib.request.build_opener(cookie_process)
with opener.open("http://www.baidu.com") as response:
print(response)
print(cookie)
#保存
cookie.save()
#加载
cooki.load("cookie.txt")
模拟登录过程
import urllib.request
from http import cookiejar
import urllib.parse
url = "http://127.0.0.1:8000/login"
values = {
"csrfmiddlewaretoken":"",
"username":"daiyu",
"password":"nicai1993"
}
cookie = cookiejar.CookieJar()
cookie_process = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(cookie_process)
with opener.open(url) as response:
for item in cookie:
values['csrfmiddlewaretoken'] = item.value
print(item.value)
print(item.name)
print(dir(item))
data = urllib.parse.urlencode(values).encode("utf-8")
req = urllib.request.Request(url,data= data)
response = opener.open(req)
with open("admin.html","wb") as f:
f.write(response.read())
print(response.read().decode('utf-8'))