mechanize登陆csdn

#coding=utf-8

import mechanize
import cookielib
from bs4 import BeautifulSoup as BS4


URL = 'https://passport.csdn.net/account/login'
COOKIE_FILE = 'csdn.cookie.txt'
HOST_URL = 'http://www.csdn.net'
BLOG_URL = 'http://blog.csdn.net'
MY_BLOG_URL = 'http://write.blog.csdn.net'

def to_utf8(s):
	return s.decode('utf-8').encode('gb2312')
 

def show_utf8(s):
	print(to_utf8(s))

def save_response(br, fname):
	c = br.response().read()
	with open(fname,'w+') as f:
		f.write(c)

def init_browser():
	#Browser
	br = mechanize.Browser()

	#Cookie Jar
	ckjar = cookielib.LWPCookieJar()
	br.set_cookiejar(ckjar)

	#Browser Options
	br.set_handle_equiv(True)
	#br.set_handle_gzip(True)
	br.set_handle_redirect(True)
	br.set_handle_referer(True)
	br.set_handle_robots(False)

	#Refresh
	br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), \
			max_time = 1)

	#Debugging messages
	br.set_debug_http(True)
	br.set_debug_redirects(True)
	br.set_debug_responses(True)

	#User-Agent [Cheating]
	br.addheaders = [('User-Agent', ''
	'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36',)]
	return br, ckjar

#Store cookie
def store_cookie():
	br,ckjar = init_browser()

	rs = br.open(URL)
	html = rs.read()

	#Show source
	#print(html)

	#Show Title
	#print('Title=%s' % (to_utf8(br.title(),)))

	#Show response headers
	#print('Response Header:\n%s' % (rs.info()))

	#Show forms
	#print('forms: %d' % (len(br.forms()),))
	n=0
	for f in br.forms():
		n+=1
		print f
	print('forms: %d' % (n,))
	
	br.select_form(nr=0)
	br.form['username'] = 'davidsu33'
	br.form['password'] = 'xxxx'
	br.submit()

	print('Submit Result:\n')
	#print br.response().read()
	save_response(br,'submit.html')

	#Save cookies
	ckjar.save(filename= COOKIE_FILE)

	rs = br.open(HOST_URL)
	save_response(br,'index.html')

	rs = br.open(BLOG_URL)
	save_response(br,'blog.html')

	rs = br.open(MY_BLOG_URL)
	save_response(br,'myblog.html')

def restore_cookie():
	br = init_browser()
	rs = br.open(HOST_URL)

	print('Cookie Login Result:\n')
	save_response(br,'rs.html')

if __name__ == '__main__':
	#restore_cookie()
	store_cookie()

你可能感兴趣的:(python,mechanize)