【Python爬蟲】統(tǒng)計(jì)各自作業(yè)完成次數(shù)

import requests
from bs4 import BeautifulSoup
import re

user_agent='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'
headers={'User-Agent':user_agent}
base_url='http://www.itdecent.cn/c/1b31f26b6af0?order_by=added_at&page='

authorList={}

def download(url):
    r=requests.get(url,headers=headers)
    return r
def get_article_num(url):   #獲取文章總數(shù)
    r = download(url)
    soup = BeautifulSoup(r.text, 'lxml')
    special_topic_info = soup.find('div', class_='info').text.strip()  #
    article_num = int(re.search(r'\d+', special_topic_info).group())
    return article_num
def run():
    page_index = 1
    num=0
    while num<=article_num:
        print("第{}頁作者...".format(page_index))
        r=download(base_url + str(page_index))
        soup=BeautifulSoup(r.text,'lxml')
        author=soup.find_all('a',class_='blue-link')
        article=soup.find_all('li',id=re.compile(r'\d+'))
        for i in range(len(author)):
            if(author[i].text not in authorList):
                authorList[author[i].text]=1
            else:
                authorList[author[i].text] = 1+authorList[author[i].text]
        if len(author) == 0: break
        print(len(author))
        num += len(author)
        page_index += 1

    for k, v in authorList.items():
        print(k + 5 * '  ' + str(v))
article_num=get_article_num(base_url + str(1))
run()

3.png
4.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容