import requests import json from pprint import pprint import csv import os import glob from time import sleep f=open('抖音评论.csv',mode='a',encoding='utf-8',newline='') csv_writer=csv.DictWriter(f,fieldnames=[ '昵称', '账号', '评论', ]) csv_writer.writeheader() #评论接口 url = 'https://www.douyin.com/aweme/v1/web/comment/list/?device_platform=webapp&aid=6383&channel=channel_pc_web&aweme_id=7368527269490445604&cursor=0&count=20&item_type=0&insert_ids=&whale_cut_token=&cut_version=1&rcFT=&update_version_code=170400&pc_client_type=1&version_code=170400&version_name=17.4.0&cookie_enabled=true&screen_width=1280&screen_height=720&browser_language=zh-CN&browser_platform=Win32&browser_name=Edge&browser_version=125.0.0.0&browser_online=true&engine_name=Blink&engine_version=125.0.0.0&os_name=Windows&os_version=10&cpu_core_num=8&device_memory=8&platform=PC&downlink=0.35&effective_type=3g&round_trip_time=800&webid=7346532852764591631&verifyFp=verify_lwlm1u72_stNCFCId_qzbH_4cpL_9Iep_AqW2XPQog7ma&fp=verify_lwlm1u72_stNCFCId_qzbH_4cpL_9Iep_AqW2XPQog7ma&msToken=BUDTdDbl3B85-cq712EQUfsoVxrmh7Xns1q17Z7JrcXxmqcc4yjx0qHJZosee8R1NtVQs-S70zcxv8VsusWsalpiXxdLwrwumnjrIhjBATZZh2wXhUDif-17&a_bogus=m6mhQmghmEVNXDyh56ALfY3q6-33Ym8r0trEMD2fsnfl1y39HMOO9exYXkzvwnfjLG%2FlIeujy4hbYN9krQA70qwfHuix%2F2ohmDSkKl5Q5xSSs1XJtyUgntsNmktUCec2-i-lrOXMoX1HFbSd09oHmhK4bIOwu3GMSf%3D%3D' headers={ 'Referer':'https://www.douyin.com/search/%E7%BE%8A%E8%82%89%E6%89%B9%E5%8F%91?aid=037a1fc5-7500-415f-9cb4-056533ed2a37&modal_id=7355745553893870874&type=general', 'cookie':'LOGIN_STATUS=1; store-region=cn-hn; store-region-src=uid; my_rd=2; __live_version__=%221.1.1.4879%22; ttwid=1%7Cj1y2rrK1xocvz8_dWNVIq9GJ1UUkc3qyI3unO5Hnz7c%7C1710497985%7C0580bd3d607011ab36c5240383d9941e485699343a9be647cab7b4e4877837e3; xgplayer_user_id=785850983213; bd_ticket_guard_client_web_domain=2; SEARCH_RESULT_LIST_TYPE=%22single%22; dy_swidth=1280; dy_sheight=720; s_v_web_id=verify_lwlm1u72_stNCFCId_qzbH_4cpL_9Iep_AqW2XPQog7ma; passport_csrf_token=58820e3ccfd2a4c8d213c627c8bfcbf2; passport_csrf_token_default=58820e3ccfd2a4c8d213c627c8bfcbf2; FORCE_LOGIN=%7B%22videoConsumedRemainSeconds%22%3A180%2C%22isForcePopClose%22%3A1%7D; n_mh=O0Ud012BYDa2zc36g8-pEsT4HQkknDbOdmVfd9MRc2k; publish_badge_show_info=%220%2C0%2C0%2C1716611360297%22; _bd_ticket_crypt_doamin=2; __security_server_data_status=1; download_guide=%223%2F20240525%2F1%22; pwa2=%220%7C0%7C3%7C1%22; volume_info=%7B%22isUserMute%22%3Afalse%2C%22isMute%22%3Atrue%2C%22volume%22%3A0.782%7D; strategyABtestKey=%221716913486.671%22; csrf_session_id=2fa734583c90f0008132a62ef1d35849; WallpaperGuide=%7B%22showTime%22%3A1716647836149%2C%22closeTime%22%3A0%2C%22showCount%22%3A1%2C%22cursor1%22%3A38%2C%22cursor2%22%3A0%7D; passport_assist_user=CkHDnwHJFqV3Hmnr_wWu_20uRY76STzf3Yk_C_17WKJ4of6iixzXSx5CYUF7diaCEN03znCNtHep5en_763hzsNyphpKCjypXf-C-sbpu6h1mWGCnh-rmG2mmxagdqRbRzhoNFr0meuqwYMCu_2RxfyI7bB04PdvZ8TMTLGWba9DBC4Q08bSDRiJr9ZUIAEiAQOh_u6X; sso_uid_tt=b3b014e9237e24a2434fcc69284c9e97; sso_uid_tt_ss=b3b014e9237e24a2434fcc69284c9e97; toutiao_sso_user=b9e365d2df3a7c6ba5499448985f61c7; toutiao_sso_user_ss=b9e365d2df3a7c6ba5499448985f61c7; sid_ucp_sso_v1=1.0.0-KGYxZDViZTQxNDIwYWQ0NmI3ZWRkOWE5MWZkZjM5YjAzMmU1YmNmYWEKHwjUmfCgqMzoAhCSp9qyBhjvMSAMMNmDoaEGOAZA9AcaAmhsIiBiOWUzNjVkMmRmM2E3YzZiYTU0OTk0NDg5ODVmNjFjNw; ssid_ucp_sso_v1=1.0.0-KGYxZDViZTQxNDIwYWQ0NmI3ZWRkOWE5MWZkZjM5YjAzMmU1YmNmYWEKHwjUmfCgqMzoAhCSp9qyBhjvMSAMMNmDoaEGOAZA9AcaAmhsIiBiOWUzNjVkMmRmM2E3YzZiYTU0OTk0NDg5ODVmNjFjNw; passport_auth_status=e86684cdf98b870d7eccf74c2242c1fc%2C19d9f08e07de1935f52dedcb194265de; passport_auth_status_ss=e86684cdf98b870d7eccf74c2242c1fc%2C19d9f08e07de1935f52dedcb194265de; uid_tt=7d49bc6824c58d434d292eb027c8021a; uid_tt_ss=7d49bc6824c58d434d292eb027c8021a; sid_tt=c93b600d87e77460e20cda04620f4809; sessionid=c93b600d87e77460e20cda04620f4809; sessionid_ss=c93b600d87e77460e20cda04620f4809; _bd_ticket_crypt_cookie=4200935d4c9d21af23023ca3e5718554; sid_guard=c93b600d87e77460e20cda04620f4809%7C1716949911%7C5183997%7CSun%2C+28-Jul-2024+02%3A31%3A48+GMT; sid_ucp_v1=1.0.0-KDY3YmQzNDA0N2M1ZWE5ZmU1ZWFiMTgyNWRkNjliMjc4ZDY3N2U1MTkKGwjUmfCgqMzoAhCXp9qyBhjvMSAMOAZA9AdIBBoCaGwiIGM5M2I2MDBkODdlNzc0NjBlMjBjZGEwNDYyMGY0ODA5; ssid_ucp_v1=1.0.0-KDY3YmQzNDA0N2M1ZWE5ZmU1ZWFiMTgyNWRkNjliMjc4ZDY3N2U1MTkKGwjUmfCgqMzoAhCXp9qyBhjvMSAMOAZA9AdIBBoCaGwiIGM5M2I2MDBkODdlNzc0NjBlMjBjZGEwNDYyMGY0ODA5; douyin.com; device_web_cpu_core=8; device_web_memory_size=8; architecture=amd64; xg_device_score=7.385531388768509; home_can_add_dy_2_desktop=%220%22; stream_player_status_params=%22%7B%5C%22is_auto_play%5C%22%3A1%2C%5C%22is_full_screen%5C%22%3A0%2C%5C%22is_full_webscreen%5C%22%3A0%2C%5C%22is_mute%5C%22%3A1%2C%5C%22is_speed%5C%22%3A1%2C%5C%22is_visible%5C%22%3A1%7D%22; passport_fe_beating_status=true; odin_tt=16f044c9bd17a88c3c02d2a39992fb90fea29dd69d929ebf9649920fff66a45b82db05491ed9a1cee677dc686ce3535720482bf0b24e29a5e727057c1aafba4e; FOLLOW_NUMBER_YELLOW_POINT_INFO=%22MS4wLjABAAAAsHY3M7WCvAd6-fwDR0tYP32MvxlSPgEfD3dF69gvt3Wg7bkb3YHFIkTg0hifLIWT%2F1716998400000%2F1716949915651%2F0%2F1716964466935%22; __ac_nonce=06656d8c2007c18889bb9; __ac_signature=_02B4Z6wo00f01r4zzQgAAIDDYWg-R7KOY1K-E8mAAMnm3nFmdeA72UBASw1nJi5NnQUyLIHzWOJ9y1GtE1cx8RHUtRM0ECXwp1sbFgNxPFu0kVAhV3spBRLkkNnSj9Z.C4nyRdop7nTjQvnmf8; IsDouyinActive=true; stream_recommend_feed_params=%22%7B%5C%22cookie_enabled%5C%22%3Atrue%2C%5C%22screen_width%5C%22%3A1280%2C%5C%22screen_height%5C%22%3A720%2C%5C%22browser_online%5C%22%3Atrue%2C%5C%22cpu_core_num%5C%22%3A8%2C%5C%22device_memory%5C%22%3A8%2C%5C%22downlink%5C%22%3A0.35%2C%5C%22effective_type%5C%22%3A%5C%223g%5C%22%2C%5C%22round_trip_time%5C%22%3A800%7D%22; bd_ticket_guard_client_data=eyJiZC10aWNrZXQtZ3VhcmQtdmVyc2lvbiI6MiwiYmQtdGlja2V0LWd1YXJkLWl0ZXJhdGlvbi12ZXJzaW9uIjoxLCJiZC10aWNrZXQtZ3VhcmQtcmVlLXB1YmxpYy1rZXkiOiJCTlplYVlVb2N5Z1ZqakNXYnptL0RNN1h6OE94VWJ6ZVVETkFBWVVvRFJEMlJTSkxFRkJQOVhjazlCSzd6K0dUMzBnTlBwKzlwWnZ6a0hoQXRrWEdabUE9IiwiYmQtdGlja2V0LWd1YXJkLXdlYi12ZXJzaW9uIjoxfQ%3D%3D; FOLLOW_LIVE_POINT_INFO=%22MS4wLjABAAAAsHY3M7WCvAd6-fwDR0tYP32MvxlSPgEfD3dF69gvt3Wg7bkb3YHFIkTg0hifLIWT%2F1716998400000%2F0%2F1716967627978%2F0%22; msToken=GforqsysdNF59KAHqxdbrTMQeuKWb4QAM2VZPKM-qh3TcoiyPRKS0O8rWin1tDqOzweZSIf_F5_OjAc1cPown5QluqMFcnNqc_E8hpWoaXrh-LW-80bZrqmC', 'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0' } response = requests.get(url=url,headers=headers,) '''try: json_str = response.text.strip()[len('mtopjsonp3('):-1] data = json.loads(json_str) print(data) except json.JSONDecodeError: print("JSON 解码失败,请检查响应是否为有效的 JSON 格式。")''' #response_text = response.json()['comments'] #print(response.text) for shuju in response.json()['comments']: user_list = shuju['user'] dit={ '昵称':user_list['nickname'], '账号':user_list['short_id'], "评论":shuju['text'] } csv_writer.writerow(dit) def check_for_csv_files(PY1): """ 检查指定目录下是否存在.csv文件。 :param directory: 要检查的目录路径 :return: 如果找到.csv文件返回True,否则返回False """ # 使用glob模块匹配目录下的所有.csv文件 csv_files = glob.glob(os.path.join(PY1, '*.csv')) # 如果找到.csv文件,则csv_files列表不为空,返回True return bool(csv_files) # 示例:检查当前目录下是否存在.csv文件 current_dir = os.getcwd() if check_for_csv_files(current_dir): print("存在.csv文件。") import douyinconnect hello() else: print("没有找到.csv文件。") sleep(2) import quchong