scrapy爬取迅雷電影天堂最新電影ed2k


前言

幾天沒用scrapy爬網站了,正好最近在刷電影,就想着把自己常用的一個電影分享網站給爬取下來保存到本地mongodb中


 

項目開始

第一步仍然是創建scrapy項目與spider文件

切換到工作目錄兩條命令依次輸入

  • scrapy startproject xunleidianying
  • scrapy genspider xunleiBT https://www.xl720.com/thunder/years/2019


內容分析

打開目標網站(分類是2019年上映的電影),分析我們需要的數據

進入頁面是列表的形式就像豆瓣電影一樣,然后我們點進去具體頁面看看

這個頁面就是我們需要拿到的內容頁面,我們來看我們需要哪些數據(某些數據從第一個頁面就可以獲得,但是下載地址必須到第二個頁面)

  • 電影名稱
  • 電影信息
  • 電影內容劇情
  • 電影下載地址

分析完成之后就可以首先編寫 items.py文件

import scrapy


class XunleidianyingItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    name = scrapy.Field()
    information = scrapy.Field()
    content = scrapy.Field()
    downloadurl = scrapy.Field()
    pass

另外別忘了去settings.py中開啟 ITEM_PIPELINES 選項


 

爬蟲文件編寫

老樣子,為了方便測試我們的爬蟲,首先編寫一個main.py的文件方便IDE調用

main.py:

import scrapy.cmdline
scrapy.cmdline.execute('scrapy crawl xunleiBT'.split())

首先我們先測試直接向目標發送請求是否可以得到響應

爬蟲文件 xunleiBT.py編寫如下:

# -*- coding: utf-8 -*-
import scrapy


class XunleibtSpider(scrapy.Spider):
    name = 'xunleiBT'
    allowed_domains = ['https://www.xl720.com/thunder/years/2019']
    start_urls = ['https://www.xl720.com/thunder/years/2019/']

    def parse(self, response):
        print(response.text)
        pass

運行 main.py 看看會出現什么

好的,發現直接返回正常的網頁也就是我們要的網頁,說明該網站沒有反爬機制,這樣我們就更容易爬取了

然后通過xpath定位頁面元素,具體就不再贅述,之前的scarpy教程中都有 繼續編寫爬蟲文件

# -*- coding: utf-8 -*-
import scrapy
#導入編寫的 item
from xunleidianying.items import XunleidianyingItem


class XunleibtSpider(scrapy.Spider):
    name = 'xunleiBT'
    allowed_domains = ['www.xl720.com']
    start_urls = ['https://www.xl720.com/thunder/years/2019/']

    def parse(self, response):
        url_list = response.xpath('//h3//@href').getall()
        for url in url_list:
            yield scrapy.Request(url,callback=self.detail_page)
        nextpage_link = response.xpath('//a[@class="nextpostslink"]/@href').get()
        if nextpage_link:
            yield scrapy.Request(nextpage_link, callback=self.parse)


    def detail_page(self,response):
        # 切記item帶括號
        BT_item = XunleidianyingItem()
        BT_item['name'] = response.xpath('//h1/text()').get()
        BT_item['information'] = ''.join(response.xpath('//div[@id="info"]//text()').getall())
        BT_item['content'] = response.xpath('//div[@id="link-report"]/text()').get()
        BT_item['downloadurl'] = response.xpath('//div[@class="download-link"]/a/text() | //div[@class="download-link"]/a/@href').getall()
        yield BT_item

ITEM爬取完成后該干什么?當然是入庫保存了,編寫pipelines.py文件進行入庫保存

再次提醒別忘了去settings.py中開啟 ITEM_PIPELINES 選項

pipelines.py文件代碼如下:

import pymongo
#連接本地數據庫
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
#數據庫名稱
mydb = myclient["movie_BT"]
#數據表名稱
mysheet = mydb["movie"]


class XunleidianyingPipeline(object):
    def process_item(self, item, spider):
        data = dict(item)
        mysheet.insert(data)
        return item

再次運行main.py 等待運行完成后打開數據庫查詢

數據保存完成,這次我們一共導入了380個數據,可以愉快的查看電影了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM