###### 问题遇到的现象和发生背景
###### 问题相关代码,请勿粘贴截图
import requests
import json
import re
import time
import pandas as pd
datas = []
def get_comments():
for i in range(1,2):
url = 'https://api-zero.livere.com/v1/comments/list?callback=jQuery112409283538250152306_1642487705017&limit=10&offset='+str(i)+'&repSeq=4272904&requestPath=%2Fv1%2Fcomments%2Flist&consumerSeq=1020&livereSeq=28583&smartloginSeq=5154&code=&_=1642487705024'
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.62'}
res = requests.get(url,headers=headers)
data =res.text
print('正在爬取第'+str(i)+"页评论")
commentSource = '"content":"(.*?)"'
nameSource = '"name":"(.*?)"'
comments = re.findall(commentSource,data,re.S)
names =re.findall(nameSource,data,re.S)
for name in names:
#name已经通过遍历取出,突然不知道comment怎么遍历取出了,懵逼了,这里怎么写呢
datas.append({
'评论者': name,
'评论内容':comment
})
return datas
if __name__ == '__main__':
get_comments()
df = pd.DataFrame(datas)
df.to_excel('json评论.xlsx',index=False)
###### 运行结果及报错内容
###### 我的解答思路和尝试过的方法
###### 我想要达到的结果
评论者name和评论语comment同时遍历取出至datas