抓取豆瓣某本书的评论

跟着视频学的

张莉 南京大学

先直接上代码

import re, time
import requests
from bs4 import BeautifulSoup
from bs4.element import Tag

def getAuthor(data):
    rate = 0
    soup = BeautifulSoup(data, 'lxml')
    comments = soup.find_all('span', "comment-info")  # name rate
    for comment in comments:
        pattern = re.compile('span class="user-stars allstar(.*?) rating"')
        p = re.findall(pattern, str(comment))
        for star in p:
            rate = int(star)
            break
        soup = BeautifulSoup(str(comment), 'lxml')
        comments = soup.find_all('a')
        for item in comments:
            return item.string, rate
            break
        break

def getContext(data):
    soup = BeautifulSoup(data, 'lxml')
    comments = soup.find_all('p', "comment-content")
    for comment in comments:
        return comment.string
        break

index = 0
count = 0
NUM = 50
while count < NUM:
    r = requests.get('https://book.douban.com/subject/1021056/comments/hot?p='+str(index+1))
    index += 1
    soup = BeautifulSoup(r.text, 'lxml')
    comments = soup.find_all('li', 'comment-item')
    for comment in comments:
        name, rate = getAuthor(str(comment))
        context = getContext(str(comment))
        print(str(count+1))
        print("\twriter:  ", name, " - ", rate)
        print("\tcontext: ", context)
        count+=1
        if count >= NUM:
            break
    time.sleep(2)

地址

https://book.douban.com/subject/bookid/comments/hot

bookid用要抓的书的id替换

第一页 最热评论  https://book.douban.com/subject/1021056/comments/hot 或 ?p=2

第2页 最热评论 https://book.douban.com/subject/1021056/comments/hot?p=2

查看网页源代码后发现, 每个评论的构成如下:

<li class="comment-item" data-cid="274376913">
            <div class="avatar">
                <a title="夕雾" href="https://www.douban.com/people/1299702/">
                    <img src="https://img3.doubanio.com/icon/u1299702-71.jpg">
                </a>
            </div>
        <div class="comment">
            <h3>
                <span class="comment-vote">
                    <span id="c-274376913" class="vote-count">0</span>
                        <a href="javascript:;" id="btn-274376913" class="j a_show_login" data-cid="274376913">有用</a>
                </span>
                <span class="comment-info">
                    <a href="https://www.douban.com/people/1299702/">夕雾</a>
                        <span class="user-stars allstar30 rating" title="还行"></span>
                    <span>2010-07-24</span>
                </span>
            </h3>
            <p class="comment-content">哎哟喂我也看过</p>
        </div>
    </li>
上述可由 

soup = BeautifulSoup(r.text, 'lxml')
comments = soup.find_all('li', 'comment-item')  获取
然后 
用户姓名 在span comment-info块的 a 标签中
	  soup.find_all('span', "comment-info")	
          comments = soup.find_all('a') 
          comment.string
用户评论 在p comment-content块中
          comments = soup.find_all('p', "comment-content")
   comment.string

用户评分 在span comment-info块的 span标签中,要正则匹配user-stars allstar[00] rating,两个数字部分要匹配

 pattern = re.compile('span class="user-stars allstar(.*?) rating"')

  re.findall(pattern, str(comment))


这里正则有点不大懂 'span class="user-stars allstar(.*?) rating"' (.*?) 代表两个数字?


另外 comments = soup.find_all( ... ) 返回的是 bs4.element.ResultSet, 可以直接当成列表使用

而 for item in comments中   item类型是bs4.element.Tag

item.string, 返回标签之间内容   如 哎哟喂我也看过

str(item) 是整个内容         如  <p class="comment-content">哎哟喂我也看过</p>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值