抓取豆瓣某本书的评论

跟着视频学的

张莉 南京大学

先直接上代码

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>

### 回答1: 好的,您需要使用Python编写一个爬虫程序,从豆瓣网站上抓取评论信息。以下是一个简单的示例代码,可以帮助您开始编写: ```python import requests from bs4 import BeautifulSoup # 将目标网页的URL替换为您要抓取评论的电影或书籍页面 url = 'https://book.douban.com/subject/1084336/comments/' # 发送GET请求并获取网页HTML内容 response = requests.get(url) html = response.text # 使用BeautifulSoup解析HTML内容,并提取评论信息 soup = BeautifulSoup(html, 'html.parser') comments = soup.find_all('span', class_='short') for comment in comments: print(comment.text) ``` 您可以将上述代码中的URL替换为您要抓取评论的电影或书籍页面,然后运行程序即可抓取评论信息。请注意,豆瓣网站可能会有反爬虫机制,因此请合理使用爬虫,避免频繁抓取同一页面或大量抓取数据。 ### 回答2: Python爬虫抓取豆瓣评论是通过编写程序来自动化地从豆瓣网站上获取评论数据的过程。下面是一个简单的示例来说明如何使用Python爬虫抓取豆瓣评论。 首先,需要安装Python中的一个网络爬虫库,例如BeautifulSoup、Scrapy或Requests等。这些库可以帮助我们更轻松地处理网页和提取所需的数据。 接下来,我们需要确定要爬取的网页URL。在豆瓣上,每个电影都有一个唯一的URL,其中包含了影片的评论信息。 然后,通过发送HTTP请求来获取网页的源代码。在Python中,可以使用Requests库来发送GET请求,获取到的源代码可以保存在一个变量中。 一旦获取到网页的源代码,就可以使用BeautifulSoup库来解析HTML标签并提取所需的评论数据。通过分析网页的结构,找到评论内容所在的标签,然后使用BeautifulSoup的相关方法来提取评论文本。 最后,可以将提取出的评论数据保存到一个文件中,或者进行相应的数据分析和处理。 需要注意的是,爬取豆瓣评论数据可能涉及到网站的反爬措施,例如限制IP访问频率、验证码等。在进行爬取时,应该尊重网站的规则并避免对其造成过大的负担。 综上所述,Python爬虫可以实现对豆瓣评论抓取,并可以通过相关库来处理网页和提取所需数据。使用Python编写爬虫程序可以简化爬取过程,并可以将抓取到的评论数据用于后续的数据分析和处理。 ### 回答3: Python爬虫是一种自动化程序,可以通过网络爬取数据。豆瓣是一个流行的社交媒体平台,用户可以在其上发布评论和评分。以下是关于用Python爬虫抓取豆瓣评论的说明。 首先,我们需要安装相关的Python库,如Requests和BeautifulSoup。这些库可以帮助我们发送HTTP请求并解析HTML页面。使用这些库可以通过URL获取网页的内容,并从HTML中提取所需的数据。 然后,我们需要找到豆瓣网页上评论的URL地址。在豆瓣电影页面上,评论URL一般在网页的源代码中可以找到。我们可以使用Requests库发送一个GET请求来获取评论页面的HTML内容。 接下来,通过解析HTML内容,我们可以找到评论所在的标签和类名。使用BeautifulSoup库可以帮助我们提取这些数据。我们可以使用find_all()函数来找到特定标签和类名的所有实例,并将评论文本提取出来。 最后,我们可以将提取的评论保存到本地或进行进一步的处理和分析。可以使用Python的文件写入功能将评论数据保存为文本文件。如果需要进行进一步的处理,还可以使用Python的数据处理和分析库,如Pandas和NumPy。 需要注意的是,使用Python爬虫进行数据抓取时,要遵守网站的使用规则。不得滥用爬虫或对网站进行过多的请求,以免给网站带来压力或造成不必要的困扰。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值