Django - UnboundLocalError: local variable 'image_link' referenced before assignment

Django - UnboundLocalError: local variable 'image_link' referenced before assignment

我写了一个解析新闻文章提要的函数。

def save_new_articles(feed, source_id, category_id):
    channel_feed_title = feed.channel.title.title()
    channel_feed_link = feed.channel.link
    channel_feed_desc = feed.channel.description
    official_source_id = source_id
    post_category_id = category_id

    for item in feed.entries:
        parsed_summary = item.summary
        soup = BeautifulSoup(parsed_summary, 'lxml')
        images = soup.findAll('img')
        for image in images:
            image_url_link = (image['src'])
            if image_url_link is not None:
                image_link = image_url_link
            else:
                image_link = "https://www.publicdomainpictures.net/pictures/280000/velka/not-found-image-15383864787lu.jpg"

        parsed_title = item.title
        formatted = re.sub("<.*?>", "", parsed_title)
        post_title = formatted

        post_link = item.link

        description = item.description
        output_summary = re.sub("<.*?>", "", description)

        title = item.title
        capital = title.title()
        tags = capital.split()

        date_published = parser.parse(item.published)

        if not Posts.objects.filter(guid=item.guid).exists():
            post = Posts(
                title = post_title,
                link = post_link,
                summary = output_summary,
                image_url = image_link,
                tags = tags,
                pub_date = date_published,
                guid = item.guid,
                feed_title = channel_feed_title,
                feed_link = channel_feed_link,
                feed_description = channel_feed_desc,
                source_id = official_source_id,
                category_id = post_category_id
            )
            post.save()
            
        else:
            logger.info("Duplicate Post Detected! Skipping...")

但是在 运行 我得到的代码:

image_url = image_link,
UnboundLocalError: local variable 'image_link' referenced before assignment

我不明白我在上面 image for loop statement 中定义 image_link 时看到的错误是从哪里来的。我已经在 SO 上检查过类似的答案,但我似乎没有找到合适的答案。请帮我调试一下。

images(用 soup.findAll('img') 初始化)为空时会发生此错误。

可能您不仅 item.summary 没有 img 标签,而且由于之前的一些错误,它也可能只是空的。

因此要修复您的代码,您应该在此之前初始化 image_link

if not Posts.objects.filter(guid=item.guid).exists():

你的默认值:

image_link = "https://www.publicdomainpictures.net/pictures/280000/velka/not-found-image-15383864787lu.jpg"

例如,像这样,而不是:

        for image in images:
            image_url_link = (image['src'])
            if image_url_link is not None:
                image_link = image_url_link
            else:
                image_link = "https://www.publicdomainpictures.net/pictures/280000/velka/not-found-image-15383864787lu.jpg"

你可以这样走:

        image_link = "https://www.publicdomainpictures.net/pictures/280000/velka/not-found-image-15383864787lu.jpg"
        for image in images:
            image_url_link = (image['src'])
            if image_url_link is not None:
                image_link = image_url_link