1. 程式人生 > >python模擬登入豆瓣

python模擬登入豆瓣

from urllib.request import urlretrieve
import requests
from bs4 import BeautifulSoup
from os import remove
import http.cookiejar as cookielib
from PIL import Image

url = 'https://accounts.douban.com/login'

datas = {'source': 'index_nav',
         'remember': 'on'}

headers = {'Host':'www.douban.com',
           'Referer': 'https://www.douban.com/',
           'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0',
           'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
           'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
           'Accept-Encoding':'gzip, deflate, br'}

# 嘗試使用cookie資訊
session = requests.session()
session.cookies = cookielib.LWPCookieJar(filename='cookies')
try:
    session.cookies.load(ignore_discard=True)
except:
    print("Cookies未能載入")
    #cookies載入不成功,則輸入賬號密碼資訊
    datas['form_email'] = input('Please input your account:')
    datas['form_password'] = input('Please input your password:')


#獲取圖片驗證碼
def get_captcha():
    r = requests.post(url, data=datas, headers=headers)
    page = r.text
    soup = BeautifulSoup(page, "html.parser") 
    
    # 利用bs4獲得驗證碼圖片地址  獲取圖片地址
    #<img id="captcha_image" src="https://www.douban.com/misc/captcha?id=OJ3tZcdY85WL6Aj85OP219s4:en&amp;size=s" alt="captcha" class="captcha_image"/>
    img_src = soup.find('img', {'id': 'captcha_image'}).get('src')
    print('img_src:',img_src)
    urlretrieve(img_src, 'captcha.jpg')
    try:
        im = Image.open('captcha.jpg')
        im.show()
        im.close()
    except:
        print('到本地目錄開啟captcha.jpg獲取驗證碼')
    finally:
        captcha = input('please input the captcha:')
        remove('captcha.jpg')
    #<input type="hidden" name="captcha-id" value="OJ3tZcdY85WL6Aj85OP219s4:en"/>
    captcha_id = soup.find( 'input', {'type': 'hidden', 'name': 'captcha-id'}).get('value')
    return captcha, captcha_id


def isLogin():
    '''
    通過檢視使用者個人賬戶資訊來判斷是否已經登入
    '''
    url = "https://www.douban.com/accounts/"
    login_code = session.get(url, headers=headers,
                             allow_redirects=False).status_code
    if login_code == 200:
        return True
    else:
        return False


def login():
    try:
        captcha, captcha_id = get_captcha()
        print('captcha:',captcha)
        print('captcha_id:',captcha_id)
        # 增加表資料
        datas['captcha-solution'] = captcha
        datas['captcha-id'] = captcha_id
    except:
        print("無需圖片驗證碼")
    
    # 儲存 cookies 到檔案,
    # 下次可以使用 cookie 直接登入,不需要輸入賬號和密碼
    session.cookies.save()

if __name__ == '__main__':
    if isLogin():
        print('Login successfully')
    else:
        login()