極簡(jiǎn)Scrapy爬蟲3:爬取多級(jí)頁(yè)面

運(yùn)行環(huán)境:
* Python 2.7.12  
* Scrapy 1.2.2
* Mac OS X 10.10.3 Yosemite

繼續(xù)爬取Scrapy 1.2.2文檔提供的練習(xí)網(wǎng)址:

"http://quotes.toscrapy.com"

可以暫時(shí)不用考慮爬蟲被封的情況,用于初級(jí)爬蟲練習(xí)。

目標(biāo)

爬取每位作者的介紹。

步驟1:通過(guò)scrapy shell定位

分析網(wǎng)頁(yè)結(jié)構(gòu)

每位作者都有一個(gè)詳細(xì)的介紹頁(yè)面。

首先需要找到介紹的入口網(wǎng)址。通過(guò)對(duì)每一條名言的html內(nèi)容分析

<div class="quote" itemscope="" itemtype="http://schema.org/CreativeWork">
        <span class="text" itemprop="text">“The world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.”</span>
        <span>by 
          <small class="author" itemprop="author">Albert Einstein</small>
          <a href="/author/Albert-Einstein">(about)</a>
        </span>
        <div class="tags">
            Tags:
            <meta class="keywords" itemprop="keywords" content="change,deep-thoughts,thinking,world">             
            <a class="tag" href="/tag/change/page/1/">change</a>            
            <a class="tag" href="/tag/deep-thoughts/page/1/">deep-thoughts</a>            
            <a class="tag" href="/tag/thinking/page/1/">thinking</a>            
            <a class="tag" href="/tag/world/page/1/">world</a>           
        </div>
</div>

可以發(fā)現(xiàn)作者介紹的網(wǎng)址在:

        <a href="/author/Albert-Einstein">(about)</a>

測(cè)試定位

可以使用scrapy shell命令行,測(cè)試是否能定位到作者介紹的鏈接。在命令行中,使用

$ scrapy shell 'http://quotes.toscrape.com'

其中網(wǎng)址建議加上引號(hào)。Windows系統(tǒng)使用雙引號(hào)。

方法1:CSS選擇相鄰兄弟標(biāo)簽

CSS的相鄰兄弟選擇,使用small.author+a::attr(href)定位。

  • small.author:定位到類名為author的small標(biāo)簽
  • “+”:表示緊鄰的兄弟標(biāo)簽
  • +a::attr(href)表示緊鄰的a標(biāo)簽,提取其中的href鏈接。
>>> author = response.css('small.author+a::attr(href)').extract_first()
>>> author
u'/author/Albert-Einstein'

方法2:XPath選擇第二個(gè)span標(biāo)簽

//div[@class="quote"]/span[2]/a/@href的意思是:

  • //div[@class="quote"]:定位在類名為quote的div標(biāo)簽中
  • /span[2]:選擇第2個(gè)span下的a標(biāo)簽
  • /a/@href:選擇a標(biāo)簽的href屬性。
>>> au = response.xpath('//div[@class="quote"]/span[2]/a/@href').extract_first() 
>>> au
u'/author/Albert-Einstein'

方法3:XPath選擇相鄰兄弟標(biāo)簽

XPath中"http://"表示所有的元素。//small[@class="author"]/following-sibling::a[1]/@href中:

  • //small[@class="author"]:表示定位在類名author為small的標(biāo)簽
  • /following-sibling::a[1]:剛才small標(biāo)簽后面的同級(jí)別兄弟標(biāo)簽中第一個(gè)a標(biāo)簽
  • /@href:提取href鏈接屬性。
>>> auth =response.xpath('//small[@class="author"]/following-sibling::a[1]/@href').extract_first()
>>> auth
u'/author/Albert-Einstein'

步驟2:代碼編寫與運(yùn)行

爬蟲名字更換為:name = 'quotes_2_3'

上1個(gè)爬蟲中的_parse()函數(shù)中,對(duì)每條名言的循環(huán)里面增加一個(gè)作者簡(jiǎn)介的爬取,增加三行內(nèi)容。

            author_page = response.css('small.author+a::attr(href)').extract_first()
            authro_full_url = response.urljoin(author_page)
            yield scrapy.Request(authro_full_url, callback=self.parse_author)

表示找到作者介紹頁(yè)面的鏈接,再拼接成絕對(duì)路徑的鏈接,最后發(fā)出請(qǐng)求(scrapy.Request)并用回調(diào)函數(shù)parse_author()對(duì)作者的介紹頁(yè)面解析。

對(duì)于函數(shù)parse_author(),結(jié)構(gòu)與內(nèi)容與普通頁(yè)面解析大同小異。

    def parse_author(self,response):
        yield{
            'author': response.css('.author-title::text').extract_first(),
            'author_born_date': response.css('.author-born-date::text').extract_first(),
            'author_born_location': response.css('.author-born-location::text').extract_first(),
            'authro_description': response.css('.author-born-location::text').extract_first(),
        }

完整的代碼如下

import scrapy

class QuotesSpider(scrapy.Spider):
    name = 'quotes_2_3'
    start_urls = [
        'http://quotes.toscrape.com',
    ]
    allowed_domains = [
        'toscrape.com',
    ]

    def parse(self,response):
        for quote in response.css('div.quote'):
            yield{
                'quote': quote.css('span.text::text').extract_first(),
                'author': quote.css('small.author::text').extract_first(),
                'tags': quote.css('div.tags a.tag::text').extract(),
            }
            author_page = response.css('small.author+a::attr(href)').extract_first()
            authro_full_url = response.urljoin(author_page)
            yield scrapy.Request(authro_full_url, callback=self.parse_author)
            
        next_page = response.css('li.next a::attr("href")').extract_first()
        if next_page is not None:
            next_full_url = response.urljoin(next_page)
            yield scrapy.Request(next_full_url, callback=self.parse)

    def parse_author(self,response):
        yield{
            'author': response.css('.author-title::text').extract_first(),
            'author_born_date': response.css('.author-born-date::text').extract_first(),
            'author_born_location': response.css('.author-born-location::text').extract_first(),
            'authro_description': response.css('.author-born-location::text').extract_first(),
        }

運(yùn)行使用

$ scrapy crawl quotes_2_3 -o results_2_3_01.json即可得到j(luò)son文件的結(jié)果。

最后編輯于
?著作權(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),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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