码迷,mamicode.com
首页 > 其他好文 > 详细

qbao

时间:2015-10-31 15:39:29      阅读:635      评论:0      收藏:0      [点我收藏+]

标签:

# -*- 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" : "1",
        "pageNum": "2",
        "pageSize" : "1",
    }
    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 : 0,
                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()

 

qbao

标签:

原文地址:http://www.cnblogs.com/hushpa/p/4925493.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!