爬取簡書首頁信息洼冻,包括:標(biāo)題,作者隅很,發(fā)表時(shí)間忠藤,閱讀量堤瘤,評(píng)論數(shù),點(diǎn)贊數(shù),打賞數(shù)齿坷,所投專題
因?yàn)樽约嚎催^一篇?jiǎng)e人寫的爬取趕集網(wǎng)的信息述呐,再加上也沒事做篮迎,就想著模仿著試一試甫菠,反正做的過程中是很痛苦的,好多基礎(chǔ)的都不會(huì)婴谱,就只能邊查資料邊學(xué)習(xí)了蟹但,硬著頭皮弄了一天,終于有了結(jié)果谭羔。
先上結(jié)果圖矮湘,存儲(chǔ)在mongodb中。
爬取的數(shù)據(jù)
好了口糕,記錄一下做的過程吧。
1.查看要爬取頁面的源碼
經(jīng)過查看元素磕蛇,發(fā)現(xiàn)在 ul 標(biāo)簽下的不同的 li 對(duì)應(yīng)不同的文章景描,而每個(gè)文章獲取標(biāo)題、作者等等的方法都一樣秀撇,那只需獲取這個(gè)文章列表超棺,然后讓他們執(zhí)行相同的操作即可獲得所需數(shù)據(jù)。
2.查找自己所需數(shù)據(jù)所在的標(biāo)簽范圍
作者名和文章發(fā)布時(shí)間
標(biāo)題
閱讀量呵燕、評(píng)論數(shù)棠绘、點(diǎn)贊數(shù)和打賞數(shù)
3.具體的爬取數(shù)據(jù)過程
#encoding=utf-8
import requests,pymongo
from bs4 import BeautifulSoup
def get_info(url):
r=requests.get(url) # 向服務(wù)器請(qǐng)求頁面
r.encoding='utf-8' # 標(biāo)明編碼為utf-8,以免出現(xiàn)解碼錯(cuò)誤
soup=BeautifulSoup(r.text,'html.parser') # 以html.parser方式對(duì)頁面進(jìn)行解析
articlelist=soup.select('ul.note-list li') #獲取首頁文章列表
#print articlelist
for article in articlelist:
title=article.select('a.title')[0].text
author=article.select('a.blue-link')[0].text
date=article.select('span.time')[0].get('data-shared-at')
if article.find_all('a',attrs={'class':'collection-tag'}): #因?yàn)橛行┪恼聸]有所屬分類,所以先判斷再扭,以免獲取為None
collection=article.select('div.meta a.collection-tag')[0].text
readnum=article.select('div.meta a:nth-of-type(2)')[0].text #:nth-of-type(n) 選擇器匹配屬于父元素的特定類型的第 N 個(gè)子元素的每個(gè)元素.
if article.find_all('i',attrs={'class':'iconfont ic-list-comments'}):
commentnum=article.select('div.meta a:nth-of-type(3)')[0].text
else:
commentnum=0
else: #如果沒有所屬分類氧苍,那么閱讀量就是第一個(gè)a標(biāo)簽里的內(nèi)容
collection='所屬分類無'
readnum=article.select('div.meta a:nth-of-type(1)')[0].text
if article.find_all('i',attrs={'class':'iconfont ic-list-comments'}):
commentnum=article.select('div.meta a:nth-of-type(2)')[0].text
else:
commentnum=0
likenum=article.select('div.meta span:nth-of-type(1)')[0].text
if article.find_all('i',attrs={'class':'iconfont ic-list-money'}):
money=article.select('div.meta span:nth-of-type(2)')[0].text
else:
money=0
data = {
'title' : title,
'author' :author,
'date': date,
'readnum' : readnum,
'commentnum' :commentnum,
'likenum' : likenum,
'money' : money,
'collection' : collection
}
jianshu.insert_one(data) #將獲取的數(shù)據(jù)存入到數(shù)據(jù)庫中
client = pymongo.MongoClient('localhost',27017) # 連接mongodb
test = client['test'] # 創(chuàng)建一個(gè)名叫test的數(shù)據(jù)庫文件
jianshu = test['jianshu'] # 創(chuàng)建一個(gè)jianshu的表
get_info('http://www.reibang.com/')