🙋?♀?Tiktok APP的基于關鍵字檢索的視頻及評論信息爬蟲共分為兩期,希望對大家有所幫助。
第一期:基于關鍵字檢索的視頻信息爬取
第二期見下文。
1.Node.js環境配置
首先配置 JavaScript 運行環境(如 Node.js),用于執行加密簽名代碼。
Node.js下載網址:https://nodejs.org/en
Node.js的安裝方法(環境配置非常關鍵,決定了后面的程序是否可以使用):https://blog.csdn.net/liufeifeihuawei/article/details/132425239
2. Py環境配置
import random
from tqdm import tqdm
import requests
from urllib.parse import urlparse, urlencode
import warnings
from urllib3.exceptions import InsecureRequestWarning
import time# 忽略 InsecureRequestWarning 警告
warnings.filterwarnings("ignore", category=InsecureRequestWarning)
3. 基于視頻URL的評論信息爬取
在上期中,已經給出了如何獲得指定視頻的URL,下面給出根據URL獲得視頻評論的信息,允許在爬取的過程中對評論進行翻頁。
1. 主程序
爬單個URL的評論信息的方法:
if __name__ == '__main__':'''單條數據'''req_url = "https://www.tiktok.com/@resep_debm/video/7475545671383174406"tiktok_comment = TiktokComment()x = tiktok_comment.get_comment_list(req_url)print(x)
爬多個URL的評論信息的方法。通過讀取videosInfo.json
文件中保存的URL信息,將最后的結果保存到videos_comments.json
文件中:
if __name__ == '__main__':'''多條數據'''data = read_json('../results/videosInfo.json')print(len(data))tiktok_comment = TiktokComment()new_data = data.copy()for i in tqdm(range(len(data))):if 'comments' not in data[i].keys(): # and i > 1695comments = tiktok_comment.get_comment_list(data[i]['video_url'])if comments != []:new_data[i]['comments'] = commentselse:continueif i % 10 == 0:write_json('../results/videos_comments.json', new_data)# 循環結束后再保存一次,確保所有數據都被寫入write_json('../results/videos_comments.json', new_data)
2. 定義TiktokComments類
允許獲得的評論信息7個字段,包括:
🎰評論ID;
💬評論內容;
🙋評論是否被作者點贊;
😍評論是否熱門;
👍評論的點贊數
👀評論的回復數目
?評論發布的時間;
class TiktokComments:def __init__(self):# self.config = read_config()self.common_utils = CommonUtils()self.cookies = cookie_str_to_dict(read_cookie())# self.proxies = self.config.get("proxies", None) # 代理配置self.comment_list_headers = {'sec-ch-ua': '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"','sec-ch-ua-mobile': '?0','User-Agent': self.common_utils.user_agent,'sec-ch-ua-platform': '"Windows"','Accept': '*/*','Sec-Fetch-Site': 'same-origin','Sec-Fetch-Mode': 'cors','Sec-Fetch-Dest': 'empty','Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',}
通過 cursor_num
設置翻頁,count={comment_num}
表示希望獲得的評論總數目。
def get_comment_list(self, video_url, comments_num=100):aweme_id = urlparse(video_url).path.split("/")[-1]ms_token = self.cookies['msToken']req_comments = []max_retries = 3 # 最大重試次數for i in range(comments_num // 20):cursor_num = i * 20comment_num = 20req_url = f"https://www.tiktok.com/api/comment/list/?WebIdLastTime=1715249710&aid=1988&app_language=ja-JP&app_name=tiktok_web&aweme_id={aweme_id}&browser_language=zh-CN&browser_name=Mozilla&browser_online=true&browser_platform=Win32&browser_version=5.0%20%28Windows%20NT%2010.0%3B%20Win64%3B%20x64%29%20AppleWebKit%2F537.36%20%28KHTML%2C%20like%20Gecko%29%20Chrome%2F123.0.0.0%20Safari%2F537.36&channel=tiktok_web&cookie_enabled=true&" \f"count={comment_num}¤t_region=JP&cursor={cursor_num}&device_id=7366941338308609569&device_platform=web_pc&enter_from=tiktok_web&focus_state=true&fromWeb=1&from_page=video&history_len=2&is_fullscreen=false&is_non_personalized=false&is_page_visible=true&odinId=7367172442253296673&os=windows&priority_region=&referer=®ion=GB&screen_height=1080&screen_width=1920&tz_name=Asia%2FShanghai&webcast_language=zh-Hans&msToken={ms_token}"xbogus = self.common_utils.get_xbogus(req_url, self.common_utils.user_agent)req_url += f'&X-Bogus={xbogus}&_signature=_02B4Z6wo000016M20awAAIDAnp.LMKuZmC-jNtUAAI6L17'for retry in range(max_retries):try:response = requests.request('GET',req_url,headers=self.comment_list_headers,# cookies=self.cookies,verify=False,timeout=random.randint(3, 7),# proxies=self.proxies)if response.status_code != 200:continuereq_json = response.json()comments = req_json.get('comments', [])# print(f"評論數目:{req_json.get('total')}")if not comments:print(f"No comments found for cursor {cursor_num}.")breakfor comment_item in comments:req_comments.append({"cid": comment_item.get('cid'),"comment": comment_item.get('text'),"comments_is_author_like": comment_item.get('is_author_digged'),"comments_is_hot": comment_item.get('is_comment_translatable'),"comments_like": comment_item.get('digg_count'),"comments_reply": comment_item.get('reply_comment_total'),"comments_time": comment_item.get('create_time')})break # 成功獲取數據,退出重試循環except Exception as e:print(f"Error: {e}. Retrying ({retry + 1}/{max_retries})...")if retry == max_retries - 1:print("Max retries reached. Skipping this request.")return req_comments