下面是我实操的实例代码。
# coding:utf-8
import datetime
import math
import random
import re
import shutil
from colorama import Fore, init
from api import get_tiiktok_anti2
import api
import cv2
import numpy as np
import requests
from urllib.parse import urlparse
import sys
import os
# 获取当前文件的目录
from selenium.webdriver import ActionChains
import cooke_path_1
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
import json
# 获取当前文件的目录
from douyin_config import DouYinConfig
from req_login import AES_Decrypt
cur_path = os.path.abspath(os.path.dirname(__file__))
sys.path.append(cur_path)
cookie_path = cooke_path_1.get_cooke_path_1_resetapp()
get_chromedriver_path = cooke_path_1.get_chromedriver_path()
class UpDataSlide:
def __init__(self, page_max, product_id=0):
self.product_id = product_id
self.get_cookie()
self.page_max = page_max + 1
self.page_no = 1
self.page_size = 200
self.detail_sleep_time = 1
self.debug = 0
self.begin_date = (datetime.datetime.now() - datetime.timedelta(days=1)).strftime('%Y/%m/%d')
self.end_date = (datetime.datetime.now() - datetime.timedelta(days=1)).strftime('%Y/%m/%d')
self.headers = {}
self.get_s_v_web_id()
if len(str(self.product_id))<10:
self.up_app_aity()
else:
self.up_h5_aity()
def get_cookie(self):
# 读取加载以后的cookie
result = os.popen(cur_path + '\\' + 'tiktok_up_key.exe "{}"'.format(cookie_path)).read()
cookies = eval(str(result))
cookies_exe_dict = {}
for cookie in cookies:
cookies_exe_dict[cookie['name']] = cookie['value']
self.cookies = cookies_exe_dict
self.msToken = self.cookies['msToken']
def get_s_v_web_id_2(self):
result = os.popen(cur_path + '\\' + 'tiktok_up_key.exe "{}"'.format(cookie_path)).read()
cookies = eval(str(result))
cookies_exe_dict = {}
for cookie in cookies:
cookies_exe_dict[cookie['name']] = cookie['value']
self.cookies = cookies_exe_dict
self.s_v_web_id = self.cookies['s_v_web_id']
return self.s_v_web_id
def get_s_v_web_id(self):
if (os.path.exists('s_v_web_id.json')):
with open('s_v_web_id.json', 'r', encoding='utf-8') as f:
cookies = json.load(f)
cookies_exe_dict = {}
for cookie in cookies:
cookies_exe_dict[cookie['name']] = cookie['value']
self.s_v_web_id = cookies_exe_dict['s_v_web_id']
return self.s_v_web_id
else:
# 读取加载以后的cookie
result = os.popen(cur_path + '\\' + 'tiktok_up_key.exe "{}"'.format(cookie_path)).read()
cookies = eval(str(result))
cookies_exe_dict = {}
for cookie in cookies:
cookies_exe_dict[cookie['name']] = cookie['value']
self.cookies = cookies_exe_dict
self.s_v_web_id = self.cookies['s_v_web_id']
return self.s_v_web_id
def get_ms_token(self):
# 读取加载以后的cookie
if (os.path.exists('s_v_web_id.json')):
with open('s_v_web_id.json', 'r', encoding='utf-8') as f:
cookies = json.load(f)
cookies_exe_dict = {}
for cookie in cookies:
cookies_exe_dict[cookie['name']] = cookie['value']
self.msToken = cookies_exe_dict['msToken']
self.cookies = cookies_exe_dict
else:
# 读取加载以后的cookie
try:
result = os.popen('tiktok_up_key.exe "{}"'.format(cookie_path)).read()
cookies = eval(str(result))
cookies_exe_dict = {}
for cookie in cookies:
cookies_exe_dict[cookie['name']] = cookie['value']
self.cookies = cookies_exe_dict
self.msToken = self.cookies['msToken']
except Exception as e:
#wphwph
exit(1)
return self.msToken
def get_ms_token_2(self):
# 读取加载以后的cookie
try:
result = os.popen('tiktok_up_key.exe "{}"'.format(cookie_path)).read()
cookies = eval(str(result))
cookies_exe_dict = {}
for cookie in cookies:
cookies_exe_dict[cookie['name']] = cookie['value']
self.cookies = cookies_exe_dict
self.msToken = self.cookies['msToken']
except Exception as e:
#wphwph
exit(1)
return self.msToken
def get_xb(self, fapi):
xb = api.get_tiiktok_anti(fapi)
return xb
def up_h5_aity(self):
for i in range(0, 15000):
start = 1
product_id = "3614617508616787316"
if start == 1:
p_url = 'https://采集网址q258599831/api/goods/uploadGoodsDetail?type=h5'
print(f"睡眠0.1秒请求产品详情")
time.sleep(0.1)
res = requests.post(p_url, json={}).text
res_data = json.loads(res)
product_id = str(res_data["data"]["product_id"])
res_h5 = str(res_data["data"]["h5"])
res_app = str(res_data["data"]["app"])
print(res_data)
print(product_id)
if len(product_id) < 7:
print(f"今天H5数据已经采集完毕,睡眠6秒会结束")
time.sleep(6)
exit(1)
break
else:
verifyFp = self.get_s_v_web_id()
msToken = self.get_ms_token()
print(msToken)
print(verifyFp)
# api_post_data = "promotion_ids={}&use_new_price=1&isFromVideo=false&is_in_app=0"
api_url = "is_h5=1&is_native_h5=1&verifyFp={}&msToken={}"
api_post_data = "use_new_price=1&is_h5=1&bff_type=2&is_in_app=0&origin_type=&promotion_ids={}&meta_param=&source_page=&request_additions=&isFromVideo=false"
fapi_url = api_url.format(verifyFp, msToken)
fapi_post_data = api_post_data.format(product_id)
xb = get_tiiktok_anti2(fapi_url, fapi_post_data)
url = "https://采集网站例如q258599831/aweme/v2/shop/promotion/pack/?" + xb
print(url)
headers = {
'Host': 'ecom.q258599831.com',
'Connection': 'keep-alive',
'Content-Length': '550',
'sec-ch-ua': DouYinConfig.sec_ch_ua,
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/x-www-form-urlencoded',
'sec-ch-ua-mobile': '?0',
'User-Agent': DouYinConfig.User_Agent,
'sec-ch-ua-platform': '"Windows"',
'Origin': 'https://haohuo.q258599831.com',
'Sec-Fetch-Site': 'cross-site',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Dest': 'empty',
'Referer': 'https://haohuo.q258599831.com/',
'Accept-Language': 'zh-CN,zh;q=0.9',
}
try:
response = requests.request("POST", url, headers=headers, data=fapi_post_data, timeout=5)
dd = json.loads(response.text)
print('success')
except requests.exceptions.RequestException as e:
print(e)
if DouYinConfig.if_reboot_router == "1":
path = os.path.join(os.getcwd(), 'selenium_chrome', 'rebootRouterNow.exe')
anti = " ".join([path, ' '])
os.popen(anti).read()
time.sleep(120)
print("睡眠120秒,从新启动路由器")
continue
else:
time.sleep(1800)
continue
############ 请求产品h5详情 ############
json_str = json.dumps(dd)
print(json_str)
############ 请求产品h5详情 ############
# z_spec_info = {"spec_detail_id":0,"effective_price":0,"name":"","img":""}
my_dict = {
"tiyan_score": "0",
"haopinglv": "0",
"shop_name": "",
"comment_sum": "0",
"shop_comment_sum": "0",
"product_num": "0",
"follower_count": "0",
"z_shop_code": "",
"z_shop_logo": "",
"z_sku_specs": [],
"y_mainimg": "",
}
if "msg" in dd:
if dd["msg"] == "商品已下架" or dd["msg"] == "数据已失效" or dd["msg"] == "商品已失效":
p_url = f'https://你的官网/uploadGoodsDetail?product_id={product_id}&type=h5&off=1'
print(p_url)
print("---------------------------失效-->")
res = requests.post(p_url, json={}).text
print(res)
continue
if "is_risky" in dd:
print("出验证码了,有is_risky,证明有风控的风险,还是换一个ip吧")
if DouYinConfig.if_reboot_router == "1":
path = os.path.join(os.getcwd(), 'selenium_chrome', 'rebootRouterNow.exe')
anti = " ".join([path, ' '])
os.popen(anti).read()
time.sleep(120)
print("睡眠120秒,从新启动路由器")
continue
else:
time.sleep(1800)
continue
if "bdturing" in dd:
print("出验证码了,程序退出")
TIKTOK(2,product_id)
break
if "data" in dd:
if "bd_turing" in dd["data"]:
print("评论数出验证码了,程序退出")
break
if "promotions" in dd and len(dd["promotions"]) == 0:
p_url = f'https://你的官网/uploadGoodsDetail?product_id={product_id}&type=h5&off=1'
print(p_url)
print("---------------------------商品失效--promotion_h5=[]-->")
res = requests.post(p_url, json={}).text
print(res)
continue
if "promotions" in dd:
if "comment_info" in dd["promotions"][0] and dd["promotions"][0]["comment_info"] != None:
if "shop_comment_stats" in dd["promotions"][0]["comment_info"] and \
dd["promotions"][0]["comment_info"]["shop_comment_stats"] != None:
if "total_count" in dd["promotions"][0]["comment_info"]["shop_comment_stats"]:
my_dict["shop_comment_sum"] = dd["promotions"][0]["comment_info"]["shop_comment_stats"][
"total_count"]
if "promotion_v2" in dd and dd["promotion_v2"] != None and dd["promotion_v2"] != "":
promotion_v2 = dd["promotion_v2"]
print(promotion_v2)
promotion_v2_dict = json.loads(promotion_v2)
if "image_component" in promotion_v2_dict and promotion_v2_dict["image_component"] != None:
if "images" in promotion_v2_dict["image_component"] and len(
promotion_v2_dict["image_component"]["images"]) > 0:
if "url_list" in promotion_v2_dict["image_component"]["images"][0] and len(
promotion_v2_dict["image_component"]["images"][0]["url_list"]) > 0:
my_dict["y_mainimg"] = promotion_v2_dict["image_component"]["images"][0]["url_list"][0]
if "spec_component" in promotion_v2_dict and promotion_v2_dict["spec_component"] != None:
if "spec_list" in promotion_v2_dict["spec_component"] and len(
promotion_v2_dict["spec_component"]["spec_list"]) > 0:
for sepc_one in promotion_v2_dict["spec_component"]["spec_list"]:
z_spec_info = {}
z_spec_info["spec_detail_id"] = sepc_one["spec_detail_id"]
z_spec_info["effective_price"] = sepc_one["effective_price"]
z_spec_info["name"] = sepc_one["name"]
if len(sepc_one["img_list"][0]["url_list"]) > 0:
z_spec_info["img"] = sepc_one["img_list"][0]["url_list"][0]
else:
z_spec_info["img"] = my_dict["y_mainimg"]
my_dict["z_sku_specs"].append(z_spec_info)
if "shop_component" in promotion_v2_dict and promotion_v2_dict["shop_component"] != None:
if "followerCount" in promotion_v2_dict["shop_component"]:
my_dict["follower_count"] = str(promotion_v2_dict["shop_component"]["followerCount"])
if "product_num" in promotion_v2_dict["shop_component"]:
my_dict["product_num"] = str(promotion_v2_dict["shop_component"]["product_num"])
if "promotion_h5" in dd:
if "comment_data" in dd["promotion_h5"] and dd["promotion_h5"]["comment_data"] != None:
if "good_comment" in dd["promotion_h5"]["comment_data"] and dd["promotion_h5"]["comment_data"][
"good_comment"] != None:
if "title_bar" in dd["promotion_h5"]["comment_data"]["good_comment"] and \
dd["promotion_h5"]["comment_data"]["good_comment"]["title_bar"] != None:
if "sum" in dd["promotion_h5"]["comment_data"]["good_comment"]["title_bar"]:
my_dict["comment_sum"] = \
dd["promotion_h5"]["comment_data"]["good_comment"]["title_bar"]["sum"]
if "shop_info" in dd["promotion_h5"] and dd["promotion_h5"]["shop_info"] != None:
if "basic_info" in dd["promotion_h5"]["shop_info"] and dd["promotion_h5"]["shop_info"][
"basic_info"] != None:
my_dict["shop_name"] = dd["promotion_h5"]["shop_info"]["basic_info"]["shop_name"]
my_dict["z_shop_logo"] = dd["promotion_h5"]["shop_info"]["basic_info"]["shop_logo"]
if "shop_link_button" in dd["promotion_h5"]["shop_info"]["basic_info"] and \
dd["promotion_h5"]["shop_info"]["basic_info"]["shop_link_button"] != None:
if "link" in dd["promotion_h5"]["shop_info"]["basic_info"]["shop_link_button"]:
my_dict["z_shop_code"] = \
dd["promotion_h5"]["shop_info"]["basic_info"]["shop_link_button"]["link"]
str2 = "sslocal://goods/store?sec_shop_id=jMvuxCNI&entrance_location=product_detail&url_maker=shop_sdk"
z_shop_code = re.findall('sec_shop_id=(.*?)&entrance_location',
my_dict["z_shop_code"])
if len(z_shop_code) > 0:
my_dict["z_shop_code"] = z_shop_code[0]
if "score_colloquial" in dd["promotion_h5"]["shop_info"]["basic_info"] and \
dd["promotion_h5"]["shop_info"]["basic_info"]["score_colloquial"] != None:
haopinglv = dd["promotion_h5"]["shop_info"]["basic_info"]["score_colloquial"][0][
"description"].replace('商品好评率', '')
haopinglv = haopinglv.replace('%', '')
if haopinglv == "商品评价一般":
my_dict["haopinglv"] = "0"
else:
my_dict["haopinglv"] = "0." + haopinglv
if "grade_info" in dd["promotion_h5"]["shop_info"]["basic_info"] and \
dd["promotion_h5"]["shop_info"]["basic_info"]["grade_info"] != None:
if "score" in dd["promotion_h5"]["shop_info"]["basic_info"]["grade_info"]:
my_dict["tiyan_score"] = \
dd["promotion_h5"]["shop_info"]["basic_info"]["grade_info"]["score"]
serv = DouYinConfig.which_server
p_url = f'https://你的官网/uploadGoodsDetailV2?product_id={product_id}&type=h5&h5_type=2&serv={serv}'
json_str = json.dumps(my_dict)
print(json_str)
print(f"------------------------>云端请求结果 产品id:{product_id}")
res = requests.post(p_url, json=my_dict).text
print(res)
print(f"###################################产品id:{product_id} 结束###################################")
#rnd_sleep = random.randint(1)
rnd_sleep= 0.1
print(f"------------------------>产品id:{product_id} 采集睡眠{rnd_sleep}秒")
time.sleep(rnd_sleep)
def up_app_aity(self):
for i in range(0, 15000):
p_url = 'https://你的官网/uploadGoodsDetail?type=app'
print(f"睡眠{self.detail_sleep_time}秒请求产品详情")
time.sleep(self.detail_sleep_time)
res = requests.post(p_url, json={}).text
res_data = json.loads(res)
product_id = str(res_data["data"]["product_id"])
res_h5 = str(res_data["data"]["h5"])
res_app = str(res_data["data"]["app"])
print(res_data)
if len(product_id) < 7:
print(f"今天APP数据已经采集完毕,睡眠6秒会结束")
print(f"进入H5里面采集")
self.up_h5_aity()
exit(1)
break
else:
verifyFp = self.s_v_web_id
print("---------------verifyFp-->" + verifyFp)
msToken = self.get_ms_token()
print(msToken)
# api_post_data = "promotion_ids={}&use_new_price=1&isFromVideo=false&is_in_app=0"
api_url = "is_h5=1&is_native_h5=1&verifyFp={}&msToken={}"
api_post_data = "use_new_price=1&is_h5=1&bff_type=2&is_in_app=0&origin_type=&promotion_ids={}&meta_param=&source_page=&request_additions=&isFromVideo=false"
fapi_url = api_url.format(verifyFp, msToken)
fapi_post_data = api_post_data.format(product_id)
xb = get_tiiktok_anti2(fapi_url, fapi_post_data)
url = "https://采集网址例如q258599831/aweme/v2/shop/promotion/pack/?" + xb
print(url)
headers = {
'Host': 'ecom.q258599831.com',
'Connection': 'keep-alive',
'Content-Length': '550',
'sec-ch-ua': DouYinConfig.sec_ch_ua,
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/x-www-form-urlencoded',
'sec-ch-ua-mobile': '?0',
'User-Agent': DouYinConfig.User_Agent,
'sec-ch-ua-platform': '"Windows"',
'Origin': 'https://haohuo.q258599831.com',
'Sec-Fetch-Site': 'cross-site',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Dest': 'empty',
'Referer': 'https://haohuo.q258599831.com/',
'Accept-Language': 'zh-CN,zh;q=0.9',
'cookie': f'msToken={msToken}'
}
if str(res_h5) == "0":
continue
# response = requests.request("POST", url, headers=headers, data=fapi_post_data)
# h5 = json.loads(response.text)
h5 = {}
# print(h5)
else:
h5 = {}
print("睡眠1秒请求app产品详情")
api_url = "is_h5=1&is_native_h5=1&verifyFp={}&msToken={}"
# api_post_data="promotion_ids={}&use_new_price=1&isFromVideo=false&is_in_app=1"
api_post_data = "promotion_ids={}&use_new_price=1&isFromVideo=false&use_new_price=1&bff_type=2&is_in_app=1"
fapi_url = api_url.format(verifyFp, msToken)
fapi_post_data = api_post_data.format(product_id)
xb = get_tiiktok_anti2(fapi_url, fapi_post_data)
url = "https://采集网址q258599831/aweme/v2/shop/promotion/pack/?" + xb
if str(res_app) == "0":
print("开始app请求--->")
try:
response = requests.request("POST", url, headers=headers, data=fapi_post_data, timeout=5)
app_data = json.loads(response.text)
except Exception as e:
print("开始app请求超时--->")
rnd_sleep = random.randint(1, 2)
print(f"采集睡眠{rnd_sleep}秒后重新请求 ")
time.sleep(rnd_sleep)
try:
response = requests.request("POST", url, headers=headers, data=fapi_post_data, timeout=5)
app_data = json.loads(response.text)
except Exception as e:
print(f"------------------------>产品id:{product_id} 两次请求超时会主动退出 ")
if DouYinConfig.if_reboot_router == "1":
path = os.path.join(os.getcwd(), 'selenium_chrome', 'rebootRouterNow.exe')
anti = " ".join([path, ' '])
os.popen(anti).read()
time.sleep(120)
print("睡眠2120秒,从新启动路由器")
continue
else:
time.sleep(1800)
continue
# exit(1)
print("app请求结束--->")
goods_dict = {}
goods_dict["h5"] = h5
goods_dict["app"] = app_data
if 'is_risky' in goods_dict["app"]:
print(f"------------------------>产品id:{product_id} 因风控所以主动退出 ")
if DouYinConfig.if_reboot_router == "1":
path = os.path.join(os.getcwd(), 'selenium_chrome', 'rebootRouterNow.exe')
anti = " ".join([path, ' '])
os.popen(anti).read()
time.sleep(120)
print("睡眠120秒,从新启动路由器")
continue
else:
time.sleep(1800)
continue
# exit(1)
if 'page_style' in goods_dict["h5"]:
del goods_dict["h5"]['page_style']
if 'fallback_page' in goods_dict["h5"]:
del goods_dict["h5"]['fallback_page']
if 'extra' in goods_dict["h5"]:
del goods_dict["h5"]['extra']
if 'log_pb' in goods_dict["h5"]:
del goods_dict["h5"]['log_pb']
if 'page_style' in goods_dict["app"]:
del goods_dict["app"]['page_style']
if 'fallback_page' in goods_dict["app"]:
del goods_dict["app"]['fallback_page']
if 'extra' in goods_dict["app"]:
del goods_dict["app"]['extra']
if 'log_pb' in goods_dict["app"]:
del goods_dict["app"]['log_pb']
############ 打印json ############
print(f"------------------------>产品id:{product_id} 返回json开始")
json_str = json.dumps(goods_dict)
print(json_str)
p_url = f'https://我的官网/uploadGoodsDetail?product_id={product_id}&type=app'
if not goods_dict:
print(Fore.GREEN + '----------------未获取远端数据----------------')
break
'''
if "is_risky" in goods_dict["h5"]:
print(Fore.GREEN + f'----------------product_id:{product_id} 数据被临时风控----------------')
break
'''
# break
res = requests.post(p_url, json=goods_dict).text
print(Fore.GREEN + '----------------返回调用结果----------------')
print(res)
print(f"------------------------>产品id:{product_id} 返回json结束")
############ 打印json ############
rnd_sleep = random.randint(1, 3)
print(f"------------------------>产品id:{product_id} 采集睡眠{rnd_sleep}秒")
time.sleep(rnd_sleep)
class Douyin_Slider(object):
def __init__(self, bg, gap):
bg_size = (340, 212)
gap_size = (68, 68)
self.img_dir = os.path.join(os.getcwd(), 'image')
self.bg = self.get_img_path(bg, 'bg', bg_size)
self.gap = self.get_img_path(gap, 'gap', gap_size)
self.out = os.path.join(self.img_dir, 'out.jpg')
def get_img_path(self, img_path, img_name, resize):
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;"
"q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9,en-GB;q=0.8,en;q=0.7,ja;q=0.6",
"Cache-Control": "max-age=0",
"Connection": "keep-alive",
"Host": urlparse(img_path).hostname,
"Referer": "https://www.douyin.com/",
# "Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0",
}
resp = requests.get(url=img_path, headers=headers)
if resp.status_code == 200:
img_path = f'./image/{img_name}.jpg'
image = np.asarray(bytearray(resp.content), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
if resize:
image = cv2.resize(image, dsize=resize)
# pass
cv2.imwrite(img_path, image)
return img_path
else:
print('下载失败,状态码为:{}'.format(resp.status_code))
@staticmethod
def clear_white(img):
img = cv2.imread(img)
rows, cols, channel = img.shape
min_x = 255
min_y = 255
max_x = 0
max_y = 0
for x in range(1, rows):
for y in range(1, cols):
t = set(img[x, y])
if len(t) >= 2:
if x <= min_x:
min_x = x
elif x >= max_x:
max_x = x
if y <= min_y:
min_y = y
elif y >= max_y:
max_y = y
img1 = img[min_x:max_x, min_y: max_y]
return img1
@staticmethod
def image_edge_detection(img):
edges = cv2.Canny(img, 100, 200)
return edges
def template_match(self, tpl, target):
th, tw = tpl.shape[:2]
result = cv2.matchTemplate(target, tpl, cv2.TM_CCOEFF_NORMED)
# 寻找矩阵(一维数组当作向量,用Mat定义) 中最小值和最大值的位置
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
tl = max_loc
br = (tl[0] + tw, tl[1] + th)
# 绘制矩形边框,将匹配区域标注出来
# target:目标图像
# tl:矩形定点
# br:矩形的宽高
# (0,0,255):矩形边框颜色
# 1:矩形边框大小
cv2.rectangle(target, tl, br, (0, 0, 255), 2)
cv2.imwrite(self.out, target)
return tl[0]
def discern(self):
img1 = self.clear_white(self.gap)
img1 = cv2.cvtColor(img1, cv2.COLOR_RGB2GRAY)
slide = self.image_edge_detection(img1)
back = cv2.imread(self.bg, 0)
back = self.image_edge_detection(back)
slide_pic = cv2.cvtColor(slide, cv2.COLOR_GRAY2RGB)
back_pic = cv2.cvtColor(back, cv2.COLOR_GRAY2RGB)
x = self.template_match(slide_pic, back_pic)
# print('x: ', x)
# 输出横坐标, 即 滑块在图片上的位置
return x
def get_track(distance):
"""
根据偏移量获取移动轨迹
:param distance: 偏移量
:return: 移动轨迹
"""
# 移动轨迹
track = []
# 当前位移
current = 0
# 减速阈值
mid = distance * 3 / 4
# 计算间隔
t = 1
# 初速度
v = 0
while current < distance:
if current < mid:
# 加速度为正 2
a = 2
else:
# 加速度为负 3
a = -3
a = 3 if current < mid / 2 else (4 if current < mid else -4)
# 初速度 v0
v0 = v
# 当前速度 v = v0 + at
v = v0 + a * t
move = v0 * t + 1 / 2 * a * t * t
# 当前位移
current += move
# 加入轨迹
track.append(round(move))
# print '轨迹 ', track
return track
class TIKTOK:
def view_detail(self):
id = "3610351231685539908"
if len(str(self.product_id))>2:
id = str(self.product_id)
base_url = f"https://某网址/ecommerce/trade/detail/index.html?id={id}"
self.driver.get(base_url)
self.driver.maximize_window()
time.sleep(2)
for i in range(0, 50):
try:
print(f"-----------------{i}->")
bg = self.driver.find_element(By.ID, "captcha-verify-image").get_attribute('src')
print("bg------------->" + bg)
gap = ""
try:
gap = self.driver.find_element(By.XPATH, '//*[@id="captcha_container"]/div/div[2]/img[2]').get_attribute('src')
except Exception as e:
#path = os.path.join(os.getcwd(), 'selenium_chrome', 'rebootRouter.exe')
#anti = " ".join([path, ' '])
#anti = os.popen(anti).read()
time.sleep(100)
my_a = int(input('input number: '))
if my_a <1:
time.sleep(6000)
break
#exit(1)
print("gap------------->" + gap)
slider = Douyin_Slider(bg=bg, gap=gap)
distance = slider.discern()
print('移动距离:', distance)
slider = self.driver.find_element(By.XPATH, '//div[@id="secsdk-captcha-drag-wrapper"]/div[2]')
ActionChains(self.driver).click_and_hold(slider).perform()
_tracks = get_track(distance)
print('开始验证....')
print(_tracks)
new_1 = _tracks[-1] - (sum(_tracks) - distance)
_tracks.pop()
_tracks.append(new_1)
# 四个节点合并一个节点记得 节点总和-3
get_1 = _tracks[-1]
get_2 = _tracks[-2]
get_3 = _tracks[-3]
get_4 = _tracks[-4]
get_two = get_1 + get_2 + get_3 + get_4 - 7
_tracks.pop()
_tracks.pop()
_tracks.pop()
_tracks.pop()
_tracks.append(get_two)
_tracks.append(3)
_tracks.append(4)
# python让数据只有八个节点 [2, 4, 8, 10, 14, 17, 21, 21, 17] 变成[2, 4, 8, 10, 14, 54, 3, 4]
_new_tracks = []
# _tracks = [2, 4, 8, 10, 14, 17, 21, 21, 17]
mylen = len(_tracks)
cha_len = mylen - 8
step = 0
he = 0
my_index = 2
if cha_len > 0:
for i in range(0, len(_tracks)):
if step <= cha_len:
he += _tracks[i]
my_8_dict["1"] = he
step = step + 1
for long in my_8_dict.values():
time.sleep(0.01)
ActionChains(self.driver).move_by_offset(long, 0).perform()
else:
for long in _tracks:
time.sleep(0.01)
ActionChains(self.driver).move_by_offset(long, 0).perform()
time.sleep(3)
ActionChains(self.driver).release().perform()
ActionChains(self.driver).click(slider)
time.sleep(0.2)
# break
except Exception as e:
print(f"--------------22---{i}->")
break
def __init__(self,which,product_id=0):
#重置时先改变fp
self.product_id = str(product_id)
cur_path = os.path.abspath(os.path.dirname(__file__))
sys.path.append(cur_path)
cookie_path = cooke_path_1.get_cooke_path_1_resetapp()
get_chromedriver_path = cooke_path_1.get_chromedriver_path()
print("打印长度-------------->"+str(len(str(self.product_id))))
try:
shutil.rmtree(cookie_path)
print("directory removed successfully")
except OSError as e:
print("Error: %s : %s" % (cookie_path, e.strerror))
if not os.path.exists(cookie_path):
os.makedirs(cookie_path)
chrome_options = webdriver.ChromeOptions()
# chrome_options.add_argument('--proxy-server={0}'.format(self.proxy.proxy))
# chrome_options.add_argument('--blink-settings=imagesEnabled=false')
chrome_options.add_argument("--disable-blink-features=AutomationControlled") # 禁用启用Blink运行时的功能
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"]) # 去除浏览器检测框
chrome_options.add_argument('--user-data-dir='+cookie_path)
self.driver = webdriver.Chrome(chrome_options=chrome_options,
executable_path=get_chromedriver_path)
with open(f'{cur_path}'+'/stealth.min.js', encoding='utf-8') as f:
js = f.read()
self.driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": js
})
self.curr_status()
#time.sleep(300)
#base_url = "https://某网址/ecommerce/trade/detail/index.html?id=3602005992881188284"
##打印cookie 并携带cookie
self.view_detail()
if '初始化失败' in self.driver.page_source:
print(f"商详数据初始化失败")
if DouYinConfig.if_reboot_router == "1":
print("睡眠120秒,从新启动路由器")
path = os.path.join(os.getcwd(), 'selenium_chrome', 'rebootRouterNow.exe')
anti = " ".join([path, ' '])
os.popen(anti).read()
print("睡眠120秒,从新启动路由器")
time.sleep(120)
TIKTOK(2)
else:
time.sleep(1800)
TIKTOK(2)
cookies = self.driver.get_cookies()
jsonCookies = json.dumps(cookies)
with open('s_v_web_id.json', 'w') as f:
f.write(jsonCookies)
cookie_dict = {}
for cookie in cookies:
k = cookie['name']
v = cookie['value']
cookie_dict[k] = v
# print(cookie_dict)
# time.sleep(2)
cookie_dict_s_v_web_id = cookie_dict.get('s_v_web_id')
cookie_dict_msToken = cookie_dict.get('msToken')
print("----------------------cookie_dict_s_v_web_id--->" + cookie_dict_s_v_web_id)
print("----------------------cookie_dict_msToken--->" + cookie_dict_msToken)
self.driver.refresh()
self.driver.close()
self.driver.quit()
##进行数据采集
updata = UpDataSlide(1, self.product_id)
#time.sleep(1)
TIKTOK(1)
##进行数据采集
# 是否登录过期
def curr_status(self):
base_url = "https://某登录平台/login"
self.driver.get(base_url)
self.driver.maximize_window()
time.sleep(2)
self.driver.refresh()
time.sleep(2)
if '扫码登录' in self.driver.page_source or '账号登录' in self.driver.page_source or '忘记密码' in self.driver.page_source:
self.driver.find_element(By.CLASS_NAME,"account-center-qrcode-switch-action").click()
time.sleep(1)
self.driver.find_element(By.XPATH,"//div[contains(@class,'account-center-switch-button switch-switch false email')]").click()
time.sleep(1)
# 输入账号和密码
user_ele =self.driver.find_element(By.XPATH,"//input[@name='email']")
acc = cooke_path_1.get_cookie_acc_1()
user_ele.send_keys(acc)
pass_ele = self.driver.find_element(By.XPATH,"//input[@name='password']")
pwd = cooke_path_1.get_cookie_pwd_1()
pass_ele.send_keys(pwd)
self.driver.find_element(By.CLASS_NAME, "account-center-action-button").click()
time.sleep(2)
print("睡眠2")
distance = 0
for i in range(0, 50):
try:
print(f"-----------------{i}->")
bg = self.driver.find_element(By.XPATH, "//*[@id=\"captcha-verify-image\"]").get_attribute('src')
print("bg------------->" + bg)
gap = self.driver.find_element(By.XPATH, "//*[@id=\"sdk-login-box-slide-container\"]/div/div[2]/img[2]").get_attribute('src')
print("gap------------->" + gap)
slider = Douyin_Slider(bg=bg, gap=gap)
distance = slider.discern()
print('移动距离:', distance)
slider = self.driver.find_element(By.XPATH, '//div[@id="secsdk-captcha-drag-wrapper"]/div[2]')
ActionChains(self.driver).click_and_hold(slider).perform()
_tracks = get_track(distance)
print('开始验证....')
print(_tracks)
new_1 = _tracks[-1] - (sum(_tracks) - distance)
_tracks.pop()
_tracks.append(new_1)
# 四个节点合并一个节点记得 节点总和-3
get_1 = _tracks[-1]
get_2 = _tracks[-2]
get_3 = _tracks[-3]
get_4 = _tracks[-4]
get_two = get_1 + get_2 + get_3 + get_4 - 7
_tracks.pop()
_tracks.pop()
_tracks.pop()
_tracks.pop()
_tracks.append(get_two)
_tracks.append(3)
_tracks.append(4)
# python让数据只有八个节点 [2, 4, 8, 10, 14, 17, 21, 21, 17] 变成[2, 4, 8, 10, 14, 54, 3, 4]
_new_tracks = []
# _tracks = [2, 4, 8, 10, 14, 17, 21, 21, 17]
mylen = len(_tracks)
cha_len = mylen - 8
step = 0
he = 0
my_index = 2
if cha_len > 0:
for i in range(0, len(_tracks)):
if step <= cha_len:
he += _tracks[i]
my_8_dict["1"] = he
step = step + 1
for long in my_8_dict.values():
time.sleep(0.01)
ActionChains(self.driver).move_by_offset(long, 0).perform()
else:
for long in _tracks:
time.sleep(0.01)
ActionChains(self.driver).move_by_offset(long, 0).perform()
time.sleep(3)
ActionChains(self.driver).release().perform()
ActionChains(self.driver).click(slider)
time.sleep(0.2)
# break
except Exception as e:
print(f"--------------22---{i}->")
break
if distance==0:
time.sleep(120)
time.sleep(1)
return
def get_cookie_info(self):
pass
if __name__ == '__main__':
tiktok = TIKTOK(2)
声明:本代码仅供学习和研究,代码为百分百原创。一定要遵守互联网安全规定。让我们一起研究技术,用技术来改变世界。欢迎私聊。