万象直播app破解版_欧美国产日韩无遮挡在线一区二区,亚洲国产综合精品中久,强奷白丝女仆在线观看,超碰三级大陆在线

您的位置:首頁 > 軟件問答

電影天堂迅雷免費(Python爬蟲框架:scrapy爬取迅雷電影天堂最新電影ed2k)

導讀項目開始第一步仍然是創建scrapy項目與Spider文件切換到工作目錄兩條命令依次輸入scrapy startproject xunleidianyingscrapy genspider xunleiBT https://www.xl720.com/th

項目開始

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

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

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

內容分析

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

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

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

電影名稱

電影信息

電影內容劇情

電影下載地址

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

import scrapy'''更多Python學習資料以及源碼教程資料,可以在群821460695 免費獲取'''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.cmdlinescrapy.cmdline.execute('scrapy crawl xunleiBT'.split())

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

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

# -*- coding: utf-8 -*-import scrapyclass 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教程中都有 繼續編寫爬蟲文件

import scrapy#導入編寫的 itemfrom xunleidianying.items import XunleidianyingItem'''更多Python學習資料以及源碼教程資料,可以在群821460695 免費獲取'''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"]'''更多Python學習資料以及源碼教程資料,可以在群821460695 免費獲取'''class XunleidianyingPipeline(object): def process_item(self, item, spider): data = dict(item) mysheet.insert(data) return item

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

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

免責聲明:本文由用戶上傳,如有侵權請聯系刪除!