码迷,mamicode.com
首页 > 其他好文 > 详细

自动化

时间:2017-06-01 10:29:46      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:ubuntu   os.path   构造   div   requests   referer   exce   cpp   mozilla   

import requests
import http.cookiejar as cookielib
import re
import time
import os.path
from PIL import Image



# 构造 Request headers
headers = {
    "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 captcha
def 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=postdata, headers=headers)
    text = str(login_page.content,utf-8)
    print(text)
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

 

自动化

标签:ubuntu   os.path   构造   div   requests   referer   exce   cpp   mozilla   

原文地址:http://www.cnblogs.com/A-FM/p/6926920.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!