爬虫 - 豆瓣网《小王子》热评

# -*- coding: utf-8 -*-
'''
‘theLittlePrinces’
@author: LU
'''

import requests,re,time
from bs4 import BeautifulSoup

count = 0 #热评数量
i = 0 #页码
s = 0 #评分总和
count_s = 0
count_del = 0 #超过50的数量
lst_stars = [] #评分列表

while count<50:
    try:
        r = requests.get('https://book.douban.com/subject/1084336/comments/hot?p='+str(i+1)) #翻页
    except Exception as err:
        print(err)
        break
    
    soup = BeautifulSoup(r.text, 'lxml')
    comments = soup.find_all('span', 'short') #寻找评论所在的行(评论行的标签是span,属性内容是short);find_all方法返回的是一个列表
    for item in comments:
        count += 1
        if count>50:
            count_del += 1
        else:
            print(count, item.string)
    
    
    pattern = re.compile('<span class="user-stars allstar(.*?)rating"') #找评分
    p = re.findall(pattern, r.text) #返回列表p
    for star in p:
        lst_stars.append(int(star))
        
    time.sleep(5)
    i += 1
    for star in lst_stars[:-count_del]:
        s += int(star)
        
if count >= 50:
    print(s//(len(lst_stars)-count_del))
  • 运行结果
    在这里插入图片描述

链接:https://pan.baidu.com/s/1xcnggj3uc1UWpM6Kbk7A6A
提取码:dowt

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值