Python3爬蟲爬取英雄聯(lián)盟高清桌面壁紙功能示例【基于Scrapy框架】
本文實(shí)例講述了Python3爬蟲爬取英雄聯(lián)盟高清桌面壁紙功能。分享給大家供大家參考,具體如下:
使用Scrapy爬蟲抓取英雄聯(lián)盟高清桌面壁紙
源碼地址:https://github.com/snowyme/loldesk
開(kāi)始項(xiàng)目前需要安裝python3和Scrapy,不會(huì)的自行百度,這里就不具體介紹了
首先,創(chuàng)建項(xiàng)目
scrapy startproject loldesk
生成項(xiàng)目的目錄結(jié)構(gòu)

首先需要定義抓取元素,在item.py中,我們這個(gè)項(xiàng)目用到了圖片名和鏈接
import scrapy class LoldeskItem(scrapy.Item): name = scrapy.Field() ImgUrl = scrapy.Field() pass
接下來(lái)在爬蟲目錄創(chuàng)建爬蟲文件,并編寫主要代碼,loldesk.py
import scrapy
from loldesk.items import LoldeskItem
class loldeskpiderSpider(scrapy.Spider):
name = "loldesk"
allowed_domains = ["www.win4000.com"]
# 抓取鏈接
start_urls = [
'http://www.win4000.com/zt/lol.html'
]
def parse(self, response):
list = response.css(".Left_bar ul li")
for img in list:
imgurl = img.css("a::attr(href)").extract_first()
imgurl2 = str(imgurl)
next_url = response.css(".next::attr(href)").extract_first()
if next_url is not None:
# 下一頁(yè)
yield response.follow(next_url, callback=self.parse)
yield scrapy.Request(imgurl2, callback=self.content)
def content(self, response):
item = LoldeskItem()
item['name'] = response.css(".pic-large::attr(title)").extract_first()
item['ImgUrl'] = response.css(".pic-large::attr(src)").extract()
yield item
# 判斷頁(yè)碼
next_url = response.css(".pic-next-img a::attr(href)").extract_first()
allnum = response.css(".ptitle em::text").extract_first()
thisnum = next_url[-6:-5]
if int(allnum) > int(thisnum):
# 下一頁(yè)
yield response.follow(next_url, callback=self.content)
圖片的鏈接和名稱已經(jīng)獲取到了,接下來(lái)需要使用圖片通道下載圖片并保存到本地,pipelines.py:
from scrapy.pipelines.images import ImagesPipeline
from scrapy.exceptions import DropItem
from scrapy.http import Request
import re
class MyImagesPipeline(ImagesPipeline):
def get_media_requests(self, item, info):
for image_url in item['ImgUrl']:
yield Request(image_url,meta={'item':item['name']})
def file_path(self, request, response=None, info=None):
name = request.meta['item']
name = re.sub(r'[?\\*|“<>:/()0123456789]', '', name)
image_guid = request.url.split('/')[-1]
filename = u'full/{0}/{1}'.format(name, image_guid)
return filename
def item_completed(self, results, item, info):
image_path = [x['path'] for ok, x in results if ok]
if not image_path:
raise DropItem('Item contains no images')
item['image_paths'] = image_path
return item
最后在settings.py中設(shè)置存儲(chǔ)目錄并開(kāi)啟通道:
# 設(shè)置圖片存儲(chǔ)路徑
IMAGES_STORE = 'F:/python/loldesk'
#啟動(dòng)pipeline中間件
ITEM_PIPELINES = {
'loldesk.pipelines.MyImagesPipeline': 300,
}
在根目錄下運(yùn)行程序:
scrapy crawl loldesk
大功告成!!!一共抓取到128個(gè)文件夾

更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python正則表達(dá)式用法總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python之print函數(shù)里逗號(hào)和加號(hào)的區(qū)別及說(shuō)明
這篇文章主要介紹了Python之print函數(shù)里逗號(hào)和加號(hào)的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
Python+django實(shí)現(xiàn)文件下載
本文是python+django系列的第二篇文章,主要是講述是先文件下載的方法和代碼,有需要的小伙伴可以參考下。2016-01-01
Python+Appium實(shí)現(xiàn)自動(dòng)化清理微信僵尸好友的方法
這篇文章主要介紹了Python+Appium實(shí)現(xiàn)自動(dòng)化清理微信僵尸好友的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
python實(shí)現(xiàn)指定文件夾下的指定文件移動(dòng)到指定位置
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)指定文件夾下的指定文件移動(dòng)到指定位置,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
Python容錯(cuò)的前綴樹實(shí)現(xiàn)中文糾錯(cuò)
本文使用 Python 實(shí)現(xiàn)了前綴樹,并且支持編輯距離容錯(cuò)的查詢。文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
python 一個(gè)figure上顯示多個(gè)圖像的實(shí)例
今天小編就為大家分享一篇python 一個(gè)figure上顯示多個(gè)圖像的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07
Matplotlib實(shí)戰(zhàn)之折線圖繪制詳解
折線圖是一種用于可視化數(shù)據(jù)變化趨勢(shì)的圖表,它可以用于表示任何數(shù)值隨著時(shí)間或類別的變化,本文主要介紹了如何利用Matplotlib實(shí)現(xiàn)折線圖的繪制,感興趣的可以了解下2023-08-08

