Skip to content

Instantly share code, notes, and snippets.

@wangx2
Created April 22, 2016 12:14
Show Gist options
  • Save wangx2/23546dfd3627d06fbcbd4aba4486d5f9 to your computer and use it in GitHub Desktop.
Save wangx2/23546dfd3627d06fbcbd4aba4486d5f9 to your computer and use it in GitHub Desktop.
#! /usr/bin/python
# coding:utf-8
import urllib.request
import http.cookiejar
import re
class WebPage():
def __init__(self,url):
self.url = url
cj = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
self.shujuxinxi(self.url,opener)#调用数据编码函数
def shujuxinxi(self,url,opener):
user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36'
header = {'User-Agent':user_agent,'Refresh':'http://account.lvye.cn/accounts/login/'}
request = urllib.request.Request(self.url,None,header)
response = urllib.request.urlopen(request)
gethtml = response.read().decode('utf-8')
regx = r"<input type='hidden' name='csrfmiddlewaretoken' value='(\S+?)' />"
pipei = re.search(regx,gethtml)
if pipei:
user_info = {}
user_info['username'] = 'teshipachong'
user_info['password'] = '2FD055C53EE6830E48852D57EDF2698B'
user_info['cap_code'] = ''
user_info['auto'] = 'on'
user_info['csrfmiddlewaretoken'] = pipei.group(1)
user_info['next'] = 'http%3A%2F%2Fbbs.lvye.cn%2F'
user_info['client_id'] = '1'
user_info['state'] = 'None'
user_info['cap_key'] = ''
user_info['login_code'] = ''
#print(user_info['csrfmiddlewaretoken'])
header = {'User-Agent':user_agent,'Refresh':'http://account.lvye.cn/accounts/login/'}
data = urllib.parse.urlencode(user_info).encode(encoding='utf-8')
self.login(self.url,data,header,opener)
def login(self,url,data,header,opener):
request = urllib.request.Request(self.url,data,header)
response = opener.open(request)
wp = WebPage('http://account.lvye.cn/accounts/login/?')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment