Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Scrapy';s链接抽取器_Python_Scrapy - Fatal编程技术网

Python 使用Scrapy';s链接抽取器

Python 使用Scrapy';s链接抽取器,python,scrapy,Python,Scrapy,我试图使用Scrapy从页面中提取所有链接,但我很难使用LinkExtractor。我尝试了以下方法: import scrapy from scrapy.spiders import CrawlSpider, Rule from scrapy.linkextractors import LinkExtractor from Funda.items import FundaItem class FundaSpider(scrapy.Spider): name = "Funda"

我试图使用Scrapy从页面中提取所有链接,但我很难使用LinkExtractor。我尝试了以下方法:

import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from Funda.items import FundaItem

class FundaSpider(scrapy.Spider):
    name = "Funda"
    allowed_domains = ["funda.nl"]
    start_urls = [
        "http://www.funda.nl/koop/amsterdam/"
    ]
    rules = (
    Rule(LinkExtractor(), callback='parse_item')
    )

    def parse_item(self, response):
        filename = response.url.split("/")[-2] + '.html'
        with open(filename, 'wb') as f:
            f.write(response.body)
我的理解是,将
LinkExtractor()
作为
规则
应该使
响应只包含链接。但是,如果我查看由此生成的
amsterdam.html
文件,它似乎仍然包含整个网页,而不仅仅是链接


如何让
响应只包含链接?

为什么您认为它只包含链接

我认为您误解了
爬行蜘蛛
规则
参数。在
规则中
实际上指定了爬网逻辑而不是解析逻辑。正在
回调指定的函数中处理解析

因此,如果您想只保存响应中的链接,您必须首先从响应中提取它们。您甚至可以使用相同的
LinkExtractor

class Spider(scrapy.Spider):
    name = 'spider1'
    le1 = LinkExtractor()
    rules = (
        Rule(le1, callback='parse_item')
    )

    def parse_item(self, response):
        # this will give you Link objects
        links = self.le1.extract_links(response)
        # this will give you html nodes of <a> 
        links = response.xpath("//a").extract()
类蜘蛛(scrapy.Spider):
名称='spider1'
le1=LinkExtractor()
规则=(
规则(le1,callback='parse_item')
)
def解析_项(自身、响应):
#这将为您提供链接对象
links=self.le1.提取链接(响应)
#这将为您提供
links=response.xpath(“//a”).extract()

如何获得仅包含链接的响应?我不明白你的问题。一些东西:用于从响应中提取链接
LinkExtractor().extract_links(response)
返回
Link
对象(带有
.url
属性)。
规则
对象中的链接提取器用于
爬行蜘蛛
子类,因此爬行蜘蛛会跟随链接,但你在回调中得到的是
响应
中的页面,而不是URL。感谢Paul,我将尝试解释一下:
开始URL
中的主页包含指向待售房屋页面的链接。最后,我想打开各个房屋的页面,从中提取数据。文档中提到,
extract\u links
返回
scrapy.link.link
对象列表。我从中了解到,
链接
类具有
url
文本
片段
、和
nofollow
属性,但是如何从每个链接获得完整的
响应
?您必须使用url生成
请求
对象。Scrapy只理解
请求
s<代码>链接
对象本身只是URL信息的持有者(没有回调,没有标题等)。因此,在某个时候,您需要
返回scrapy.Request(link.url,callback=…)
。谢谢Granitosaurus。作为后续问题,我还想“检查”
链接中的输出。我尝试了链接中的链接:
item=FundaItem()
item['title']=link.extract()
yield item
,然后尝试运行scrapy,输出为
scrapy crawl Funda-o Funda.csv
。但是,由此生成的.csv文件是空的。如何使用第一种方法生成的
链接?@khpeek您的蜘蛛会返回任何物品吗?爬网结束时,原木停留在什么位置?i、 e.“‘物品刮擦计数’:1”?