1. 程式人生 > >Python爬蟲之自動登入與驗證碼識別

Python爬蟲之自動登入與驗證碼識別

轉自:http://blog.csdn.net/tobacco5648/article/details/50640691

在用爬蟲爬取網站資料時,有些站點的一些關鍵資料的獲取需要使用賬號登入,這裡可以使用requests傳送登入請求,並用Session物件來自動處理相關Cookie。

另外在登入時,有些網站有時會要求輸入驗證碼,比較簡單的驗證碼可以直接用pytesser來識別,複雜的驗證碼可以依據相應的特徵自己採集資料訓練分類器。

以CSDN網站的登入為例,這裡用Python的requests庫與pytesser庫寫了一個登入函式。如果需要輸入驗證碼,函式會首先下載驗證碼到本地,然後用pytesser識別驗證碼後登入,對於CSDN登入驗證碼,pytesser的識別率很高。

其中的pytesser的下載地址為: pytesser下載

具體程式碼如下:

#coding:utf-8
import sys
import time
import urllib
import shutil
import pytesser
import requests

from lxml import etree

config = {'gid': 1}

def parse(s, html, idx):
    result = {}

    tree = etree.HTML(html)
    try:
        result['lt'] = tree.xpath('//input[@name="lt"]/@value')[0]
        result['execution'] = tree.xpath('//input[@name="execution"]/@value')[0]
        result['path'] = tree.xpath('//form[@id="fm1"]/@action')[0]
    except IndexError, e:
        return None

    valimg = None
    valimgs = tree.xpath('//img[@id="yanzheng"]/@src')
    if len(valimgs) > 0:
        valimg = valimgs[0]

    validateCode = None
    if valimg:
        fname = 'img/' + str(idx) + '_' + str(config['gid']) + '.jpg'
        config['gid'] = config['gid'] + 1
        ri = s.get("https://passport.csdn.net" + valimg)
        with open(fname, 'wb') as f:
            for chk in ri:
                f.write(chk)
            f.close()
        validateCode = pytesser.image_file_to_string(fname)
        validateCode = validateCode.strip()
        validateCode = validateCode.replace(' ', '')
        validateCode = validateCode.replace('\n', '')
        result['validateCode'] = validateCode

    return result

def login(usr, pwd, idx):
    s = requests.Session()

    r = s.get('https://passport.csdn.net/account/login',
    headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0', 'Host': 'passport.csdn.net', })

    while True:
        res = parse(s, r.text, idx)
        if res == None:
            return False
        url = 'https://passport.csdn.net' + res['path']
        form = {'username': usr, 'password':pwd, '_eventId':'submit', 'execution':res['execution'], 'lt':res['lt'],}
        if res.has_key('validateCode'):
            form['validateCode'] = res['validateCode']
        s.headers.update({
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0',
            'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4',
            'Content-Type': 'application/x-www-form-urlencoded',
            'Host': 'passport.csdn.net',
            'Origin': 'https://passport.csdn.net',
            'Referer': 'https://passport.csdn.net/account/login',
            'Upgrade-Insecure-Requests': 1,
            })
        r = s.post(url, data=form)

        tree = etree.HTML(r.text)
        err_strs = tree.xpath('//span[@id="error-message"]/text()')
        if len(err_strs) == 0:
            return True
        err_str = err_strs[0]
        print err_str
        err = err_str.encode('utf8')

        validate_code_err = '驗證碼錯誤'
        usr_pass_err = '帳戶名或登入密碼不正確,請重新輸入'
        try_later_err = '登入失敗連續超過5次,請10分鐘後再試'

        if err[:5] == validate_code_err[:5]:
            pass
        elif err[:5] == usr_pass_err[:5]:
            return False
        elif err[:5] == try_later_err[:5]:
            return False
        else:
            return True

if __name__ == '__main__':
    main(sys.argv[1], sys.argv[2], 0)