求大神帮忙修改下python代码
本帖最后由 howhappy 于 2022-8-10 12:35 编辑之前在论坛看到一个快手的python的代码,很好用,但是现在想改进一下,就是在原有的基础上抓取发布时间,并在保存的文件前加上作品发布时间
附源码:
import logging
import os
import re
import time
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
from pathlib import Path
from typing import List
import requests
from rich.console import Console
from constant import Constant
from untils import mkdir
from save_excel import SaveExecl
console = Console()
const = Constant()
logging.basicConfig(level=logging.INFO,
filename='log.txt',
format='%(asctime)s %(filename)s %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filemode='a')
class Kuaishou:
def __init__(self) -> None:
self.nickname = "" # 主播名字存放点
self.timestamp = ""
self.datas = []
def get_json(self) ->List:
'''
获取json数据
'''
response = requests.post(
url=const.base_url, headers=const.headers, json=const.data)
json_data = response.json()
feeds = json_data['data']['visionProfilePhotoList']['feeds']
# 下一页 链接pcursor导入data
const.pcursor = json_data['data']['visionProfilePhotoList']['pcursor']
return feeds
def get_caption_photoUrl(self, feed):
'''
获取视频地址和标题
feed: 单个视频数据
'''
try:
filepath = os.getcwd() + '/' + 'kuaishou_downloads' + '/' + re.sub(u"([^\u4e00-\u9fa5\u0030-\u0039\u0041-\u005a\u0061-\u007a])","",feed['author']['name']) if feed['author']['name'] else os.getcwd() + '/' + 'kuaishou_downloads' + '/' + '未知用户'
self.nickname = feed['author']['name'] if feed['author']['name'] else "未知"
caption = feed['photo']['caption']# title
photoUrl = feed['photo']['photoUrl']# video link
caption = re.sub('[ \\/:*?"<>|\n\t]', '', caption)
caption = caption[:128] if len(caption)>128 else caption
video_data = requests.get(photoUrl).content
if caption:
time_ns = time.time()
self.save_video(os.path.normpath(filepath), caption + '_' + str(time_ns) + '.mp4', video_data,photoUrl)
except Exception as e:
logging.error(f'错误:{e},获取数据失败,请检查网址是否正确,也可能cookies已过期!')
console.print('获取数据失败,请检查网址是否正确,也可能cookies已过期!')
def save_video(self, path: Path, filename: str, video_data,url):
'''
保存视频文件
'''
mkdir(path)
with open(os.path.normpath(os.path.join(path, filename)), 'wb') as f:
f.write(video_data)
now_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
console.print(
f'创建时间:{now_time}\n文件:{filename}\n状态:下载完成!')
console.print('-'*20)
self.datas.append()
def save_excel(self):
'''
保存excel数据
'''
print('请输入保存的excel文件名称(例如:快手.xlsx):')
ex_file_name = input()
se = SaveExecl(ex_file_name, sheet_name = '快手下载数据')
num = 1 # 序号
one_row = ['序号', '创建时间', '文件名', '路径','网址']
# 注:添加第一行,map是生成器函数,需用list执行生成器
list(map(lambda x:se.save_to_excel(1,x+1,one_row), range(len(one_row))))
for row,data in enumerate(self.datas):
se.save_to_excel(row+2,1,num)
num += 1
for col,d in enumerate(data):
se.save_to_excel(row+2,col+2,d)
se.sucess_sing(True)
console.print('保存成功!')
def main(self):
'''
主函数
'''
page_num = 1
start_time = time.time()
while True:
if const.pcursor != 'no_more':
feeds = self.get_json()
with ThreadPoolExecutor(max_workers=1) as executor:
executor.map(self.get_caption_photoUrl, feeds)
logging.info(f'正在下载第{page_num}页数据...')
page_num += 1
else:
console.print(f'已全部完成下载!')
logging.info(f'{self.nickname}已全部完成下载!')
break
logging.info(f'主播:{self.nickname}的视频下载总耗时:{time.time()-start_time}秒')
time.sleep(2)
console.print(f'总耗时:{time.time()-start_time-2}秒')
self.save_excel()
if __name__ == '__main__':
kuaishou = Kuaishou()
kuaishou.main()
源码作者@话痨司机啊
原帖:【2022-06-10更新】快手视频下载(可双击运行)V1.7 - 『编程语言区』 - 吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn
timestamp字段是13位时间戳,转成时间之后加进文件名里 就是不知道改怎么加呢?抓取不到timestamp字段数据 第一张图的倒数第3个不是一个时间戳么?看看是不是你想要的发布时间戳。
实在不行,photoUrl的路径中有日期... unmask 发表于 2022-8-9 19:34
第一张图的倒数第3个不是一个时间戳么?看看是不是你想要的发布时间戳。
实在不行,photoUrl的路径中有日 ...
就是倒数第三个的时间戳,但是直接抓取不到呢
timestamp = feed['photo']['timestamp'] 为什么我在同样地方加入获取时间戳的代码却提示获取失败? caption = feed['photo']['caption']# title
photoUrl = feed['photo']['photoUrl']# video link
timestamp = feed['photo']['timestamp'] 现在获取到13位的时间戳如何转换成时间呢? def get_caption_photoUrl(self, feed):
'''
获取视频地址和标题
feed: 单个视频数据
'''
try:
filepath = os.getcwd() + '/' + 'kuaishou_downloads' + '/' + re.sub(u"([^\u4e00-\u9fa5\u0030-\u0039\u0041-\u005a\u0061-\u007a])","",feed['author']['name']) if feed['author']['name'] else os.getcwd() + '/' + 'kuaishou_downloads' + '/' + '未知用户'
self.nickname = feed['author']['name'] if feed['author']['name'] else "未知"
caption = feed['photo']['caption']# title
photoUrl = feed['photo']['photoUrl']# video link
timestamp = feed['photo']['timestamp']
time_stamp_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(timestamp) / 1000))
caption = re.sub('[ \\/:*?"<>|\n\t]', '', caption)
caption = caption[:128] if len(caption)>128 else caption
video_data = requests.get(photoUrl).content
if caption:
time_ns = time.time()
self.save_video(os.path.normpath(filepath), str(time_stamp_time) + caption + '_' + str(time_ns) + '.mp4', video_data,photoUrl)
except Exception as e:
logging.error(f'错误:{e},获取数据失败,请检查网址是否正确,也可能cookies已过期!')
console.print('获取数据失败,请检查网址是否正确,也可能cookies已过期!')
现在代码是这样的,但是不能输出日期,怎么回事呢 本帖最后由 bobo2017365 于 2022-8-10 14:57 编辑
print(time_stamp_time)
先看看自己获取到时间没有
页:
[1]