准备工作
1、进入豆瓣网图书频道:https://book.douban.com
2、寻找感兴趣的图书,进入其页面并查看该图书的评论
3、分析评论数据URL地址特性,得到其共有部分为:https://book.douban.com/subject/book_id/comments?
其中book_id为图书在网页地址栏中的编号
编码实现爬虫
# 获取HTML页面 def getHtml(url): try: r = requests.get(url, timeout=30) r.raise_for_status() return r.text except: return '' # 获取评论 def getComment(html): soup = BeautifulSoup(html, 'html.parser') comments_list = [] # 评论列表 comment_nodes = soup.select('.comment > p') for node in comment_nodes: comments_list.append(node.get_text().strip().replace("\n", "") + u'\n') return comments_list # 获取并将评论保存到文件中 def saveCommentText(fpath): pre_url = "https://book.doub