當(dāng)前位置:
首頁(yè) >
爬取CSDN最新月份所写的文章的最高阅读量文章(以及统计整个月所写的文章的阅读量的累积和)
發(fā)布時(shí)間:2025/4/16
43
豆豆
生活随笔
收集整理的這篇文章主要介紹了
爬取CSDN最新月份所写的文章的最高阅读量文章(以及统计整个月所写的文章的阅读量的累积和)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
項(xiàng)目簡(jiǎn)述
所用工具:python 3
統(tǒng)計(jì)中,在最下面的主函數(shù)中,url就是我們需要放的初始鏈接。
比如,我放的鏈接就是https://blog.csdn.net/a19990412/
這個(gè)頁(yè)面的具體效果,可以點(diǎn)擊上面鏈接看到,也可以,直接看下面圖片
大概就是這樣的一個(gè)界面。然后只要把這個(gè)網(wǎng)頁(yè)的url放到下面的函數(shù)中,就可以直接獲取對(duì)應(yīng)的url的信息(具體信息如題目所說(shuō))
具體內(nèi)容簡(jiǎn)介:
就是爬取最新的寫(xiě)作頁(yè)的所有文章的閱讀量。
通過(guò)編織的爬蟲(chóng)去統(tǒng)計(jì)一下數(shù)據(jù)。
例如,在我寫(xiě)這篇文章的時(shí)候,運(yùn)行這個(gè)代碼,輸出結(jié)果是
In [date]: 2018年5月 原 Fiddler捕捉數(shù)據(jù)包得到幾乎全是加密過(guò)的tunnel to【解決方法】 Its count is 150 The Sum of all the article is 365代碼
import requests from bs4 import BeautifulSoup import redef cal(soup):global thebestOne_countglobal thebestOne_nameglobal Sum_countdiv_article = soup.find('div', attrs={'class': "article-list"})divs = div_article.find_all('div', attrs={'class': "article-item-box csdn-tracking-statistics"})for d in divs:co = int(re.search('\d+', d.find('span', attrs={'class': "read-num"}).text).group())if co > thebestOne_count:h4 = d.find('h4').text.replace('\n', '').replace(' ', '')thebestOne_name = h4thebestOne_count = coSum_count += codef main(url):global dateheaders = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36'}res = requests.get(url, headers=headers)soup = BeautifulSoup(res.text, 'lxml')try:a = soup.find('aside').find('ul', attrs={'class': 'archive-list'}).find('a')date = list(filter(lambda x: x and x != '\n', a.text.split(' ')))[0]href = a['href']except Exception as e:print('[Error]', e.args)else:res = requests.get(href, headers=headers)soup = BeautifulSoup(res.text, 'lxml')ids = soup.find('div', attrs={'id': 'pageBox'})ids_string = str(ids)data = re.findall('data-page="(\d+)"', ids_string)cal(soup)if data:data = data[1:]for d in data:newhref = href + d + '?'res = requests.get(newhref, headers=headers)soup = BeautifulSoup(res.text, 'lxml')cal(soup)if __name__ == '__main__':Sum_count = 0thebestOne_name = ''thebestOne_count = 0date = ''url = 'https://blog.csdn.net/a19990412/'main(url)print('In [date]: %s' % date, thebestOne_name, '\nIts count is %d' % thebestOne_count)print('The Sum of all the article is %d' % Sum_count)總結(jié)
以上是生活随笔為你收集整理的爬取CSDN最新月份所写的文章的最高阅读量文章(以及统计整个月所写的文章的阅读量的累积和)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【requests】Python轻松爬取
- 下一篇: matplotlib显示中文(显现中文之