UnboundLocalError:赋值前引用的局部变量“author”

时间:2017-05-07 05:42:43

标签: python python-2.7 scrapy

我正在从列表页面抓取网站的详细信息页面,每个详细信息页面都有一些差异。

第1个详细信息页面:

<div class="td-post-content">
    <p style="text-align: justify;">
        <strong>[ Karda Natam ]</strong>
        <br>
        <strong>ITANAGAR, May 6:</strong> Nacho, Taksing, Siyum and ...
        <br> “Offices are without ...
    </p>
</div>

第二个详细信息页面:

<div class="td-post-content">
    <p style="text-align: justify;">
        <strong>Guwahati, May 6 (PTI)</strong> Sarbananda Sonowal today ...
        <br> “Books are a potent tool to create ...
    </p>
</div>

我正在尝试解析作者并从详细信息页面发布日期:

class ArunachaltimesSpider(scrapy.Spider):
    ...
    ...

    def parse(self, response):
        urls = response.css("...").extract()
        for url in urls:
            yield scrapy.Request(url=url, callback=self.parse_detail)

    def parse_detail(self, response):
        strong_elements = response.css("div.td-ss-main-content").css("div.td-post-content").css("p > strong::text").extract()
        for strong in strong_elements:
            if ', ' in strong:
                news_date = strong.split(', ')[1].replace(":", "")
            elif '[ ' and ' ]' in strong:
                author = strong
            else:
                news_date = None
                author = None
        yield {
            'author': author,
            'news_date': news_date
        }

但是我收到了这个错误:

  

UnboundLocalError:在赋值之前引用的局部变量'author'

我在这里做错了什么?请问您如何分别从每个页面获取作者和新闻日期。谢谢。

0 个答案:

没有答案