制作scrapy蜘蛛跟随给定起始URL的链接

问题描述 投票:1回答:2

我正在尝试使用scrapy构建一个简单的蜘蛛来导航链接从给定的start_urls开始并在页面内部,刮掉两个项目。

目标:这是我的starting page。在这里你看到一个护身符列表,我想进入每个护身符页面和这些页面内部,刮去风味文本和项目名称。

我首先构建了一个工作原型,给出了一个单独的护身符,它刮掉了他的数据,现在我想扩展它,所以它会立刻为所有这些做到这一点,但我在寻找如何这样做时苦苦挣扎。

这是迄今为止的代码:

import scrapy
from PoExtractor.items import PoextractorItem
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor


class ArakaaliSpider(scrapy.Spider):
    name = "arakaali"
    allowed_domains = ['pathofexile.gamepedia.com']
    start_urls = ['https://pathofexile.gamepedia.com/List_of_unique_accessories']

    rules = (Rule(LinkExtractor(restrict_xpaths=(unique=True), callback='parse', follow=True))


    def parse(self, response):
        for link in LinkExtractor(allow=(), deny=()).extract_links(response):
          item = PoextractorItem()
          item["item_name"] = response.xpath("//*[@id='mw-content-text']/span/span[1]/span[1]/text()[1]").extract()
          item["flavor_text"] = response.xpath("//*[@id='mw-content-text']/span/span[1]/span[2]/span[3]/text()").extract()
          yield item

item_nameflavor_text xpath运行良好,它是使用Chrome“检查元素”功能提取的,但是规则或parse的循环中有些东西不起作用,因为这是首次输出:

2018-08-30 09:23:13 [scrapy.core.scraper] DEBUG: Scraped from <200 https://pathofexile.gamepedia.com/List_of_unique_accessories>
{'flavor_text': [], 'item_name': []}
2018-08-30 09:23:13 [scrapy.core.scraper] DEBUG: Scraped from <200 https://pathofexile.gamepedia.com/List_of_unique_accessories>
{'flavor_text': [], 'item_name': []}
2018-08-30 09:23:13 [scrapy.core.scraper] DEBUG: Scraped from <200 https://pathofexile.gamepedia.com/List_of_unique_accessories>
{'flavor_text': [], 'item_name': []}
2018-08-30 09:23:13 [scrapy.core.scraper] DEBUG: Scraped from <200 https://pathofexile.gamepedia.com/List_of_unique_accessories>
{'flavor_text': [], 'item_name': []}
2018-08-30 09:23:13 [scrapy.core.scraper] DEBUG: Scraped from <200 https://pathofexile.gamepedia.com/List_of_unique_accessories>
{'flavor_text': [], 'item_name': []}
2018-08-30 09:23:13 [scrapy.core.scraper] DEBUG: Scraped from <200 https://pathofexile.gamepedia.com/List_of_unique_accessories>
{'flavor_text': [], 'item_name': []}
2018-08-30 09:23:13 [scrapy.core.scraper] DEBUG: Scraped from <200 https://pathofexile.gamepedia.com/List_of_unique_accessories>
{'flavor_text': [], 'item_name': []}
2018-08-30 09:23:13 [scrapy.core.scraper] DEBUG: Scraped from <200 https://pathofexile.gamepedia.com/List_of_unique_accessories>
{'flavor_text': [], 'item_name': []}

这会持续一段时间,然后包含名称和风味的文件显示:

flavor_text,item_name

,

,

,

,

,

,

它持续超过300行。

其他有用的信息:并非页面中的所有链接都指向另一个页面,其中存在项目名称和风格,因此可以找到空白点,我的问题是,为什么它们都是白色的?它不遵循游戏项目页面的链接吗?

每个回复都要提前感谢

scrapy web-crawler html-parsing scrapy-spider
2个回答
1
投票

不要使用parse作为LinkExtractor回调的名称!我修复了你的语法错误,并在你的代码中添加了一些restrict_xpaths

class ArakaaliSpider(CrawlSpider):
    name = "arakaali"
    allowed_domains = ['pathofexile.gamepedia.com']
    start_urls = ['https://pathofexile.gamepedia.com/List_of_unique_accessories']

    rules = (
        Rule(
            LinkExtractor(
                restrict_xpaths='//table[contains(@class, "wikitable")]//tr/td[1]//span[@class="c-item-hoverbox__activator"]//a[1]'
            ),
            callback='parse_details',
            follow=True
        ),
    )


    def parse_details(self, response):
        item = PoextractorItem()
        item["item_name"] = response.xpath("//*[@id='mw-content-text']/span/span[1]/span[1]/text()[1]").extract()
        item["flavor_text"] = response.xpath("//*[@id='mw-content-text']/span/span[1]/span[2]/span[3]/text()").extract()
        yield item

0
投票

您必须首先编写一个函数来向游戏项页面发送请求(解析函数本身),然后在第二个函数中添加函数解析中的当前代码。

您可以通过多种方式发送请求。

1.由于您正在使用scrapy,因此可以使用以下代码

def parse_page1(self, response):
    return scrapy.Request("http://www.example.com/some_page.html",
                          callback=self.parse_page2)

def parse_page2(self, response):
    # this would log http://www.example.com/some_page.html
    self.logger.info("Visited %s", response.url)

parse_page1将向url发送请求,您将在parse_page2函数中获得响应。

你甚至可以使用python请求模块发送请求,

import requests
resp = req.get("http://www.something.com")

print(resp.text)

如果您对此有任何疑问,请发表评论,谢谢

© www.soinside.com 2019 - 2024. All rights reserved.