1

嗨,我正在基于 python 语言在 scrapy 框架中编写蜘蛛。我正在尝试以下代码,但它给了我一个意想不到的缩进,我没有找到类似的东西。我全新这是我在 python 中的第一个蜘蛛。请帮我

from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector

from first_sample.items import FirstSampleItem

class FirstSpider(BaseSpider):
    name = "first"
    allowed_domains = ["reviews42.com"]
    start_urls = ["http://www.reviews42.com/mobiles/category/"]


    def parse(self, response):
        hxs = HtmlXPathSelector(response)
        apps = hxs.select("//*[@id=\"col1\"]/div[@class=\"clear left summary-block relative\"]")
        count = 0
        items = []

    for app in apps:
        item = FirstSampleItem()
        item['title'] = appa.select("//div[@class=\"padding10 relative\"]/div[@class=\"thin-heading font700 black marginTop5\"]/a/text()")[count].extract()
        items.append(item)
        count += 1
    return items

IndentationError: unindent 不匹配任何外部缩进级别

4

1 回答 1

4

函数中的循环和return语句parse没有正确缩进。


def parse(self, response):
    hxs = HtmlXPathSelector(response)
    apps = hxs.select("//*[@id=\"col1\"]/div[@class=\"clear left summary-block relative\"]")
    count = 0
    items = []

    for app in apps:

        item = FirstSampleItem()
        item['title'] = appa.select("//div[@class=\"padding10 relative\"]/div[@class=\"thin-heading font700 black marginTop5\"]/a/text()")[count].extract()

        items.append(item)
        count += 1

    return items

编程 Python 时注意不要混合空格和制表符。Tab 是单个字符,与单个空格不同。

于 2013-07-09T07:44:36.393 回答