# -*- coding: utf-8 -*-
import Image, cStringIO, webbrowser, re, time, math
import urllib, urllib2, cookielib
from HTMLParser import HTMLParser
import sys
reload(sys)
sys.setdefaultencoding('utf8')
class WebHelper:
def __init__(self):
self.cookie = cookielib.CookieJar()
handler = urllib2.HTTPCookieProcessor(self.cookie)
opener = urllib2.build_opener(handler)
urllib2.install_opener(opener)
def open(self, path, req\_header = None, pd = None):
try:
req = urllib2.Request(path, pd, req\_header)
res = urllib2.urlopen(req)
return res
except urllib2.HTTPError, e:
print e.code
return None
def showCookie(self):
for item in self.cookie:
print 'Name = ' + item.name
print 'value = ' + item.value
def showResponse(self):
content = self.response.read()
return content
def showCaptcha(url, header):
#print url
#file = cStringIO.StringIO(c.open(url, header).read())
file = c.open(url, header).read()
local = open("E:\\py_work\\c.jpg", "wb")
local.write(file)
local.close()
img = Image.open("E:\\py_work\\c.jpg")
img.show()
#webbrowser.open\_new\_tab(url)
def syscState():
req_header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0',
'Accept-Language':'en-US,en;q=0.5',
'Accept': '*/*',
'Connection':'keep-alive',
'Host':'bc.qbao.com',
'Referer': 'http://user.qbao.com/syncLoginState.jsp',
}
rlt = c.open("http://bc.qbao.com/account/ucIndex.html", req_header)
print '-------------after sync-------------'
print c.cookie
def login(user, passwd):
req\_header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,\*/\*;q=0.8',
'Accept-Language':'en-US,en;q=0.5',
#'Accept-Encoding':'gzip, deflate',
'Connection':'keep-alive',
'Host':'passport.qbao.com',
}
rlt = c.open("https://passport.qbao.com/cas/qianbaoLogin", req\_header)
print '-------------befor login-------------'
print c.cookie
html = rlt.read()
pos = html.find("/cas/captcha.html?pid=");
html = html\[pos:\]
img\_url = html\[:html.find('\\"')\]
showCaptcha("https://passport.qbao.com" + img\_url, req\_header)
captcha = raw\_input("Input captcha !")
pos = html.find("name=\\"lt\\"");
lt = html\[pos+17: pos+17+76\]
pos = html.find("name=\\"s\_uuid\\"");
uuid = html\[pos+21: pos+21+36\]
pos = html.find("name=\\'cas\_signed\\'");
cas\_signed = html\[pos+25: pos+25+36\]
login\_path = "https://passport.qbao.com/cas/qianbaoLogin"
post\_data = {
"username" : user,
"password" : passwd,
"j\_captcha\_response" : captcha,
"lt" : lt,
"\_eventId" : "submit",
"s\_uuid" : uuid,
"cas\_signed" : cas\_signed,
}
#print post\_data
pd = urllib.urlencode(post\_data)
rlt = c.open(login\_path, req\_header, pd)
html = rlt.read()
if html.find("用户登录") >= 0:
print "Login failed!"
else:
print "Login success!"
print html
print '-------------After login-------------'
print c.cookie
syscState()
def getSharedUrl():
print c.cookie
req_header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language':'en-US,en;q=0.5',
#'Accept-Encoding':'gzip, deflate',
'Connection':'keep-alive',
'Host':'bc.qbao.com',
}
post_data = {
"state" : "",
"pageNum": "",
"pageSize" : "",
}
pd = urllib.urlencode(post_data)
res = c.open("http://bc.qbao.com/listProduct2.html", req_header, pd)
html = res.read()
#print html
patt = re.compile("(?<=\"id\":)\d*")
ids = patt.findall(html)
print html
print '-------------after get product list-------------'
print c.cookie
req\_header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0',
'X-Requested-With' : 'XMLHttpRequest',
'Referer' : '',
'Accept':'application/json, text/javascript, \*/\*; q=0.01',
'Accept-Language':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
#'Accept-Encoding':'gzip, deflate',
'Connection':'keep-alive',
'Host':'bc.qbao.com',
'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
'Pragma': 'no-cache',
'Cache-Control' : 'no-cache',
}
for id in ids:
time.sleep(1)
prod\_url = "http://bc.qbao.com/getShareInfo.html"
pos\_data = {
"productId" : id,
"r" : "8.100113228429109"
}
req\_header\['Referer'\] = "http://bc.qbao.com/tuan/" + id + ".html"
print req\_header
pd = urllib.urlencode(post\_data)
res = c.open(prod\_url, req\_header, pd)
html = res.read()
print html
print '-------------befor get Share info-------------'
print c.cookie
def visitUser():
url = 'http://user.qbao.com/usercenter/ucIndex.html'
req_header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
#'Accept-Encoding':'gzip, deflate',
'Connection':'keep-alive',
'Host':'user.qbao.com',
}
res = c.open(url, req_header)
html = res.read()
print html
def visitHome():
req_header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
#'Accept-Encoding':'gzip, deflate',
'Connection':'keep-alive',
'Host':'www.qbao.com',
}
res = c.open("http://www.qbao.com/index.html", req_header)
c = WebHelper()
visitHome()
login("XXX", "XXX")
#visitUser()
getSharedUrl()
手机扫一扫
移动阅读更方便
你可能感兴趣的文章