1 import requests 2 import time 3 import re 4 5 6 7 p=0 8 while p<=0: 9 p+=1 10 url = "https://m.weibo.cn/api/comments/show?id=4257289713596342&page="+str(p) #杨洋为例 11 html = requests.get(url) 12 print(html) 13 try: 14 for c in range(len(html.json()['data']['data'])): 15 data=html.json()['data']['data'][c]['text'] 16 with open('comments.txt','a',encoding='utf-8') as f: 17 comments=''.join(re.findall('[u4e00-u9fa5]',data)) 18 print(comments) 19 f.write(comments+' '*2) 20 except: 21 None
效果如下所示: