def search_one_user(self, keyword): device = random.randint(1999999999999, 99999999999999)
url = self.api + "/aweme/v1/search/general/single/?carrier_region=US®ion=US&device_id={}&os_version=5.1.1&aid=4082&keyword={}&count=1&query_correct_type=1&search_source=normal_search&cursor=0".format(
str(device),
keyword)
try:
res = requests.get(url).content.decode('utf-8')
res = res.replace("https", "http")
res = json.loads(res)
if res.get('data'):
for v in res.get('data'):
if v.get('type') == 4:
# print(v['user_list'][0]['user_info']['sec_uid'])
# return v['user_list'][0]['user_info']
res = self.get_user_other(v['user_list'][0]['user_info']['sec_uid'])
return res
except:
pass
return {"status_code": "-1"}
作品获取,需要参数sec_uid 和 翻页游标 cursor
[Python] 纯文本查看复制代码
def post(self, sec_uid, cursor):
device = random.randint(1999999999999, 99999999999999)
url = self.api + "/aweme/v1/aweme/post/?count=12&sec_user_id={}&max_cursor={}&os_api=22&device_id={}&aid=4082".format(
sec_uid, str(cursor), str(device))
for _ in range(10):
try:
res = requests.get(url).content.decode('utf-8')
res = res.replace("https", "http")
res = json.loads(res)
if res.get('aweme_list'):
return res
except:
pass
return {"status_code": "-1", 'aweme_list': []}
批量下载视频
[Python] 纯文本查看复制代码
def down(self, sec_uid, nickname):
if self.downing == True:
return "等待下载任务结束后再下载"
self.downing = True
self.sec_uid = sec_uid
self.nickname = nickname
self.Isend = False
self.path = "." + sprit + "Download" + sprit + self.nickname + sprit
if not os.path.exists(self.path):
os.makedirs(self.path)
self.max_cursor = 0
device = random.randint(1999999999999, 99999999999999)
url = self.api + "/aweme/v1/aweme/post/?count=12&sec_user_id={}&max_cursor={}&os_api=22&device_id={}&aid=4082".format(
sec_uid, str(self.max_cursor), str(device))
for _ in range(10):
res = requests.get(url).content.decode('utf-8')
res = res.replace("https", "http")
res = json.loads(res)
if res['has_more'] == 0:
self.Isend = True
if res.get('aweme_list'):
self.max_cursor = res['max_cursor']
self.getVideoInfo(res.get('aweme_list'))
break
else:
break
self.downing = False
def getNextData(self):
if self.Isend == False:
device = random.randint(1999999999999, 99999999999999)
url = self.api + "/aweme/v1/aweme/post/?count=12&sec_user_id={}&max_cursor={}&os_api=22&device_id={}&aid=4082".format(
self.sec_uid, str(self.max_cursor), str(device))
for _ in range(10):
try:
res = requests.get(url).content.decode('utf-8')
res = res.replace("https", "http")
res = json.loads(res)
if res['has_more'] == 0:
self.Isend = True
if res.get('aweme_list'):
self.max_cursor = res['max_cursor']
self.getVideoInfo(res.get('aweme_list'))
break
else:
break
except:
pass
def getVideoInfo(self, result):
self.author_list = []
# 无水印视频链接
self.video_list = []
# 作品idvideo_list
self.aweme_id = []
# 唯一视频标识
self.uri_list = []
for v in range(len(result)):
try:
self.author_list.append(str(result[v]['desc']))
self.video_list.append(
str(result[v]['video']['play_addr']['url_list'][0]).replace("https", "http"))
self.uri_list.append(
str(result[v]['video']['play_addr']['uri']))
self.aweme_id.append(str(result[v]['aweme_id']))
except Exception as e:
break
for i in range(len(self.author_list)):
self.xhl.acquire()
self.xc = threading.Thread(target=self.download,
args=(self.author_list[i], self.uri_list[i], self.video_list[i]), daemon=True)
self.xc.start()
self.getNextData()
def download(self, author, uri, video_):
author = ''.join(author.splitlines())
if len(author) > 182:
author = author[0:180]
file_state = test(self.path, re.sub(r'[\\/:*?"<>|\r\n]+', "_", author), uri, ".mp4")
if file_state == True:
self.xhl.release()
else:
try:
video = requests.get(video_) # 视频信息
t_video = video.content
if video.status_code == 200: # 判断是否响应成功
# print(self.uri_list[i])
v_url = self.path + sprit + re.sub(r'[\\/:*?"<>|\r\n]+', "_", author) + uri + '.mp4'
with open(v_url, 'wb') as file: # 显示进度条
file.write(t_video)
self.xhl.release()
except:
print("下载出错")
# lyex_zb
# HOLA AJJSKSKSKS | #fyp #parati #musica #cancion #lyric #letra #viral #capcut #ariela640 #lyexzbv09044g40000capqukbc77u2aho81g90.mp4
def getdowninfo(self, nickname, all):
try:
if self.downing == False:
return {'count': '100', 'status': 0}
path = "." + sprit + "Download" + sprit + nickname + sprit
files = os.listdir(path) # 读入文件夹
num_png = len(files)
return {'count': str(math.ceil(num_png * 100 / int(all))), 'status': 0}
except:
return {'status': -1}