【问题标题】:Making scrapy spider follow links in given starting url使scrapy蜘蛛跟随给定起始网址中的链接
【发布时间】:2018-08-30 07:49:30
【问题描述】:

我正在尝试使用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


    【解决方案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
    

    【讨论】:

    • 解决了大部分问题,非常感谢。我会从那里继续工作,祝你有美好的一天!
    【解决方案2】:

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

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

    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 requests 模块发送请求,

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

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

    【讨论】:

    • 您好 Agus,感谢您的回复,您介意澄清一下吗?我想我错过了“发送请求”部分,如果您不想费心编写代码,您介意将我指向相关的特定文档吗?提前感谢您选择的任何选项!
    • @Peeveswozere 欢迎,如果您对我的回答满意,请标记为回答
    • 答案很明确,但不能解决问题,此外,您不应将自己的答案复制粘贴到不同的问题上,因为这可能会被标记为垃圾邮件或试图赚取积分。跨度>
    猜你喜欢
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多