Commit d51420d8 by Peng

no message

parent 3650247a
......@@ -19,6 +19,7 @@ class get_1688_order_data():
self.alipay_data_list = []
self.err_orders_list = [] # 有问题的订单号
self.time_num = 0
self.chrom_new_page_tab = None # 浏览器标签页,初始化为 None
def redis_db(self):
nums = 0
......@@ -40,8 +41,13 @@ class get_1688_order_data():
chrome_options.set_local_port(9333) # 设置 Chrome 的调试端口
chrom_page = ChromiumPage(addr_or_opts=chrome_options)
print(f"Chrome 浏览器运行在端口: {9333}")
self.get_order_tab(chrom_page, list_orders, orders_type) # 请求订单页面。判断是否保持登录状态
chrom_page.quit()
try:
self.get_order_tab(chrom_page, list_orders, orders_type) # 请求订单页面。判断是否保持登录状态
finally:
try:
chrom_page.quit() # 无论是否报错,都确保退出浏览器
except:
pass
def get_order_tab(self, chrom_page, list_orders, orders_type):
url = 'https://air.1688.com/app/ctf-page/trade-order-list/buyer-order-list.html?tradeStatus=waitbuyerpay&spm=a260k.home2025.topmenu.dmyorder_popup_ddaifukuan&page=1&pageSize=10'
......@@ -183,14 +189,18 @@ class get_1688_order_data():
}
}
if orders_type:
try:
json_data = json.dumps(items, ensure_ascii=False)
self.redis_db22.rpush('alipay_zszh:response', json_data)
print(f'从alipay_zszh:{self.account} redis删除订单号:', order_num)
self.redis_db22.lrem(f'alipay_zszh:{self.account}', 1, order_num)
continue
except:
pass
for retry in range(3):
try:
json_data = json.dumps(items, ensure_ascii=False)
self.redis_db22.rpush('alipay_zszh:response', json_data)
print(f'从alipay_zszh:{self.account} redis删除订单号:', order_num)
self.redis_db22.lrem(f'alipay_zszh:{self.account}', 1, order_num)
break
except Exception as e:
print(f'redis写入失败,第{retry+1}次重试: {e}')
self.redis_db22 = self.redis_db()
time.sleep(2)
continue
else:
self.alipay_data_list.append(items)
else:
......@@ -233,8 +243,8 @@ class get_1688_order_data():
print('寻找支付方式')
# 对公支付
chrom_page_tab.scroll.down(180)
time.sleep(random.randint(1, 2))
channel_name = chrom_page_tab.ele('xpath://span[contains(text(),"对公支付")]', timeout=5)
time.sleep(random.randint(2, 4))
channel_name = chrom_page_tab.ele('xpath://span[contains(text(),"对公支付")]', timeout=8)
if channel_name:
print('对公支付')
chrom_page_tab.ele('xpath://span[contains(text(),"对公支付")]', timeout=3).click()
......@@ -265,16 +275,21 @@ class get_1688_order_data():
_items = None
if _items:
if orders_type:
try:
json_data = json.dumps(_items, ensure_ascii=False)
self.redis_db22.rpush('alipay_zszh:response', json_data)
print(f'从alipay_zszh:{self.account} redis删除订单号:', order_num)
self.redis_db22.lrem(f'alipay_zszh:{self.account}', 1, order_num)
except:
pass
for retry in range(3):
try:
json_data = json.dumps(_items, ensure_ascii=False)
self.redis_db22.rpush('alipay_zszh:response', json_data)
print(f'从alipay_zszh:{self.account} redis删除订单号:', order_num)
self.redis_db22.lrem(f'alipay_zszh:{self.account}', 1, order_num)
break
except Exception as e:
print(f'redis写入失败,第{retry+1}次重试: {e}')
self.redis_db22 = self.redis_db()
time.sleep(2)
else:
self.alipay_data_list.append(_items)
self.save_redis()
chrom_page_tab.close() # 关闭标签页,防止资源泄漏
continue
if orders_type is None:
print(', 点击网银')
......@@ -311,106 +326,84 @@ class get_1688_order_data():
chrom_page_tab.ele('xpath://div[contains(text(),"专属账号余额:")]', timeout=3).click()
except:
pass
time.sleep(random.randint(1, 3))
if chrom_page_tab.ele('xpath://div[contains(text(),"专属账号余额不足")]'):
resp = chrom_page_tab.html
respons = etree.HTML(resp)
sapn_text_list = respons.xpath('//div[contains(text(),"专属账号余额不足")]//text()')
if sapn_text_list:
span_str = ''.join(sapn_text_list)
else:
span_str = '专属账号余额不足,可用余额: ¥'
print('span_str:::', span_str)
items = {
"account": self.account,
"order_id": order_num,
"type": "异常",
"json": {
"value": span_str
}
}
print('专属账号余额支付 items:::', items)
json_data = json.dumps(items, ensure_ascii=False)
self.redis_db22.rpush('alipay_zszh:response', json_data)
else:
print('开始付款')
try:
time.sleep(random.randint(1, 3))
if self.click_pay(chrom_page_tab, order_num):
items_err = {
if chrom_page_tab.ele('xpath://div[contains(text(),"专属账号余额不足")]'):
resp = chrom_page_tab.html
respons = etree.HTML(resp)
sapn_text_list = respons.xpath('//div[contains(text(),"专属账号余额不足")]//text()')
if sapn_text_list:
span_str = ''.join(sapn_text_list)
else:
span_str = '专属账号余额不足,可用余额: ¥'
print('span_str:::', span_str)
items = {
"account": self.account,
"order_id": order_num,
"type": "异常",
"json": {
"value": '系统繁忙,请重试'
"value": span_str
}
}
json_data = json.dumps(items_err, ensure_ascii=False)
print('专属账号余额支付 items:::', items)
json_data = json.dumps(items, ensure_ascii=False)
self.redis_db22.rpush('alipay_zszh:response', json_data)
else:
if '支付成功' in chrom_page_tab.html:
print('专属账号余额 支付成功 :', order_num)
else:
items = {
print('开始付款')
time.sleep(random.randint(1, 3))
if self.click_pay(chrom_page_tab, order_num):
items_err = {
"account": self.account,
"order_id": order_num,
"type": "异常",
"json": {
"value": '需要手动确认,专属账号余额是否足够支付'
"value": '系统繁忙,请重试'
}
}
print('专属账号余额支付 items:::', items)
json_data = json.dumps(items, ensure_ascii=False)
json_data = json.dumps(items_err, ensure_ascii=False)
self.redis_db22.rpush('alipay_zszh:response', json_data)
try:
print(f'从alipay_zszh:{self.account} redis删除订单号:', order_num)
self.redis_db22.lrem(f'alipay_zszh:{self.account}', 1, order_num)
except:
print('删除失败:')
pass
chrom_page_tab.close() # 关闭标签页
else:
if '支付成功' in chrom_page_tab.html:
print('专属账号余额 支付成功 :', order_num)
else:
items = {
"account": self.account,
"order_id": order_num,
"type": "异常",
"json": {
"value": '需要手动确认,专属账号余额是否足够支付'
}
}
print('专属账号余额支付 items:::', items)
json_data = json.dumps(items, ensure_ascii=False)
self.redis_db22.rpush('alipay_zszh:response', json_data)
try:
print(f'从alipay_zszh:{self.account} redis删除订单号:', order_num)
self.redis_db22.lrem(f'alipay_zszh:{self.account}', 1, order_num)
except:
print('删除失败:')
pass
except Exception as e:
print(f'专属账号支付 redis 操作失败: {e}')
if 'connection' in str(e).lower() or 'timeout' in str(e).lower():
print('Redis 连接失败,重新初始化...')
self.redis_db22 = self.redis_db()
finally:
chrom_page_tab.close() # 无论如何都关闭标签页
continue
else:
zfb = chrom_page_tab.ele('xpath://span[contains(text(),"支付宝")]', timeout=3).click()
if zfb:
print('找到支付宝。开始选择支付宝')
if self.click_pay(chrom_page_tab, order_num):
items_err = {
"account": self.account,
"order_id": order_num,
"type": "异常",
"json": {
"value": '系统繁忙,请重试'
}
}
self.alipay_data_list.append(items_err)
else:
self.online_zfb(chrom_page_tab, order_num)
else:
print('没有找到可以支付方式。退出 订单号。', order_num)
items = {
"account": self.account,
"order_id": order_num,
"type": "无",
"json": {
"value": None
}
items_err = {
"account": self.account,
"order_id": order_num,
"type": "异常",
"json": {
"value": '没有对公支付'
}
self.alipay_data_list.append(items)
}
self.alipay_data_list.append(items_err)
chrom_page_tab.close() # 关闭标签页
self.save_redis()
# def save_alipay_zszh(self,_items, order_num):
# self.redis_db()
# for i in range(5):
# try:
# json_data = json.dumps(_items, ensure_ascii=False)
# self.redis_db22.rpush('alipay_zszh:response', json_data)
# print(f'从alipay_zszh:{self.account} redis删除订单号:', order_num)
# self.redis_db22.lrem(f'alipay_zszh:{self.account}', 1, order_num)
# break
# except:
# time.sleep(10)
def save_redis(self):
if self.alipay_data_list:
for alipay_data in self.alipay_data_list:
......@@ -422,8 +415,8 @@ class get_1688_order_data():
break
except:
print('写入redis报错:重试')
self.redis_db22 = self.redis_db() # 重连 redis
time.sleep(5)
# 删除
try:
print('从redis删除订单号:', alipay_data['order_id'])
self.redis_db22.lrem(f"alipay:{self.account}", 1, f"{alipay_data['order_id']}")
......@@ -560,8 +553,19 @@ class get_1688_order_data():
self.get_alipay_zszh()
except Exception as e:
print("报错xx2222xxx:", e, f"\n{traceback.format_exc()}")
# 检测到 redis 连接失败,重新初始化
if 'redis' in str(e).lower() or 'connection' in str(e).lower() or 'timeout' in str(e).lower():
print('Redis 连接失败,重新初始化...')
self.redis_db22 = self.redis_db()
time.sleep(300)
self.chrom_new_page_tab.close()
# 安全关闭浏览器标签页
if hasattr(self, 'chrom_new_page_tab') and self.chrom_new_page_tab:
try:
self.chrom_new_page_tab.close()
except:
pass
self.err_orders_list = [] # 异常时也清空,防止累积
continue # 继续下一轮循环,不退出
base_min, base_max = 20, 60
sleep_min = base_min + self.time_num * 2
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment