标签: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