上回我們說到 WOW?。?/p> awesome!! 怎么會(huì)有這么牛逼的框架 wow??! awesome!! 用 scrapy 來爬取數(shù)據(jù) 豈!不!是!非!常!爽! wow??! 接下來就是我獨(dú)享的moment 哦不 接下來就是 學(xué)習(xí) python 的正確姿勢 我們已經(jīng)創(chuàng)建了爬取糗事百科的項(xiàng)目 并且把糗事百科的前兩頁的作者和段子爬取到 json 文件了 這次 我們將我們要爬取所有的數(shù)據(jù) 使用 scrapy 存儲到 mangodb 中 在此之前還是先介紹一下我們使用 scrapy 創(chuàng)建出來的文件目錄 各個(gè)文件代表的都是啥意思 免得又有些 b 友當(dāng)場懵逼 我們從上往下依個(gè)介紹一下 這個(gè) spiders 目錄呢 就是用來存放我們寫爬蟲文件的地方 items.py 就是用來定義我們要存儲數(shù)據(jù)的字段 middlewares.py 就是中間件,在這里面可以做一些在爬蟲過程中想干的事情,比如爬蟲在響應(yīng)的時(shí)候你可以做一些操作 pipelines.py 這是我們用來定義一些存儲信息的文件,比如我們要連接 MySQL或者 MongoDB 就可以在這里定義 settings.py 這個(gè)文件用來定義我們的各種配置,比如配置請求頭信息等 以上就是 scrapy 生成的目錄中主要文件的作用 ok 接下來我們就進(jìn)入代碼中 我們上次創(chuàng)建了 QiushiSpider 來寫我們的爬蟲 當(dāng)時(shí)我們只是獲取了前兩頁的數(shù)據(jù) 我們要獲取所有頁面的數(shù)據(jù)怎么玩呢 打開糗事百科的鏈接可以看到 13 頁的數(shù)據(jù) 其實(shí)按照以前我們直接寫個(gè) for 循環(huán)就可以了 不過我們這次還可以使用 scrapy 的 follow 函數(shù) 具體使用是這樣的 我們先獲取下一頁的鏈接 由于下一頁這個(gè)按鈕都是在最后一個(gè) li 標(biāo)簽中的 所以用 xpath 獲取就這樣 next_page = response.xpath('//*[@id="content-left"]/ul/li[last()]/a').attrib['href'] 接著我們就可以讓它去請求下一頁的內(nèi)容數(shù)據(jù)了 if next_page is not None: yield response.follow(next_page, callback=self.parse) 你也可以用 urljoin 的方式 # if next_page is not None: # next_page = response.urljoin(next_page) # yield scrapy.Request(next_page, callback=self.parse) 這樣我們就可以獲取到所有頁面的數(shù)據(jù)了 接下來我們要把所有的數(shù)據(jù)保存到數(shù)據(jù)庫 首先我們在 items.py 中定義一下我們要存儲的字段 import scrapy
class QiushibaikeItem(scrapy.Item): # define the fields for your item here like: # name = scrapy.Field() author = scrapy.Field() content = scrapy.Field() _id = scrapy.Field() 接著我們在 parse 方法中將獲取到的數(shù)據(jù)賦值給 item 具體來說就是這樣 def parse(self, response):
content_left_div = response.xpath('//*[@id="content-left"]') content_list_div = content_left_div.xpath('./div')
for content_div in content_list_div: item = QiushibaikeItem() item['author'] = content_div.xpath('./div/a[2]/h2/text()').get() item['content'] = content_div.xpath('./a/div/span/text()').getall() item['_id'] = content_div.attrib['id'] yield item
next_page = response.xpath('//*[@id="content-left"]/ul/li[last()]/a').attrib['href']
if next_page is not None: yield response.follow(next_page, callback=self.parse) 第 7 行就是獲取我們剛剛定義的 item 的類 8-10 行就是相應(yīng)的賦值 那么我們定義好了要存儲的字段以及寫好了數(shù)據(jù)爬取 接下來還有一步 就是定義好我們要存儲的數(shù)據(jù)庫 到 pipelines.py 中 class QiushibaikePipeline(object):
def __init__(self): self.connection = pymongo.MongoClient('localhost', 27017) self.db = self.connection.scrapy self.collection = self.db.qiushibaike
def process_item(self, item, spider): if not self.connection or not item: return self.collection.save(item)
def __del__(self): if self.connection: self.connection.close() 在這里我們連接到本地的 MongoDB 建立了 scrapy 數(shù)據(jù)庫及以下的 qiushibaike 接下來還要在 settings.py 文件中配置下 # See https://doc./en/latest/topics/item-pipeline.html ITEM_PIPELINES = { 'qiushibaike.pipelines.QiushibaikePipeline': 300, } 這樣才可以使用到pipelines 當(dāng)然我們還可以在 settings.py 里面做更多的設(shè)置 比如設(shè)置請求頭 # Crawl responsibly by identifying yourself (and your website) on the user-agent USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/73.0.3683.86 Chrome/73.0.3683.86 Safari/537.36' ok 搞定了之后 我們使用命令來抓取一下 scrapy crawl qiushibaike 運(yùn)行之后 我們打開 MongoDB 看看 可以看到 所有的數(shù)據(jù)就被我爬取到 MongoDB 了 ok 以上就是 scrapy 的簡單又牛逼的操作 更多 scrapy 的牛逼操作可以前往 https://doc. 了解 這兩篇所涉及的源代碼已經(jīng)上傳 可以在公眾號后臺發(fā)送 scrapy 獲取 那么 我們下回見 peace 對了 有個(gè)事 你希望接下來這個(gè)公眾號能有更多什么教程 例如 爬蟲實(shí)戰(zhàn)? ubuntu? vim? ...? 請掃下方的碼評論告訴我一下 |
|