【问题标题】:Scrapy crawl extracted linksScrapy抓取提取的链接
【发布时间】:2016-01-23 22:14:03
【问题描述】:

我需要抓取一个网站,并在特定 xpath 上抓取该网站的每个 url 例如。: 我需要抓取在容器中有 10 个链接的“http://someurl.com/world/”(xpath("//div[@class='pane-content']")),我需要抓取所有这 10 个链接并从中提取图像,但“http://someurl.com/world/”中的链接看起来像 "http://someurl.com/node/xxxx"

我现在拥有的:

import scrapy
from scrapy.contrib.spiders import Rule, CrawlSpider
from scrapy.contrib.linkextractors import LinkExtractor
from imgur.items import ImgurItem

class ImgurSpider(CrawlSpider):
    name = 'imgur'
    allowed_domains = ['someurl.com/']
    start_urls = ['http://someurl.com/news']
    rules = [Rule(LinkExtractor(allow=('/node/.*')), callback='parse_imgur', follow=True)]

    def parse_imgur(self, response):
        image = ImgurItem()
        image['title'] = response.xpath(\
            "//h1[@class='pane-content']/a/text()").extract()
        rel = response.xpath("//img/@src").extract()
        image['image_urls'] = response.xpath("//img/@src").extract()
        return image

【问题讨论】:

    标签: python hyperlink scrapy extractor


    【解决方案1】:

    您可以重写您的“规则”以适应您的所有要求:

    rules = [Rule(LinkExtractor(allow=('/node/.*',), restrict_xpaths=('//div[@class="pane-content"]',)), callback='parse_imgur', follow=True)]
    

    要从提取的图像链接中下载图像,您可以使用 Scrapy 捆绑的 ImagePipeline

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-25
      相关资源
      最近更新 更多