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()

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值