Joke Collection Website - Blessing messages - Python crawler, the question about verification code. Enter the verification code to search.

Python crawler, the question about verification code. Enter the verification code to search.

# Give me an example for your reference? The verification code request has been changed once.

#! /usr/bin/python?

#coding=utf-8?

Import? ask

Import? urllib?

Import? urllib2,hashlib,md5

From where? BeautifulSoup? Import? Beautiful voice

Import? Kuchlib

def? _md5 (password):

md5? =? hashlib.md5()?

Md5.update(str.encode (password))

psw? =? md5.hexdigest()

Return? The PacificSouthwest.

Website? =? /captcha/randomcode? 2hy270c2ji 1 '

req? =? urllib2。 Request (url)

res_data? =? urllib2.urlopen(req)

res? =? res_data.read()

Output _ file? =? Open ('1.jpg',? WB’)?

output_file.writelines(res)?

output_file.close()

Verification code? =? res_data.headers['Set-Cookie']。 Split (';' )[0]

Verification code? =? verify code . replace(' verify code = ','')

File name? =? RES _ data . headers[' Content-disposition ']。 Split (';' ) [1]. Strip ()

Execute (file name)

Cookeye? =? Kukri cloth. CookieJar()

Opening? =? urllib2.build_opener(urllib2。 HTTPCookieProcessor(cookiejar))

vidcode=? Raw_input(u "Please enter the verification code (in this path? 1.jpg):? ”)?

Data? =? { ' user _ log in ':" love sword 85 @ yeah . net ",

isMd5':" 1 ",

user _ pwd ':_ MD5(' love 123456 '),

Verifycode': video code,

URL“:”}

Website? =? /h/login.json '

Data? =? Urllib.urlencode (data)

Title? =? {'Content Type':? application/x-www-form-urlencoded ',? X-Requested-With ':XMLHttpRequest ',? Cookie ':verify code = { 0 }; “。 Format (verification code)}

Request? =? urllib2。 Request (url, data, title)

Response? =? Opener.open (request)

Print? -Results-'

Print? response.read()

Print? -Title-'

Print? Response. Title

Print? Cookies.

For what? Cookies? Are you online? cookiejar:

Print? Cookies