使抓痒的蜘蛛遵循给定起始网址中的链接

时间:2018-08-30 07:49:31

标签: scrapy web-crawler html-parsing scrapy-spider

我正在尝试构建一个具有抓取功能的简单蜘蛛,以导航从给定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多个行。

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

在此先感谢您的回复

2 个答案:

答案 0 :(得分:1)

请勿使用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

var groups = [
  [
    {

      name: 'John'

    }, {

      name: 'Sally'

    }
  ],
  [
    {

      name: 'paul'

    }, {

      name: 'Joseph'

    }
  ]
]

答案 1 :(得分: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函数中获得响应。

  

2。您甚至可以使用python请求模块发送请求,

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

print(resp.text)

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

相关问题