我的目标是从这个网站获取数据:https://pokemondb.net/pokedex/all
我正在努力获得能力,它们必须是这样的:
URL名称效果描述
但其中一些信息在另一页。任何提示我如何才能得到它们?
- 我需要访问每个技能的链接并收集信息:*
- 我的代码现在看起来像这样:*
import scrapy
class PokeSpider(scrapy.Spider):
name = 'pokespider'
start_urls = ['https://pokemondb.net/pokedex/all']
def parse(self, response):
linha = response.css('table#pokedex > tbody > tr:first-child')
link = linha.css("td:nth-child(2) > a::attr(href)")
yield response.follow(link.get(), self.parser_pokemon)
def parser_pokemon(self, response):
nome = response.css('h1::text').get()
id = response.css('table.vitals-table > tbody > tr:nth-child(1) > td > strong::text').get()
tamanho = response.css('table.vitals-table > tbody > tr:nth-child(4) > td::text').get()
peso = response.css('table.vitals-table > tbody > tr:nth-child(5) > td::text').get()
url_pokemon = response.url
tipos = response.css('table.vitals-table tbody tr:nth-child(2) td a::text').getall()[:2]
evolucoes = []
evolucoes_possiveis = response.css('#main div.infocard-list-evo div span.infocard-lg-data.text-muted')
for evolucao in evolucoes_possiveis:
nome_evolucao = evolucao.css('a::text').get()
id_evolucao = evolucao.css('small:nth-child(1)::text').get()
url_evolucao = evolucao.css('a::attr(href)').get()
url_evolucao_completinha = f'https://pokemondb.net{url_evolucao}'
evolucoes.append({
"nome_evolucao": nome_evolucao,
"id_evolucao": id_evolucao,
"url_evolucao": url_evolucao_completinha
})
yield {
"nome": nome,
"id": id,
"tamanho": tamanho,
"peso": peso,
"url_pokemon": url_pokemon,
"tipos": tipos,
"evolucoes": evolucoes,
}
1条答案
按热度按时间kmbjn2e31#
我建议您阅读有关https://docs.scrapy.org/en/latest/topics/debug.html?highlight=cb_kwargs中的cb_kwargs和https://docs.scrapy.org/en/latest/topics/items.html中的scrapy项目的文档
您可以发出下一个请求,并通过Meta参数将信息传递给下一个函数,如下所示