今天抓取一個新聞媒體,36kr的文章內(nèi)容,也是為后面的數(shù)據(jù)分析做相應(yīng)的準(zhǔn)備
創(chuàng)新互聯(lián)公司主營新城網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都App定制開發(fā),新城h5成都小程序開發(fā)搭建,新城網(wǎng)站營銷推廣歡迎新城等地區(qū)企業(yè)咨詢36kr 讓一部分人先看到未來,而你今天要做的事情確實要抓取它的過去。
網(wǎng)址 https://36kr.com/
36kr的頁面是一個瀑布流的效果,當(dāng)你不斷的下拉頁面的時候,數(shù)據(jù)從后臺追加過來,基于此,基本可以判斷它是ajax異步的數(shù)據(jù),只需要打開開發(fā)者工具,就能快速的定位到想要的數(shù)據(jù),我們嘗試一下!
捕獲鏈接如下
https://36kr.com/api/search-column/mainsite?per_page=20&page=1&_=1543840108547
https://36kr.com/api/search-column/mainsite?per_page=20&page=2&_=1543840108547
https://36kr.com/api/search-column/mainsite?per_page=20&page=3&_=1543840108547
https://36kr.com/api/search-column/mainsite?per_page=20&page=4&_=1543840108547在多次嘗試之后,發(fā)現(xiàn)per_page大可以擴(kuò)展到300,但是當(dāng)大于100的數(shù)據(jù),返回的數(shù)據(jù)并不是很理想,所以,我們擬定為100即可,page就是頁碼,這個不斷循環(huán)疊加即可。
上面的參數(shù)還有一個更加重要的值,叫做total_count 總共有多少文章數(shù)目。有這個參數(shù),我們就能快速的拼接出來,想要的頁碼了。
scrapy startproject kr36 scrapy genspider Kr36 "www.gaokaopai.com"
Python資源分享qun 784758214 ,內(nèi)有安裝包,PDF,學(xué)習(xí)視頻,這里是Python學(xué)習(xí)者的聚集地,零基礎(chǔ),進(jìn)階,都?xì)g迎頁面起始地址start_urls為第一頁數(shù)據(jù),之后會調(diào)用parse函數(shù),在函數(shù)內(nèi)容,我們?nèi)カ@取total_count這個參數(shù)
這個地方,需要注意 yield 返回數(shù)據(jù)為Request() 關(guān)于他的詳細(xì)說明,請參照
https://scrapy-chs.readthedocs.io/zh_CN/0.24/topics/request-response.html
所有參數(shù)清單,參數(shù)名字起得好,基本都能代表所有的意思了。比較重要的是url和callback
class scrapy.http.Request(url[, callback, method='GET', headers, body, cookies, meta, encoding='utf-8', priority=0, dont_filter=False, errback])class Kr36Spider(scrapy.Spider):
name = 'Kr36'
allowed_domains = ['36kr.com']
start_urls = ['https://36kr.com/api/search-column/mainsite?per_page=100&page=1&_=']
def parse(self, response):
data = json.loads(response.body_as_unicode())
totle = int(data["data"]["total_count"])
#totle = 201
for page in range(2,int(totle/100)+2):
print("正在爬取{}頁".format(page),end="")
yield Request("https://36kr.com/api/search-column/mainsite?per_page=100&page={}&_=".format(str(page)), callback=self.parse_item)在解析數(shù)據(jù)過程中,發(fā)現(xiàn)有時候數(shù)據(jù)有缺失的情況發(fā)生,所以需要判斷一下 app_views_count , mobile_views_count , views_count , favourite_num 是否出現(xiàn)在字典中。
注意下面代碼中的Kr36Item類,這個需要提前創(chuàng)建一下
Kr36Item
class Kr36Item(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
app_views_count = scrapy.Field() # APP觀看數(shù)量
mobile_views_count = scrapy.Field() # 移動端觀看數(shù)量
views_count = scrapy.Field() # PC觀看數(shù)量
column_name = scrapy.Field() # 類別
favourite_num = scrapy.Field() # 收藏數(shù)量
title = scrapy.Field() # 標(biāo)題
published_at = scrapy.Field() # 發(fā)布時間
is_free = scrapy.Field() # 是否免費(fèi)
username = scrapy.Field() def parse_item(self,response):
data = json.loads(response.body_as_unicode())
item = Kr36Item()
for one_item in data["data"]["items"]:
print(one_item)
item["app_views_count"] = one_item["app_views_count"] if "app_views_count" in one_item else 0# APP觀看數(shù)量
item["mobile_views_count"] = one_item["mobile_views_count"] if "mobile_views_count" in one_item else 0 # 移動端觀看數(shù)量
item["views_count"] = one_item["views_count"] if "views_count" in one_item else 0 # PC觀看數(shù)量
item["column_name"] = one_item["column_name"] # 類別
item["favourite_num"] = one_item["favourite_num"] if "favourite_num" in one_item else 0 # 收藏數(shù)量
item["title"] = one_item["title"] # 標(biāo)題
item["published_at"] = one_item["published_at"] # 發(fā)布時間
item["is_free"] = one_item["is_free"] if "is_free" in one_item else 0# 是否免費(fèi)
item["username"] = json.loads(one_item["user_info"])["name"]
yield item最后打開settings.py中的pipelines編寫數(shù)據(jù)持久化代碼
ITEM_PIPELINES = {
'kr36.pipelines.Kr36Pipeline': 300,
}import os
import csv
class Kr36Pipeline(object):
def __init__(self):
store_file = os.path.dirname(__file__)+'/spiders/36kr.csv'
self.file = open(store_file,"a+",newline="",encoding="utf_8_sig")
self.writer = csv.writer(self.file)
def process_item(self, item, spider):
try:
self.writer.writerow((
item["title"],
item["app_views_count"],
item["mobile_views_count"],
item["views_count"],
item["column_name"],
item["favourite_num"],
item["published_at"],
item["is_free"],
item["username"]
))
print("數(shù)據(jù)存儲完畢")
except Exception as e:
print(e.args)
def close_spider(self,spider):
self.file.close()
Python資源分享qun 784758214 ,內(nèi)有安裝包,PDF,學(xué)習(xí)視頻,這里是Python學(xué)習(xí)者的聚集地,零基礎(chǔ),進(jìn)階,都?xì)g迎運(yùn)行上述代碼,沒有做過多的處理,也沒有調(diào)整并發(fā)速度,也沒有做反爬措施。跑了一下,大概獲取到了69936條數(shù)據(jù),和預(yù)估的差了300多條,問題不大,原因沒細(xì)查。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
文章題目:Python爬蟲入門【18】:36氪(36kr)數(shù)據(jù)抓取scrapy-創(chuàng)新互聯(lián)
URL地址:http://chinadenli.net/article26/cdohcg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、營銷型網(wǎng)站建設(shè)、動態(tài)網(wǎng)站、網(wǎng)站維護(hù)、云服務(wù)器、自適應(yīng)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容