# -*- coding: utf-8 -*-
# 主题 : 苏宁易购试用申请
# 版本 : 1.0(Python 3)
# 时间 : 2019.09.28
# 作者 : 池塘里的大鳄鱼
# 说明 : 需要先在59、60行输入账号密码,登录次数过多会出现滑块验证可将56-61行注释掉,选择下方63-66行手动登录方式。申请次数上限好像是50次/天。鼠标点击坐标值可能需要重设
import requests
import re
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
import win32api,win32con
from time import sleep
# 获取所有试用商品
def get_all_product_id():
urls = ['https://try.m.suning.com/mtp-web/api/index/batchCategoryProductBriefInfoByPageList/getFreeTabData1569656586954-1-101--1-1000.jsonp',
'https://try.m.suning.com/mtp-web/api/index/batchCategoryProductBriefInfoByPageList/getFreeTabData1569656734752-1-103--1-1000.jsonp',
'https://try.m.suning.com/mtp-web/api/index/batchCategoryProductBriefInfoByPageList/getFreeTabData1569656778689-1-102--1-1000.jsonp',
'https://try.m.suning.com/mtp-web/api/index/batchCategoryProductBriefInfoByPageList/getFreeTabData1569656845737-1-104--1-1000.jsonp']
product_id_list = []
for url in urls:
r = requests.get(url)
info = r.text
product_id = re.findall('(?<="productId":)(.*?)(?=,)', info)
product_id_list += product_id
return product_id_list
# 获取商品子ID
def get_child_id(driver, product_id): # 需设置火狐浏览器
url = 'https://c.m.suning.com/sy_content.html?productId=' + product_id
driver.get(url)
page = driver.page_source
child_id = re.findall('(?<=data-id=")(.*?)(?=")', page)
return child_id
# 检测已经申请过的所有商品
def cheek_exist_product(driver):
url = 'https://c.m.suning.com/sy_myApply.html?code=0'
driver.get(url)
page = driver.page_source
product_id = re.findall('(?<=data-productid=")(.*?)(?=")', page)
return product_id
# 主程序
def main():
driver = webdriver.Firefox()
driver.set_window_size(370, 740) # 设置浏览器窗口大小, 配合鼠标定位使用
# 先登陆苏宁账号
driver.get('https://passport.suning.com/ids/login?service=https://aq.suning.com/asc/auth?targetUrl=https://m.suning.com/&loginTheme=wap_new')
driver.implicitly_wait(3)
sleep(1) # 休眠1秒
driver.find_element_by_css_selector('.other_btn1').click()
driver.implicitly_wait(3)
sleep(1)
driver.find_element_by_css_selector('#username').send_keys('苏宁账户名称')
driver.find_element_by_css_selector('#password').send_keys('苏宁账户密码')
driver.find_element_by_css_selector('div.btnBox:nth-child(7) > a:nth-child(1)').click()
# # 等待登陆
# win32api.MessageBox(0, "请在网页上登录后到Python中继续下一步操作!", "登陆确认", win32con.MB_OK)
# # 等待用户登录
# input('*' * 10 + '按回车键继续操作' + '*' * 10)
print('-' * 35)
print('程序正在自动执行中...')
all_ids = get_all_product_id()
all_ids = set(all_ids)
exist_id = cheek_exist_product(driver)
exist_id = set(exist_id)
ids = all_ids - exist_id
ids = list(ids)
i = 0
# driver.get('https://c.m.suning.com/snsy.html')
for id in ids:
i += 1
print('正在执行第{}个任务'.format(i))
cid = get_child_id(driver, id)
cid = cid[0]
print(cid)
url = 'https://c.m.suning.com/sy_applyIndex.html?adTypeCode=470001&id=' + cid
driver.get(url)
sleep(1)
ActionChains(driver).move_by_offset(300, 637).click().perform() # 点击申请 不同分辨率的电脑可能需要重新设置(300, 637)这个坐标
sleep(1)
ActionChains(driver).move_by_offset(-300, -637).perform() # 滚回鼠标位置 不同分辨率的电脑可能需要重新设置(-300, -637)这个坐标
print('共执行{}个任务'.format(i))
win32api.MessageBox(0, '共执行{}个任务'.format(i), '执行完毕', win32con.MB_OK)
driver.close()
if __name__ == '__main__':
main()