首页 > 代码库 > 自动化

自动化

import requestsimport http.cookiejar as cookielibimport reimport timeimport os.pathfrom PIL import Image# 构造 Request headersheaders = {    "Host": "www.dotcpp.com",    "Referer": "http://www.dotcpp.com/oj/loginpage.php",    User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0,}# 构造 cookie 信息session = requests.session()session.cookies = cookielib.LWPCookieJar(filename=cookies)try:    session.cookies.load(ignore_discard=True) # 如果已经有 cookie信息的话 直接用于登录except:    print("Cookie 未能加载")    def get_capther():    captcha_url = http://www.dotcpp.com/oj/vcode.php    r = session.get(captcha_url, headers=headers)    with open(captcha.jpg, wb) as f:        f.write(r.content)        f.close()    try:        im = Image.open(captcha.jpg)        im.show()        im.close()    except:        print(u请到 %s 目录找到captcha.jpg 手动输入 % os.path.abspath(captcha.jpg))    captcha = input("please input the captcha\n>")    return captchadef login():    post_url = http://www.dotcpp.com/oj/login.php    postdata = {        user_id: xpower,        password: q.123456,        vcode: get_capther()    }        login_page = session.post(post_url, data=http://www.mamicode.com/postdata, headers=headers)    text = str(login_page.content,utf-8)    print(text)                                                            

 

自动化