凌的博客

您现在的位置是:首页  > 学无止境 > python

python

python 模拟登陆

2020-03-07python0
import requests
from downloader import downloader
import time
import json

session = requests.session()

verifycode_url = "http://域名.com/index.php/vcode"
verify = "./data/verify.png"
downloader(verifycode_url, verify, session)

verify_code = input("输入验证码:")
print(verify_code)
data = {'loginForm[username]': '用户名',
        'loginForm[password]': '密码',
        'loginForm[vcode]': verify_code,
        }
time.sleep(1)
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36', }

url = "http://域名.com/index.php/login"
r = session.post(url=url, data=data, headers=headers)
cookie_jar = r.cookies
cookie_t = json.dumps(requests.utils.dict_from_cookiejar(cookie_jar))

print(r.content.decode("utf-8"))
print(cookie_t)
time.sleep(1)
r = session.get("http://域名.com/admin/index/welcome.html")
print(r.content.decode("utf-8"))
import requests
import time
import os
import base64, re


def downloader(url, path,req=None):
    start = time.time()
    size = 0
    if req is None:
        response = requests.get(url, stream=True)  # stream 必须带上
    else:
        response = req.get(url, stream=True)  # stream 必须带上
    chunk_size = 1024  # 每次下载大小

    if "content-length" in response.headers:
        flag = 1
        content_size = int(response.headers['content-length'])
    else:
        flag = 0
        content_size = 1024

    if response.status_code == 200:
        if flag == 1:
            print("[文件大小]:%.4f MB" % (content_size / chunk_size / 1024))
        with open(path, "wb") as file:
            for data in response.iter_content(chunk_size=chunk_size):
                file.write(data)
                size += len(data)  # 已下载大小
                t = time.time() - start
                if t > 0:
                    kbs = round(size / round(time.time() - start, 3) / 1024, 2)
                # \r 指定第一个字符开始,搭配end属性完成覆盖进度条
                print("\r" + "[下载进度]:%s%.2f%% %.2fkb/s" % (
                    ">" * int(size * 50 / content_size), float(size / content_size * 100), kbs), end="")
        if flag == 0:
            print("[文件大小]:%.4f MB" % (os.path.getsize(path) / chunk_size / 1024))
        end = time.time()  # 结束时间
        print("\n" + "全部下载完成!用时%.2f秒" % (end - start))


如果本文对您有所帮助,请支持下本站哦!!!^_^

文章评论

0条评论