urllib的简单应用

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'))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值