scrapy框架爬取王者榮耀面板

2021-10-14 02:12:50 字數 4076 閱讀 1497

建立專案命令:scrapy startproject wangzhephotomax

建立爬蟲:scrapy genspider wangzhecrawl

更改settings.py中的設定:

user_agent =

# obey robots.txt rules

robotstxt_obey =

false

設定的儲存路徑

# 設定儲存路徑

images_store=

'./imgs'

更改儲存管道,更換為儲存的管道

# 更改儲存管道,更換為儲存的管道

item_pipelines =

download_delay =

2

wangzhecrawl.py檔案

import bs4

import time

import scrapy

from wangzhephotomax.items import wangzhephotomaxitem

class

wangzhecrawlspider

(scrapy.spider)

: name =

'wangzhecrawl'

allowed_domains =

['pvp.qq.com'

,'game.gtimg.cn'

] start_urls =

"/web201605/herolist.shtml"

)def

parse

(self, response)

: bs = bs4.beautifulsoup(response.text,

'html.parser'

) ul_bs = bs.find(

'ul'

,class_=

"herolist clearfix"

) hero_li = ul_bs.find_all(

'li'

)for hero in hero_li:

a_link = hero.find(

'a')

next_url =

'/web201605/'

+ a_link[

'href'

]# 英雄的名字

name = a_link.text

# print(next_url)

yield scrapy.request(next_url,callback=self.next_page_parse,meta=

) time.sleep(1)

defnext_page_parse

(self,response)

:# print('****************' * 20)

bs = bs4.beautifulsoup(response.text,

'html.parser'

) style_bs = bs.find(

'div'

,class_=

"zk-con1 zk-con")[

'style'

] part_url = style_bs.replace(

"background:url('//",''

).replace(

"') center 0",''

)# print('part_url:',part_url)

# 獲取某個英雄的**個數

]# 將資料返回

item = wangzhephotomaxitem(

) item[

'hero_name'

]= response.meta[

'name'

] item[

'skin_url'

]= skin_url

item[

'skin_name'

]= name

yield item

items.py :它定義 item 資料結構,所有的 item 的定義都可以放這裡,在這裡定義的item可以供上面wangzhecrawl.py呼叫。
items.py

import scrapy

class

wangzhephotomaxitem

(scrapy.item)

:# define the fields for your item here like:

# name = scrapy.field()

hero_name = scrapy.field(

) skin_url = scrapy.field(

) skin_name = scrapy.field(

)

設定的儲存

import scrapy

from scrapy.pipelines.images import imagespipeline

class

imgpileline

(imagespipeline)

:# 接收item且將item中儲存的img_src進行請求傳送

defget_media_requests

(self, item, info)

:print

('&'*50

)print

(item[

'skin_url'])

yield scrapy.request(url=item[

'skin_url'

],meta=

)# 指定資料儲存的路徑(資料夾【在配置檔案中指定】+名稱【該方法中返回】)

進入到專案根目錄(包含settings.py檔案的目錄)下輸入啟動爬蟲命令:scrapy crawl wangzhecrawl

python 爬取王者榮耀高畫質桌布

位址如下 打過王者的童鞋一般都會喜歡裡邊設計出來的英雄吧,特別想把王者榮耀的英雄的高畫質當成電腦桌面 預覽一下桌面吧 問題簡單了就 api 返回情況,大致如下 是乙個jsonp callback的返回 這種情況一般是callback後邊對應了乙個json物件,我們可以用python的 json 類庫...

利用threading多執行緒爬取王者榮耀的高畫質桌布

首先我們需要匯入以下模組 import requests from urllib import request from urllib import parse import queue import threading import os import time為了防止王者榮耀的網頁後台識別出爬蟲...

python爬蟲 爬取王者榮耀全英雄面板

爬取王者榮耀全英雄 import requests import re 1 分析目標網頁,確定爬取的url路徑,headers引數 base url headers 2 傳送請求 response requests.get base url,headers headers base data res...