1. 程式人生 > >豆瓣網《小王子》熱評

豆瓣網《小王子》熱評

# -*- 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))
  • 執行結果
    在這裡插入圖片描述