import requests
import csv
#保存数据
f=open('快手评论19.txt',mode='a',encoding='utf-8',newline='')
csv_writer=csv.writer(f)
csv_writer.writerow(['comment'])
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.81',
#did=web_bf55ab6c2d565d791b00f378cf0ad245; didv=1694483268622; kpf=PC_WEB; clientid=3; kpn=KUAISHOU_VISION
#did=web_bf55ab6c2d565d791b00f378cf0ad245; didv=1694483268622; kpf=PC_WEB; clientid=3; kpn=KUAISHOU_VISION
'cookie':'did=web_bf55ab6c2d565d791b00f378cf0ad245; didv=1694483268622; kpf=PC_WEB; clientid=3; userId=132516133; kuaishou.server.web_st=ChZrdWFpc2hvdS5zZXJ2ZXIud2ViLnN0EqABS_riA4fyZbFAAVBR2tVwS8FvZ9e30oKuGLntt5dA6-sjjQcRmK4VFCmPr9P4wQbZecl4TWVwomrra9KpNsszw3wVuAm4VHKxwd66sCntEdLQl4kpC0rHp82deZLl7Z9_oNb7yltyjZuIQPTRObPjOJAqhLkBNt4N20ktDMHhhzf310iEtErd_1mMPWDNNgpHt6AU7XyCSKjGzDbKh8Ub4hoS7YoRGiN2PM_7zCD1Dj9m5oYoIiDEcxyqnYA3b7ZyFHSSp9EBaaGkrdMCn82_7tP9mK3PsCgFMAE; kuaishou.server.web_ph=25ec7cc74dc4e28961b584395bab01cea7cc; kpn=KUAISHOU_VISION',
#https://www.kuaishou.com/short-video/3xj3qiksk3ht8pe?authorId=3xyfnq5qmwtw7iu&streamSource=search&searchKey=%E4%B8%89%E8%83%8E%E5%AD%95%E5%A6%88&area=searchxxnull
#https://www.kuaishou.com/short-video/3xvy6njb6u3jvqs?authorId=3xyrd52b4kqan7k&streamSource=samecity&area=nearbyxxnull
'Referer':'https://www.kuaishou.com/short-video/3xttih7au72uz7w?authorId=3xv5fuk2yx92b5s&streamSource=search&area=searchxxnull&searchKey=%E6%80%80%E5%AD%95%E4%B8%89%E5%8F%B0',
'Host':'www.kuaishou.com',
}
url1=input('输入想要获取的视频链接:')
a=url1.split("/")[4]
b=a.split("?")[0]
pcursor=[]
for i in range(1,50):
json={
'operationName': 'commentListQuery',
'query':'query commentListQuery($photoId: String, $pcursor: String) {\n visionCommentList(photoId: $photoId, pcursor: $pcursor) {\n commentCount\n pcursor\n rootComments {\n commentId\n authorId\n authorName\n content\n headurl\n timestamp\n likedCount\n realLikedCount\n liked\n status\n authorLiked\n subCommentCount\n subCommentsPcursor\n subComments {\n commentId\n authorId\n authorName\n content\n headurl\n timestamp\n likedCount\n realLikedCount\n liked\n status\n authorLiked\n replyToUserName\n replyTo\n __typename\n }\n __typename\n }\n __typename\n }\n}\n',
'variables': {'photoId': f"{b}", 'pcursor': f"{pcursor}"}#photoId是视频号
}
url='https://www.kuaishou.com/graphql'
response =requests.post(url=url, json=json,headers=headers)
json_data=response.json()
feeds=json_data['data']['visionCommentList']['rootComments']
pcursor=json_data['data']['visionCommentList']['pcursor']
for data in feeds:
comment=data['content']
print(comment)
csv_writer.writerow([comment])
请求头要根据自己的实际情况填写。