indentation error when commenting out with triple quotes in scrapy
我将我的文本编辑器设置为使用缩进的所有空格,并且我已删除并重新缩进以获得良好的度量。我正在尝试使用scrapy来查看不同语法的执行情况,并尝试在两个代码块之间切换,一个代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class BasicSpider(scrapy.Spider): name = 'basic' allowed_domains = ['web'] start_urls = ['http://foobar.com'] def parse(self, response): l = ItemLoader(item = TestItem(), response=response) item = TestItem() l.add_xpath('title', '/html/body/div[1]/article/header/div[3]/h1/text()') l.add_xpath('author', '/html/body/div[1]/article/div/div[2]/div[1]/span/span[1]/a/text()') l.add_xpath('published', '/html/body/div[1]/article/div/div[2]/div[2]/time/text()') l.add_xpath('year', '/html/body/div[1]/article/div/div[2]/div[2]/time/text()') l.add_xpath('month', '/html/body/div[1]/article/div/div[2]/div[2]/time/text()') item['publication'] = 'Foobar' l.add_xpath('content', '/html/body/div[1]/article/div/div[3]/div[1]//p/text()') return l.load_item(), item |
但是,如果我想注释掉上面的代码并仅使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | ''' l.add_xpath('title', '/html/body/div[1]/article/header/div[3]/h1/text()') l.add_xpath('author', '/html/body/div[1]/article/div/div[2]/div[1]/span/span[1]/a/text()') l.add_xpath('published', '/html/body/div[1]/article/div/div[2]/div[2]/time/text()') l.add_xpath('year', '/html/body/div[1]/article/div/div[2]/div[2]/time/text()') l.add_xpath('month', '/html/body/div[1]/article/div/div[2]/div[2]/time/text()') item['publication'] = 'Foobar' l.add_xpath('content', '/html/body/div[1]/article/div/div[3]/div[1]//p/text()') return l.load_item(), item ''' item['title'] = response.xpath('/html/body/div[1]/article/header/div[3]/h1/text()').extract() item['author'] = response.xpath('/html/body/div[1]/article/div/div[2]/div[1]/span/span[1]/a/text()').extract() item['published'] = response.xpath('/html/body/div[1]/article/div/div[2]/div[2]/time/text()').extract() item['year'] = response.xpath('/html/body/div[1]/article/div/div[2]/div[2]/time/text()').extract() item['month'] = response.xpath('/html/body/div[1]/article/div/div[2]/div[2]/time/text()').extract() item['publication'] = 'Foobar' item['content'] = response.xpath('/html/body/div[1]/article/div/div[3]/div[1]//p/text()').extract() return item |
它告诉我在
1 2 3 4 5 | def parse(self, response): #l = ItemLoader(item = TestItem(), response=response) item = TestItem() item['title'] = response.xpath('/html/body/div[1]/article/header/div[3]/h1/text()').extract() item['author'] = response.xpath('/html/body/div[1]/article/div/div[2]/div[1]/span/span[1]/a/text()').extract() |
如果我通过完全unindenting return语句尝试完全做同样的事情,它告诉我返回是在函数之外,当我将它插回到函数内部时,我得到一个意外的缩进错误。如果我删除所有注释和整个add_xpath块,代码工作正常。
我希望能够轻松地在add_xpath和项目之间来回切换,而且我也不确定我是否误解了关于三重引号的规则。使用多个文本编辑器编辑时会发生这种情况。
三重引号需要缩进以匹配您评论的内容。 这是因为您将该代码转换为字符串,python不会忽略它,该字符串对您的输出没有影响。