日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/1/17 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 爬虫 - 豆瓣网《小王子》热评 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • url:https://book.douban.com/subject/1084336/comments/hot

  • 要求:抓取前50條短評內容并計算評分(有的評論中并不包含評分)的平均值。

  • 熱評格式:

  • 評分格式:

  • 代碼

# -*- coding: utf-8 -*- ''' ‘theLittlePrinces’ @author: LU '''import requests,re,time from bs4 import BeautifulSoupcount = 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)breaksoup = BeautifulSoup(r.text, 'lxml')comments = soup.find_all('span', 'short') #尋找評論所在的行(評論行的標簽是span,屬性內容是short);find_all方法返回的是一個列表for item in comments:count += 1if count>50:count_del += 1else:print(count, item.string)pattern = re.compile('<span class="user-stars allstar(.*?)rating"') #找評分p = re.findall(pattern, r.text) #返回列表pfor star in p:lst_stars.append(int(star))time.sleep(5)i += 1for 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

總結

以上是生活随笔為你收集整理的爬虫 - 豆瓣网《小王子》热评的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。