1. 程式人生 > >Python3 Scrapy框架學習二:爬取豆瓣電影Top250

Python3 Scrapy框架學習二:爬取豆瓣電影Top250

開啟專案裡的items.py檔案,定義如下變數,

import scrapy
from scrapy import Item,Field


class DoubanItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    movie = Field()
    year = Field()
    star = Field()
    vote = Field()
    quote = Field()

然後開啟專案裡的spiders資料夾內的doubanTop250.py檔案。

# -*- coding: utf-8 -*-
import scrapy
from douban.items import DoubanItem


class Doubantop250Spider(scrapy.Spider):
    name = 'doubanTop250'
    #allowed_domains = ['movie.douban.com/top250/']    這裡需要修改下,涉及爬取下一頁的關鍵
    allowed_domains = ['movie.douban.com']    
    start_urls = ['http://movie.douban.com/top250/']

    def parse(self, response):
        infos = response.css('.info')
        for info in infos:
            item = DoubanItem()
            item['movie'] = info.css('.title::text').extract_first().strip()
            item['year'] = info.css('div p[class = ""]::text').re_first('\d+') #正則的用法
            item['vote'] = info.css('.rating_num::text').extract_first().strip()
            item['star'] = info.css('.star span::text').extract()[-1].strip() #獲取最後一項
            item['quote'] = info.css('.inq::text').extract_first().strip()
            yield item

        next = response.css('.next a::attr("href")').extract_first()  #查詢下一頁的連結
        url = response.urljoin(next)
        yield scrapy.Request(url = url,callback = self.parse)    #解析下一頁

開啟settings.py檔案,加入如下兩行程式碼:

USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
FEED_EXPORT_ENCODING = 'gbk'

然後在命令提示符輸入:

scrapy crawl doubanTop250 -o douban.csv

爬取結束後會生成一個csv檔案,效果圖如下: