1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
__author__ = 'CQC'
import urllib import urllib2 import cookielib import re import webbrowser import tool
class Taobao:
def __init__(self): self.loginURL = "https://login.taobao.com/member/login.jhtml" self.proxyURL = 'http://120.193.146.97:843' self.loginHeaders = { 'Host':'login.taobao.com', 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0', 'Referer' : 'https://login.taobao.com/member/login.jhtml', 'Content-Type': 'application/x-www-form-urlencoded', 'Connection' : 'Keep-Alive' } self.username = 'cqcre' self.ua = '191UW5TcyMNYQwiAiwTR3tCf0J/QnhEcUpkMmQ=|Um5Ockt0TXdPc011TXVKdyE=|U2xMHDJ+H2QJZwBxX39Rb1d5WXcrSixAJ1kjDVsN|VGhXd1llXGNaYFhkWmJaYl1gV2pIdUtyTXRKfkN4Qn1FeEF6R31TBQ==|VWldfS0TMw8xDjYWKhAwHiUdOA9wCDEVaxgkATdcNU8iDFoM|VmNDbUMV|V2NDbUMV|WGRYeCgGZhtmH2VScVI2UT5fORtmD2gCawwuRSJHZAFsCWMOdVYyVTpbPR99HWAFYVMpUDUFORshHiQdJR0jAT0JPQc/BDoFPgooFDZtVBR5Fn9VOwt2EWhCOVQ4WSJPJFkHXhgoSDVIMRgnHyFqQ3xEezceIRkmahRqFDZLIkUvRiEDaA9qQ3xEezcZORc5bzk=|WWdHFy0TMw8vEy0UIQE0ADgYJBohGjoAOw4uEiwXLAw2DThuOA==|WmBAED5+KnIbdRh1GXgFQSZbGFdrUm1UblZqVGxQa1ZiTGxQcEp1I3U=|W2NDEz19KXENZwJjHkY7Ui9OJQsre09zSWlXY1oMLBExHzERLxsuE0UT|XGZGFjh4LHQdcx5zH34DRyBdHlFtVGtSaFBsUmpWbVBkSmpXd05zTnMlcw==|XWdHFzl5LXUJYwZnGkI/VitKIQ8vEzMKNws3YTc=|XmdaZ0d6WmVFeUB8XGJaYEB4TGxWbk5yTndXa0tyT29Ta0t1QGBeZDI=' self.password2 = '7511aa6854629e45de220d29174f1066537a73420ef6dbb5b46f202396703a2d56b0312df8769d886e6ca63d587fdbb99ee73927e8c07d9c88cd02182e1a21edc13fb8e140a4a2a4b53bf38484bd0e08199e03eb9bf7b365a5c673c03407d812b91394f0d3c7564042e3f2b11d156aeea37ad6460118914125ab8f8ac466f' self.post = post = { 'ua':self.ua, 'TPL_checkcode':'', 'CtrlVersion': '1,0,0,7', 'TPL_password':'', 'TPL_redirect_url':'http://i.taobao.com/my_taobao.htm?nekot=udm8087E1424147022443', 'TPL_username':self.username, 'loginsite':'0', 'newlogin':'0', 'from':'tb', 'fc':'default', 'style':'default', 'css_style':'', 'tid':'XOR_1_000000000000000000000000000000_625C4720470A0A050976770A', 'support':'000001', 'loginType':'4', 'minititle':'', 'minipara':'', 'umto':'NaN', 'pstrong':'3', 'llnick':'', 'sign':'', 'need_sign':'', 'isIgnore':'', 'full_redirect':'', 'popid':'', 'callback':'', 'guf':'', 'not_duplite_str':'', 'need_user_id':'', 'poy':'', 'gvfdcname':'10', 'gvfdcre':'', 'from_encoding ':'', 'sub':'', 'TPL_password_2':self.password2, 'loginASR':'1', 'loginASRSuc':'1', 'allp':'', 'oslanguage':'zh-CN', 'sr':'1366*768', 'osVer':'windows|6.1', 'naviVer':'firefox|35' } self.postData = urllib.urlencode(self.post) self.proxy = urllib2.ProxyHandler({'http':self.proxyURL}) self.cookie = cookielib.LWPCookieJar() self.cookieHandler = urllib2.HTTPCookieProcessor(self.cookie) self.opener = urllib2.build_opener(self.cookieHandler,self.proxy,urllib2.HTTPHandler) self.J_HToken = '' self.newCookie = cookielib.CookieJar() self.newOpener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.newCookie)) self.tool = tool.Tool()
def needCheckCode(self): request = urllib2.Request(self.loginURL,self.postData,self.loginHeaders) response = self.opener.open(request) content = response.read().decode('gbk') status = response.getcode() if status == 200: print u"获取请求成功" pattern = re.compile(u'\u8bf7\u8f93\u5165\u9a8c\u8bc1\u7801',re.S) result = re.search(pattern,content) if result: print u"此次安全验证异常,您需要输入验证码" return content else: tokenPattern = re.compile('id="J_HToken" value="(.*?)"') tokenMatch = re.search(tokenPattern,content) if tokenMatch: self.J_HToken = tokenMatch.group(1) print u"此次安全验证通过,您这次不需要输入验证码" return False else: print u"获取请求失败" return None
def getCheckCode(self,page): pattern = re.compile('<img id="J_StandardCode_m.*?data-src="(.*?)"',re.S) matchResult = re.search(pattern,page) if matchResult and matchResult.group(1): return matchResult.group(1) else: print u"没有找到验证码内容" return False
def loginWithCheckCode(self): checkcode = raw_input('请输入验证码:') self.post['TPL_checkcode'] = checkcode self.postData = urllib.urlencode(self.post) try: request = urllib2.Request(self.loginURL,self.postData,self.loginHeaders) response = self.opener.open(request) content = response.read().decode('gbk') pattern = re.compile(u'\u9a8c\u8bc1\u7801\u9519\u8bef',re.S) result = re.search(pattern,content) if result: print u"验证码输入错误" return False else: tokenPattern = re.compile('id="J_HToken" value="(.*?)"') tokenMatch = re.search(tokenPattern,content) if tokenMatch: print u"验证码输入正确" self.J_HToken = tokenMatch.group(1) return tokenMatch.group(1) else: print u"J_Token获取失败" return False except urllib2.HTTPError, e: print u"连接服务器出错,错误原因",e.reason return False
def getSTbyToken(self,token): tokenURL = 'https://passport.alipay.com/mini_apply_st.js?site=0&token=%s&callback=stCallback6' % token request = urllib2.Request(tokenURL) response = urllib2.urlopen(request) pattern = re.compile('{"st":"(.*?)"}',re.S) result = re.search(pattern,response.read()) if result: print u"成功获取st码" st = result.group(1) return st else: print u"未匹配到st" return False
def loginByST(self,st,username): stURL = 'https://login.taobao.com/member/vst.htm?st=%s&TPL_username=%s' % (st,username) headers = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0', 'Host':'login.taobao.com', 'Connection' : 'Keep-Alive' } request = urllib2.Request(stURL,headers = headers) response = self.newOpener.open(request) content = response.read().decode('gbk') pattern = re.compile('top.location = "(.*?)"',re.S) match = re.search(pattern,content) if match: print u"登录网址成功" location = match.group(1) return True else: print "登录失败" return False
def getGoodsPage(self,pageIndex): goodsURL = 'http://buyer.trade.taobao.com/trade/itemlist/listBoughtItems.htm?action=itemlist/QueryAction&event_submit_do_query=1' + '&pageNum=' + str(pageIndex) response = self.newOpener.open(goodsURL) page = response.read().decode('gbk') return page
def getAllGoods(self,pageNum): print u"获取到的商品列表如下" for x in range(1,int(pageNum)+1): page = self.getGoodsPage(x) self.tool.getGoodsInfo(page)
def main(self): needResult = self.needCheckCode() if not needResult ==None: if not needResult == False: print u"您需要手动输入验证码" checkCode = self.getCheckCode(needResult) if not checkCode == False: print u"验证码获取成功" print u"请在浏览器中输入您看到的验证码" webbrowser.open_new_tab(checkCode) self.loginWithCheckCode() else: print u"验证码获取失败,请重试" else: print u"不需要输入验证码" else: print u"请求登录页面失败,无法确认是否需要验证码"
if not self.J_HToken: print "获取Token失败,请重试" return st = self.getSTbyToken(self.J_HToken) result = self.loginByST(st,self.username) if result: page = self.getGoodsPage(1) pageNum = self.tool.getPageNum(page) self.getAllGoods(pageNum) else: print u"登录失败"
taobao = Taobao() taobao.main()
|
评论(0)