python爬取支付宝账单

本文介绍了一种使用Selenium和ChromeDriver从支付宝获取账单信息,并将其存储到数据库的方法。通过逐步解析网页元素,实现了自动化登录、账单数据抓取及数据处理,最后将数据插入数据库中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1、获取支付宝账单信息并存入数据库

from selenium import webdriver
from DbHandle import DbHandle
import time

# 登录 url
Login_Url = 'https://auth.alipay.com/login/index.htm?goto=https%3A%2F%2Fwww.alipay.com%2F'
# 账单 url
Bill_Url = 'https://consumeprod.alipay.com/record/standard.htm'

class Alipay_Bill_Info(object):

    def __init__(self,user, passwd):
        self.user = user
        self.passwd = passwd

    def wait_input(self, ele, str):
        for i in str:
            ele.send_keys(i)
            time.sleep(0.5)

    def get_data(self):
        # 初始化浏览器对象
        sel = webdriver.Chrome()
        sel.maximize_window()
        sel.get(Login_Url)
        sel.implicitly_wait(3)
        time.sleep(1)
        # 找到用户名字输入框
        sel.find_element_by_xpath('//*[@data-status="show_login"]').click()
        uname = sel.find_element_by_id('J-input-user')
        uname.clear()
        print('正在输入账号.....')
        self.wait_input(uname, self.user)
        time.sleep(1)
        # 找到密码输入框
        upass = sel.find_element_by_id('password_rsainput')
        upass.clear()
        print('正在输入密码....')
        self.wait_input(upass, self.passwd)
        # 找到登录按钮
        time.sleep(1)
        butten = sel.find_element_by_id('J-login-btn')
        butten.click()

        # sel.save_screenshot('2.png')
        print(sel.current_url)
        # 跳转到账单页面
        print('正在跳转页面....')
        sel.get(Bill_Url)
        sel.implicitly_wait(3)
        time.sleep(2)
        try:
            sel.find_element_by_xpath('//*[@id="J-item-1"]')
        except:
            print("登录出错!")
            sel.close()
            return
        db = DbHandle()
        sql = "SELECT set_date FROM ali_pay_bill ORDER BY bid DESC LIMIT 1"
        print(sql)
        dateline = db.select_one(sql)
        result = dateline['set_date']
        result = result.replace('.', '')
        result = result.replace('-', '')
        result = result.replace(':', '')
        dateline = int(result[2:])
        print(dateline)
        for num in range(10):
            num = num + 1
            al_day_xpath = '//*[@id="J-item-' + str(num) + '"]/td[1]/p[1]'      #日期
            al_time_xpath = '//*[@id="J-item-' + str(num) + '"]/td[1]/p[2]'     #时刻
            al_way_xpath = '//*[@id="J-item-' + str(num) + '"]/td[3]/p[1]'      #方式
            al_payee_xpath = '//*[@id="J-item-' + str(num) + '"]/td[5]/p[1]'    #收款人
            al_snum_xpath = '//*[@id="J-item-' + str(num) + '"]/td[4]/p[1]'     #流水号
            al_figure_xpath = '//*[@id="J-item-' + str(num) + '"]/td[6]/span'   #金额
            al_status_xpath = '//*[@id="J-item-' + str(num) + '"]/td[8]/p[1]'   #交易状态
            al_day = sel.find_element_by_xpath(al_day_xpath).text
            al_time = sel.find_element_by_xpath(al_time_xpath).text
            al_way = sel.find_element_by_xpath(al_way_xpath).text
            al_payee = sel.find_element_by_xpath(al_payee_xpath).text
            al_snum = sel.find_element_by_xpath(al_snum_xpath).text
            al_figure = sel.find_element_by_xpath(al_figure_xpath).text
            al_status = sel.find_element_by_xpath(al_status_xpath).text
            single_data = self.handle_text(al_day,al_time,al_way,al_payee,al_snum,al_figure,al_status)
            print(single_data)
            if(single_data['set_time']<=dateline):
                break
            sql = "INSERT INTO ali_pay_bill (set_date, \
                   other, amount, sign,statu,way,code) \
                   VALUES ('%s', '%s', %s, %s, %s, '%s', '%s' )" % \
                  (single_data['set_date'], single_data['other'], single_data['amount'], \
                   single_data['sign'], single_data['statu'], single_data['way'], single_data['code'])
            print(sql)
            db.update(sql)
        sel.close()

    def handle_text(self,set_date,set_time,way,payee,snum,figure,status):
        statu = 0
        sign = 2
        amount = 0
        if '成功' in status:
            statu = 1
        if '+' in figure:
            sign = 1
            amount = float(figure[2:])
        elif '-' in figure:
            sign = 0
            amount = float(figure[2:])
        al_day = set_date+"-"+set_time
        al_time = set_time.replace(':', '')
        al_time = int((set_date+al_time).replace('.','')[2:])
        return {'set_date':al_day,'set_time':al_time,'other':payee,'amount':amount,'sign':sign,'statu':statu,'way':way,'code':snum}

 2、主类

from GetAlipayBill import Alipay_Bill_Info
import time
from datetime import datetime

# 登录用户名和密码
USERNMAE = ''
PASSWD = ''

while 1==1:
    print("---------------请求时间" + datetime.now().strftime('%Y-%m-%d %H:%M:%S') + "---------------------")
    get = Alipay_Bill_Info(USERNMAE,PASSWD)
    get.get_data()
    time.sleep(60*10)

具体参考:https://www.v2ex.com/t/383179

<think>好的,我现在要解决用户的问题:如何在bash命令行中调用Lua函数并传递参数。首先,我需要回顾下用户提供的引用内容,看看有没有相关的线索。 用户提供的引用[2]中有Lua脚本的例子,里面有两个函数luaExecute和luaPipe,它们通过os.execute和io.popen调用shell脚本。这说明在Lua中执行外部命令是可行的,但现在的问题相反,是要在bash中调用Lua函数,所以可能需要反过来处理。 引用[3]和[4]提到C++、Java与Lua的交互,但用户的问题是关于bash和Lua的,所以可能需要另种方法。通常,在命令行中调用Lua脚本并传递参数,可以通过Lua的解释器来实现。比如,执行lua脚本时传入参数,然后在脚本内部解析这些参数。 用户可能需要的是在bash中执行Lua脚本,并让这个脚本调用特定的函数,同时传递参数给这个函数。例如,在bash中运行命令如lua script.lua arg1 arg2,然后在script.lua中获取这些参数,并调用相应的函数处理。 但用户特别提到要调用Lua函数并传参,可能需要更直接的方式。可能需要通过命令行参数传递函数名和参数,然后在Lua脚本中根据这些参数动态调用函数。或者,使用Lua的交互模式,直接执行函数调用。 另外,考虑到Lua的全局环境,可以在脚本中定义函数,然后通过命令行参数来触发函数执行。例如,使用-e选项在命令行执行代码片段,比如lua -e 'dofile("script.lua"); myfunc(arg[1])'。但需要确保参数正确传递。 还需要考虑参数的类型转换,因为从bash传递的都是字符串,Lua中可能需要转换为数字或其他类型。此外,错误处理也很重要,比如函数不存在或参数错误的情况。 可能需要分步骤:首先,在Lua脚本中定义函数;然后,通过命令行参数传递函数名和参数;最后,在脚本中解析参数并调用对应的函数。例如,使用lua script.lua functionName param1 param2,然后在脚本中获取arg[1]作为函数名,arg[2]和之后的作为参数,再使用_G[functionName](unpack(params))来调用。 还要注意作用域的问题,比如函数是否在全局环境中可见。如果函数是local的,可能无法通过这种方式调用,所以需要将函数定义为全局的。 总结可能的解决方案:通过命令行参数传递函数名和参数,在Lua脚本中解析这些参数,并调用相应的函数。或者使用Lua的交互式命令直接在bash中执行函数调用。需要确保参数正确传递和处理,同时处理可能的错误情况。</think>在Bash中调用Lua函数并传递参数可通过以下步骤实现: ### 、基础调用方法 1. **编写Lua脚本**(如`demo.lua`),定义全局函数: ```lua function add(a, b) return tonumber(a) + tonumber(b) end -- 通过命令行参数调用函数 local func_name = arg[1] local args = {arg[2], arg[3]} if _G[func_name] then print(_G[func_name](unpack(args))) else error("Function not found") end ``` 2. **通过Bash调用**: ```bash lua demo.lua add 5 3 # 输出8 ``` 这里的`arg[1]`对应函数名,后续参数作为函数输入[^3][^2] ### 二、进阶参数传递 使用`-l`加载库和`-e`执行代码: ```bash lua -l demo -e "print(add(7, 2))" ``` 需先编译Lua模块或设置`LUA_PATH`环境变量[^4] ### 三、多参数处理 处理可变数量参数时建议使用表结构: ```lua function sum(...) local total = 0 for _,v in ipairs({...}) do total = total + tonumber(v) end return total end ``` 调用方式: ```bash lua demo.lua sum 1 2 3 4 # 输出10 ``` ### 四、错误处理 建议添加参数校验: ```lua if #arg < 3 then io.stderr:write("Usage: lua script.lua [function] [args...]\n") os.exit(1) end ```
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值