import requests import json from pprint import pprint import csv import os import glob 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=7365837826807631104&cursor=0&count=50&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=124.0.0.0&browser_online=true&engine_name=Blink&engine_version=124.0.0.0&os_name=Windows&os_version=10&cpu_core_num=8&device_memory=8&platform=PC&downlink=1.35&effective_type=3g&round_trip_time=400&webid=7346532852764591631&msToken=Zh11fSSG59_VrYd-zMUNRvD38iOfSZq0UDToOsFK2-ult1qo9BQVgXRir1BKb0LGc4rhgEAWTRKbTlsGHxAbwLmpUu768a9jT2xXJkAoCY9YXCXyfHCLLphzpZvU362LnA%3D%3D&a_bogus=d6RZBR8fdEINgDyh53ALfY3q66F3Y2yN0trEMD2fhVv4dL39HMTN9exEsDzvjK8jLG%2FlIeSjy4hbYN9krQVJ8qwfHuix%2F2ohmDSkKl5Q59Rbs1X9CjusrziNmkwUSaBB5JrlrO0MqJKcFmyp09on5JIlO6ZCcHgOEisnOny%3D&verifyFp=verify_lwlm1u72_stNCFCId_qzbH_4cpL_9Iep_AqW2XPQog7ma&fp=verify_lwlm1u72_stNCFCId_qzbH_4cpL_9Iep_AqW2XPQog7ma' 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; douyin.com; device_web_cpu_core=8; device_web_memory_size=8; architecture=amd64; csrf_session_id=5a0c67ee78f30c41eb914110756afbbd; dy_swidth=1280; dy_sheight=720; strategyABtestKey=%221716611306.695%22; 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; passport_assist_user=CkEmsI3D5cT77CMXH_Rw9ayZQNM96ZeoZvKovL2gIhhk4d0xiQiby061YRFQaMu608dWj3YdpLEZ-p6-BWrRGAOxHBpKCjzaC-uRfEc6NzirGheBoZJXZdeJqH_aboX_Xh9_Wqe1NdonJQSRcRvjeh9miUu1WznmuFPPsQfQ0bWAPV4QipzSDRiJr9ZUIAEiAQOwQNbY; n_mh=O0Ud012BYDa2zc36g8-pEsT4HQkknDbOdmVfd9MRc2k; sso_uid_tt=36dab72494b381978af1f8ec95cccd49; sso_uid_tt_ss=36dab72494b381978af1f8ec95cccd49; toutiao_sso_user=99f5355446fd8ab649012219f3e2c00e; toutiao_sso_user_ss=99f5355446fd8ab649012219f3e2c00e; sid_ucp_sso_v1=1.0.0-KGVkODQ0ZGM3ZTc4NGM1NjIwYjhhMzllZGM4OTU3OTM5MmNjZWFhZGYKHwjUmfCgqMzoAhCR0sWyBhjvMSAMMNmDoaEGOAZA9AcaAmxxIiA5OWY1MzU1NDQ2ZmQ4YWI2NDkwMTIyMTlmM2UyYzAwZQ; ssid_ucp_sso_v1=1.0.0-KGVkODQ0ZGM3ZTc4NGM1NjIwYjhhMzllZGM4OTU3OTM5MmNjZWFhZGYKHwjUmfCgqMzoAhCR0sWyBhjvMSAMMNmDoaEGOAZA9AcaAmxxIiA5OWY1MzU1NDQ2ZmQ4YWI2NDkwMTIyMTlmM2UyYzAwZQ; passport_auth_status=558559b4d7486bb6bceb13785c821dd9%2C; passport_auth_status_ss=558559b4d7486bb6bceb13785c821dd9%2C; uid_tt=514b7a348e37156fc7889d2a49ca2f9b; uid_tt_ss=514b7a348e37156fc7889d2a49ca2f9b; sid_tt=028b6962eeeaa62204dfd3ba56e9e135; sessionid=028b6962eeeaa62204dfd3ba56e9e135; sessionid_ss=028b6962eeeaa62204dfd3ba56e9e135; publish_badge_show_info=%220%2C0%2C0%2C1716611360297%22; _bd_ticket_crypt_doamin=2; _bd_ticket_crypt_cookie=775db95da32166f649cdae74b48f8025; __security_server_data_status=1; sid_guard=028b6962eeeaa62204dfd3ba56e9e135%7C1716611363%7C5183985%7CWed%2C+24-Jul-2024+04%3A29%3A08+GMT; sid_ucp_v1=1.0.0-KDg2OWMxMGQwMzhmYTViYzQ3YmMzMjViMDc2OGM4ZTQ5ZWI1M2I1MDAKGwjUmfCgqMzoAhCj0sWyBhjvMSAMOAZA9AdIBBoCaGwiIDAyOGI2OTYyZWVlYWE2MjIwNGRmZDNiYTU2ZTllMTM1; ssid_ucp_v1=1.0.0-KDg2OWMxMGQwMzhmYTViYzQ3YmMzMjViMDc2OGM4ZTQ5ZWI1M2I1MDAKGwjUmfCgqMzoAhCj0sWyBhjvMSAMOAZA9AdIBBoCaGwiIDAyOGI2OTYyZWVlYWE2MjIwNGRmZDNiYTU2ZTllMTM1; xg_device_score=7.592368005596743; download_guide=%223%2F20240525%2F0%22; volume_info=%7B%22isUserMute%22%3Afalse%2C%22isMute%22%3Afalse%2C%22volume%22%3A0.782%7D; pwa2=%220%7C0%7C3%7C0%22; __ac_signature=_02B4Z6wo00f01juiYNAAAIDCzctp98R.7iY7gmRAAOjHyOsi1kV4xyKXuJGBupYuSpCLNWXlgZ78cxNwl0NaVXpT7iWHFDv5XKGjUefXjlXS0bF4XO1qHKAosCRN43rvtJtPIwad8gNOjBiK6f; FOLLOW_NUMBER_YELLOW_POINT_INFO=%22MS4wLjABAAAAsHY3M7WCvAd6-fwDR0tYP32MvxlSPgEfD3dF69gvt3Wg7bkb3YHFIkTg0hifLIWT%2F1716652800000%2F0%2F0%2F1716636117202%22; 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%3A10%2C%5C%22effective_type%5C%22%3A%5C%224g%5C%22%2C%5C%22round_trip_time%5C%22%3A150%7D%22; bd_ticket_guard_client_data=eyJiZC10aWNrZXQtZ3VhcmQtdmVyc2lvbiI6MiwiYmQtdGlja2V0LWd1YXJkLWl0ZXJhdGlvbi12ZXJzaW9uIjoxLCJiZC10aWNrZXQtZ3VhcmQtcmVlLXB1YmxpYy1rZXkiOiJCTlplYVlVb2N5Z1ZqakNXYnptL0RNN1h6OE94VWJ6ZVVETkFBWVVvRFJEMlJTSkxFRkJQOVhjazlCSzd6K0dUMzBnTlBwKzlwWnZ6a0hoQXRrWEdabUE9IiwiYmQtdGlja2V0LWd1YXJkLXdlYi12ZXJzaW9uIjoxfQ%3D%3D; FOLLOW_LIVE_POINT_INFO=%22MS4wLjABAAAAsHY3M7WCvAd6-fwDR0tYP32MvxlSPgEfD3dF69gvt3Wg7bkb3YHFIkTg0hifLIWT%2F1716652800000%2F0%2F1716635590613%2F0%22; home_can_add_dy_2_desktop=%221%22; msToken=jf09o1cUU1UtCf-WCDKt_UOZIAl5To6vSd0Q6H86ea3m-C8DpdJljGjJA5TuXnwZpHKLQUYmuqr6qSd9Kex5dB8sRe3l1bMJIj-xv2GmyE73f6jGwnieVj6gpetVLZXR-g==; odin_tt=31e7648805afb8d4175378f0e75532bdf4aeefcfa31fab2480f12020cd09fc7843ca63aabb07a71c77fb3e5f8131f723; WallpaperGuide=%7B%22showTime%22%3A1716647836149%2C%22closeTime%22%3A0%2C%22showCount%22%3A1%2C%22cursor1%22%3A7%2C%22cursor2%22%3A0%7D; 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%3A0%7D%22; passport_fe_beating_status=true; IsDouyinActive=true', '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 #douyinconnect.hello() else: print("没有找到.csv文件。")