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

首先需要定義抓取元素,在item.py中,我們這個項目用到了圖片名和鏈接
import scrapy class LoldeskItem(scrapy.Item): name = scrapy.Field() ImgUrl = scrapy.Field() pass
接下來在爬蟲目錄創(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:
# 下一頁
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
# 判斷頁碼
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):
# 下一頁
yield response.follow(next_url, callback=self.content)
圖片的鏈接和名稱已經獲取到了,接下來需要使用圖片通道下載圖片并保存到本地,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中設置存儲目錄并開啟通道:
# 設置圖片存儲路徑
IMAGES_STORE = 'F:/python/loldesk'
#啟動pipeline中間件
ITEM_PIPELINES = {
'loldesk.pipelines.MyImagesPipeline': 300,
}
在根目錄下運行程序:
scrapy crawl loldesk
大功告成?。。∫还沧ト〉?28個文件夾

更多關于Python相關內容可查看本站專題:《Python Socket編程技巧總結》、《Python正則表達式用法總結》、《Python數(shù)據(jù)結構與算法教程》、《Python函數(shù)使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
相關文章
Python之print函數(shù)里逗號和加號的區(qū)別及說明
這篇文章主要介紹了Python之print函數(shù)里逗號和加號的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02
Python+Appium實現(xiàn)自動化清理微信僵尸好友的方法
這篇文章主要介紹了Python+Appium實現(xiàn)自動化清理微信僵尸好友的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-02-02
python實現(xiàn)指定文件夾下的指定文件移動到指定位置
這篇文章主要為大家詳細介紹了python實現(xiàn)指定文件夾下的指定文件移動到指定位置,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-09-09

