【问题标题】:Scrapy TabError: inconsistent use of tabs and spaces in indentationScrapy TabError:缩进中制表符和空格的不一致使用
【发布时间】:2019-07-24 20:32:36
【问题描述】:

我想用 scrapy 抓取一个网站,只是在网站内部,而不是外部链接。
这是我尝试过的:

import scrapy
import json
import uuid
import os
from scrapy.linkextractors import LinkExtractor

class ItemSpider(scrapy.Spider):
    name = "items"
    allowed_domains = ['https://www.website.com']
    start_urls = ['https://www.website.com/post']
    rules = (Rule(LxmlLinkExtractor(allow=()), callback='parse_obj', follow=True),)
    def parse_obj(self, response):
        for link in LxmlLinkExtractor(allow=self.allowed_domains).extract_links(response):
        response_obj = {}
        counter = 1
        for item in response.css(".category-lcd"):
            title = item.css("div.td-post-header > header > h1::text").extract()
            title_name = title[0]
            response_obj[counter] = {
                'demo': item.css("div.td-post-content > blockquote:nth-child(10) > p::text").extract(),
                'title_name': title_name,
                'download_link': item.css("div.td-post-content > blockquote:nth-child(12) > p::text").extract()
            }
            counter += 1
        filename =  str(uuid.uuid4()) + ".json"
        with open(os.path.join('C:/scrapy/tutorial/results/',filename), 'w') as fp:
            json.dump(response_obj, fp)

但是刮板不起作用,怎么了?! 它说:

Scrapy TabError:缩进中制表符和空格的使用不一致

【问题讨论】:

  • LxmlLinkExtractor(allow=self.allowed_domains).extract_links(response)中的链接后需要缩进:
  • @QHarr 我已经缩进了,但还是没有运气。
  • 没有运气是什么意思?您是否继续检查缩进是否适合您的代码?
  • 你在第 12 行的 for 循环中有 unindent 块,缩进该块,你的代码就可以工作了。

标签: python python-3.x web-scraping scrapy


【解决方案1】:

这部分需要添加缩进:

    for link in LxmlLinkExtractor(allow=self.allowed_domains).extract_links(response):
        response_obj = {}
        counter = 1

【讨论】:

    猜你喜欢
    • 2015-08-28
    • 2019-04-15
    • 2020-01-23
    • 2020-08-10
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 2019-10-05
    • 1970-01-01
    相关资源
    最近更新 更多