python网络爬虫从入门到实践,第四章第二节测试代码

这里给出的是这本书第四章第二节,即通过浏览器解析动态网页地址爬取评论的代码

import requests
from bs4 import BeautifulSoup
import json
comment_file=open('./comment.txt','w')
#获取内容
link="https://api-zero.livere.com/v1/comments/list?callback=jQuery112403463819624416844_1546351564784&limit=10&repSeq=4272904&requestPath=%2Fv1%2Fcomments%2Flist&consumerSeq=1020&livereSeq=28583&smartloginSeq=5154&_=1546351564786"
header={"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36",
      "Host":"api-zero.livere.com"} 
comment=requests.get(link,headers=header)
#解析
com_text=comment.text
com_extract=com_text[com_text.find('{'):-2]
com_json=json.loads(com_extract)
com_list=com_json['results']['parents']
for x in com_list:
    user=x['name']
    message=x['content']
    print(user+":"+message)
#存储数据
for i in range(1,6):
    link_i="https://api-zero.livere.com/v1/comments/list?callback=jQuery112403463819624416844_1546351564784&limit=10&offset="+str(i)+"&repSeq=4272904&requestPath=%2Fv1%2Fcomments%2Flist&consumerSeq=1020&livereSeq=28583&smartloginSeq=5154&_=1546393421446"
    comment_i=requests.get(link_i,headers=header)
    json_com_i=json.loads(comment_i.text[comment_i.text.find('{'):-2])
    com_i_list=json_com_i['results']['parents']
    comment_file.write("第"+str(i)+"页评论"+':\n')
    for m in com_i_list:
        user_x=m['name']
        message_i=m['content']
        comment_file.write(user_x+":"+message_i+'\n')
comment_file.close()
file_save=open('./webtxt.txt','w',encoding='utf-8')
file_save.write(com_text)
file_save.close()

你可能感兴趣的:(Python)