From e70d03426826e645491f10ad895be314ecfe2b91 Mon Sep 17 00:00:00 2001 From: liuwei Date: Tue, 11 Mar 2025 13:26:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E8=A7=86=E9=A2=91=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E5=8F=91=E9=80=81=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- douyin_parser/main.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/douyin_parser/main.py b/douyin_parser/main.py index 4ea7ec4..4220b74 100644 --- a/douyin_parser/main.py +++ b/douyin_parser/main.py @@ -198,7 +198,13 @@ class DouyinParser: """ try: # 发送GET请求,启用流式传输 - response = requests.get(url, stream=True) + headers = { + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36", + "Referer": "https://www.douyin.com/", + "Accept": "*/*", + "Connection": "keep-alive", + } + response = requests.get(url, headers=headers) # 检查请求是否成功 response.raise_for_status() # 如果状态码不是200,将抛出异常 @@ -208,9 +214,10 @@ class DouyinParser: # 检查是否是视频流(可选,根据Content-Type判断) content_type = response.headers.get("Content-Type", "").lower() + self.LOG.info(response.text) if "video" not in content_type and "application/octet-stream" not in content_type: - print(f"警告: 返回的可能不是视频流,Content-Type: {content_type}") - print("响应内容预览:", response.text[:100]) # 打印前100字符查看 + self.LOG.info(f"警告: 返回的可能不是视频流,Content-Type: {content_type}") + self.LOG.info("响应内容预览:", response.text[:100]) # 打印前100字符查看 return # 以二进制写入模式保存流数据 @@ -218,11 +225,11 @@ class DouyinParser: for chunk in response.iter_content(chunk_size=1024): # 分块读取,每块1KB if chunk: # 过滤空块 file.write(chunk) - print(f"视频已下载到: {save_path}") + self.LOG.info(f"视频已下载到: {save_path}") return os.path.abspath(save_path) except requests.RequestException as e: - print(f"请求失败: {e}") + self.LOG.info(f"请求失败: {e}") except IOError as e: - print(f"文件写入失败: {e}") + self.LOG.info(f"文件写入失败: {e}") except Exception as e: - print(f"发生未知错误: {e}") + self.LOG.info(f"发生未知错误: {e}")