├── Cookie.cfg ├── README.md ├── alipay.py └── alipay_new.py /Cookie.cfg: -------------------------------------------------------------------------------- 1 | [alipay] 2 | cookie = ALIPAYJSESSIONID=RZ05jON66r5SUgJJcklt1kXxDimrohauth 3 | test_time = 60 4 | get_time = 60 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Alipay 2 | 支付宝交易信息爬虫 3 | 4 | 推荐使用alipay_new,alipay代码较乱,懒得重构 5 | 6 | # 用法 7 | 8 | 配置Cookie.cgf 9 | 10 | [alipay] 11 | cookie = ALIPAYJSESSIONID=RZ05jON66r5SUgJJcklt1kXxDimrohauthRZ1 12 | test_time = 60 #定时访问支付宝 保持cookie有效性 建议60s 13 | get_time = 60 #定时获取账单信息 14 | 15 | 关于Cookie需要登录支付宝后按下F12,然后再console上面输入document.cookie,或者到开发者工具中查看HTTP报文 16 | 17 | Cookie 只需要其中一个字段: 18 | 19 | ALIPAYJSESSIONID=xxxxxxxxxxxxx 20 | 21 | 22 | # 说明 23 | alipay 可获取对方名字,手机或邮箱,金额,订单号网址,集成了qq邮件模块,可以直接调用,发送邮件: 24 | 25 | sender = '490021209@qq.com' 26 | receivers =['490021209@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱 27 | 28 | qq邮件秘钥需要申请,请自行搜索 29 | 30 | alipay_new 可获取金额,订单号,时间,使用另外的api,没有写保持cookie模块,可自行添加 31 | 32 | 结果: 33 | 34 | 姓名187****3962&30.00|https://shenghuo.alipay.com/send/queryTransferDetail.htm?tradeNo=20170221200040011100460045967033 35 | 用户名 手机号 金额 订单号信息 36 | 37 | 30.00 & 1487677389 | 20170221200040011100790046934184 38 | 金额 时间 订单号 39 | -------------------------------------------------------------------------------- /alipay.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | #可获取对方用户名,手机或邮箱,金额,订单号网址 4 | 5 | import threading 6 | import ConfigParser 7 | import sys 8 | import ConfigParser 9 | import requests 10 | from bs4 import BeautifulSoup 11 | import time 12 | import smtplib 13 | from email.mime.text import MIMEText 14 | from email.mime.multipart import MIMEMultipart 15 | from email.header import Header 16 | 17 | class alipay: 18 | def __init__(self): 19 | try: 20 | print '[+]读取cookie' 21 | config = ConfigParser.ConfigParser() 22 | config.read('Cookie.cfg') 23 | user = dict(config.items('alipay')) 24 | self.cookie= user['cookie'] 25 | self.test_time = int(user['test_time']) 26 | self.get_time = int(user['get_time']) 27 | 28 | except: 29 | print '[-]读取cookie失败' 30 | 31 | def test_online(self): 32 | url = 'https://consumeprod.alipay.com/record/standard.htm' 33 | auth_header = {'Host':'consumeprod.alipay.com', 34 | 'Referer':'https://my.alipay.com/portal/i.htm', 35 | 'Cookie':self.cookie} 36 | 37 | rsp = requests.get(url, headers=auth_header) 38 | if '登录' in rsp.text.encode('utf-8') and '注册' in rsp.text.encode('utf-8'): 39 | print '[-]cookie失效' 40 | #self.notice() #发送邮件通知你cookie失效 41 | #sys.exit() #停止运行 42 | else: 43 | print '[+]cookie有效' 44 | 45 | def keep_online(self): 46 | while(1): 47 | self.test_online() 48 | print '[+]保持登录结束 休息%d秒'%self.test_time 49 | time.sleep(self.test_time) 50 | 51 | def notice(self): 52 | sender = '490021209@qq.com' 53 | receivers =['490021209@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱 54 | 55 | #创建一个带附件的实例 56 | message = MIMEMultipart() 57 | message['From'] = Header("Hackerl", 'utf-8') 58 | message['To'] = Header("Hackerl", 'utf-8') 59 | subject = 'Cookie failed' 60 | message['Subject'] = Header(subject, 'utf-8') 61 | 62 | #邮件正文内容 63 | message.attach(MIMEText('Cookie failed', 'plain', 'utf-8')) 64 | 65 | try: 66 | smtpObj = smtplib.SMTP_SSL("smtp.qq.com", 465) 67 | smtpObj.login('490021209@qq.com','秘钥 并非qq密码 需要申请') 68 | smtpObj.sendmail(sender, receivers, message.as_string()) 69 | print "邮件发送成功" 70 | except smtplib.SMTPException: 71 | print '发送失败' 72 | 73 | def get_bill(self): 74 | print 'cookie:',self.cookie 75 | auth_header = {'Host':'consumeprod.alipay.com', 76 | 'Referer':'https://my.alipay.com/portal/i.htm', 77 | 'Cookie':self.cookie} 78 | rsp = requests.get('https://consumeprod.alipay.com/record/standard.htm', headers=auth_header) 79 | soup = BeautifulSoup(rsp.text,"html.parser") 80 | finish=[] 81 | f=open('zhifu.txt','a+') 82 | for w in f.readlines(): 83 | try: 84 | finish.append(w.split('|')[1][:-1]) 85 | except: 86 | pass 87 | result=[] 88 | for q in soup.find_all("tr",class_="J-item"): 89 | mon_num = q.find_all("span",class_="amount-pay")[0].get_text().replace('\t','').replace(' ','').replace('\n','').replace('\r','').encode('utf-8') 90 | if mon_num == '+30.00' or mon_num == '+50.00': #获取付款 30 或 50 的账单信息 可自行修改 91 | i=q.find_all("li",class_="btn-group-item",attrs={"data-target":"_blank"})[0]['data-link'] 92 | if i in finish: 93 | print 'finished:'+i 94 | else: 95 | result.append(i) 96 | zhifu=[] 97 | 98 | if '登录' not in rsp.text.encode('utf-8'): 99 | print '[+]cookie work' 100 | for url in result: 101 | rsp_url = requests.get(url, headers=auth_header) 102 | soup_url = BeautifulSoup(rsp_url.text,"html.parser") 103 | user='' 104 | for i in soup_url.find_all("div",class_="tb-border p-trade-slips"): 105 | user = i.find_all("td")[0].parent.get_text().replace('\t','').replace(' ','').replace('\n','').replace('\r','').encode('utf-8') 106 | # 107 | if '对方信息' in user: 108 | user=user.split('对方信息:')[1] 109 | mount=soup_url.find_all("td",class_="amount")[0].get_text().replace('\t','').replace(' ','').replace('\n','').replace('\r','').encode('utf-8') 110 | if '留言' in user: 111 | user=user.split('留言')[0] 112 | if '=' in mount: 113 | mount=mount[1:] 114 | zhifu.append(user+'&'+mount+'|'+url.encode('utf-8')+'\n') 115 | f.writelines(zhifu) 116 | 117 | else: 118 | print '[-]cookie faild' 119 | 120 | f.close() 121 | 122 | def get(self): 123 | while 1: 124 | self.get_bill() 125 | print '[+]获取账单结束 休息%d秒'%self.get_time 126 | time.sleep(self.get_time) 127 | 128 | def run(self): 129 | t1 = threading.Thread(target=self.get,args=()) 130 | t2 = threading.Thread(target=self.keep_online,args=()) 131 | t1.start() 132 | t2.start() 133 | 134 | if __name__ =='__main__': 135 | alipay = alipay() 136 | alipay.run() 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | -------------------------------------------------------------------------------- /alipay_new.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | #可获取金额,订单号,时间 4 | 5 | from bs4 import BeautifulSoup as bs 6 | from requests.packages.urllib3 import connectionpool 7 | import requests 8 | import time 9 | import logging 10 | import sys 11 | import ConfigParser 12 | 13 | # EDIT START 14 | class alipay: 15 | def __init__(self): 16 | try: 17 | print '[+]读取cookie' 18 | config = ConfigParser.ConfigParser() 19 | config.read('Cookie.cfg') 20 | user = dict(config.items('alipay')) 21 | self.cookie= {'ALIPAYJSESSIONID': user['cookie'].split('=')[1]} 22 | except: 23 | print '[-]读取cookie失败' 24 | 25 | 26 | # EDIT END 27 | def bili(self,soup): 28 | PaymentID = [] 29 | for i in soup.select('.consumeBizNo'): 30 | PaymentID.append(i.string.strip().encode('utf-8')) 31 | 32 | 33 | Time = [] 34 | timeFormat = '%Y-%m-%d %H:%M:%S' 35 | for i in soup.select('.time'): 36 | Time.append(int(time.mktime(time.strptime(i.string.strip(), timeFormat)))) 37 | 38 | 39 | Name = [] 40 | for i in soup.select('.emoji-li'): 41 | for ii in i.stripped_strings: 42 | Name.append(ii.encode('utf-8')) 43 | 44 | 45 | Amount = [] 46 | for i in soup.select('.amount.income'): 47 | Amount.append(i.string.encode('utf-8')) 48 | return PaymentID,Time,Name,Amount 49 | 50 | 51 | def run(self): 52 | req = requests.get('https://lab.alipay.com/consume/record/items.htm', cookies=self.cookie) 53 | if req.url.startswith('https://auth.alipay.com/'): 54 | print 'cookie失效' 55 | sys.exit(0) 56 | 57 | soup = bs(req.text, 'lxml') 58 | for i in soup.select('.amount.outlay'): 59 | i.parent.decompose() 60 | for i in soup.select('.subTransCodeValue'): 61 | i.decompose() 62 | 63 | PaymentID,Time,Name,Amount = self.bili(soup) 64 | length = len(PaymentID) 65 | 66 | f=open('zhifu.txt','a+') 67 | finish=[] 68 | for w in f.readlines(): 69 | finish.append(w.split('| ')[1][:-1]) 70 | for i in range(length): 71 | if PaymentID[i] in finish: 72 | print 'finished:'+PaymentID[i] 73 | else: 74 | print (Name[i],Amount[i],Time[i],PaymentID[i]) 75 | f.write('%s & %d | %s\n' % (Amount[i],Time[i],PaymentID[i])) 76 | time.sleep(5) 77 | if __name__ =='__main__': 78 | alipay=alipay() 79 | alipay.run() 80 | --------------------------------------------------------------------------------