python 属性错误:“QuerySet”对象没有属性“url”

6ojccjat  于 2022-12-17  发布在  Python
关注(0)|答案(2)|浏览(150)

此代码有此错误,我是绝对失去如何修复它.该代码运行罚款,直到它得到最后一个对象保存在数据库中,之后它抛出此错误.该代码是一个工具,检查html的一个网站之间2点在时间检查它是否有一个错误,即使该网站运行良好,并给出一个200响应代码
这是错误:
in check_html print(monitor.url) AttributeError: 'QuerySet' object has no attribute 'url'

def run_monitors():
    delete_from_db()
    monitors = Monitor.objects.filter(is_active=True)
    monitors._fetch_all()
    asyncio.run(_run_monitors(monitors))
    check_html(monitor=monitors)

def check_html(monitor):
    start_time = time.time()
    print(monitor.url)
    # The URLs to compare
    old_html = monitor.html_compare
    new_url = monitor.url

    # Get the HTML of each URL
    try:
        old_html = old_html
        # html1.raise_for_status()
    except Exception as e:
        print(e)

    try:
        html2 = requests.get(new_url)
        html2.raise_for_status()

    except Exception as e:
        print(e)
        return None

    html2 = html2.text[:10000]

    # Create a SequenceMatcher object to compare the HTML of the two URLs
    matcher = difflib.SequenceMatcher(None, old_html, html2)

    similarity_ratio = matcher.ratio() * 100

    response_time = time.time() - start_time

    monitor.html_compare = html2

    html_failure = False
    counter = monitor.fault_counter
    if similarity_ratio <= 90 and counter == 0:
        print(f"The two HTMLs have {similarity_ratio:}% in common.")
        print("change detected")
        html_failure = False
        counter += 1
    elif similarity_ratio > 90 and counter == 0:
        print(f"The two HTMLs have {similarity_ratio:.2f}% in common.")
        print("no change detected")
        html_failure = False
        counter = 0
    elif similarity_ratio > 90 and counter >= 1:
        print(f"The two HTMLs have {similarity_ratio:.2f}% in common.")
        if counter >= 4:
            print(f"HTML fault detected")
            html_failure = True
        else:
            counter += 1
            print(f"checks if fault persists, current fault counter: {counter}")
    elif similarity_ratio < 90 and counter >= 1:
        print("Fault is presumably resolved")
        html_failure = False
        counter = 0
    monitor.fault_counter = counter

    # Print the similarity ratio between the two URLs
    monitor.save(update_fields=['html_compare', 'fault_counter'])
    return html_failure

def send_notifications():
    for monitor in Monitor.objects.all():
            multiple_failures, last_result = has_multiple_failures(monitor)
            result = check_html(monitor)
            no_notification_timeout = (not monitor.last_notified) or \
                                      monitor.last_notified < timezone.now() - timedelta(hours=1)
            if multiple_failures and no_notification_timeout and monitor.is_active:
                _send_notification(monitor, last_result)
            if result:
                _send_notification(monitor, last_result)

我已经尝试过在'check_html'函数周围放置一个for循环,该循环迭代monitor中的每个对象,但它只返回不能迭代monitor的结果。

nbnkbykc

nbnkbykc1#

您已经将queryset传递给了check_html()函数。使用过滤器我们可以得到一个或多个可迭代的项。您可以在check_html()函数中使用for循环,或者只将一个对象密码传递给函数。

nfeuvbwi

nfeuvbwi2#

各位,我发现了这个问题。所以我添加了函数check_html来运行某个命令。在脚本的结尾,它试图将整个查询集交给check_html函数本身。
所以我只需要从run_monitor中删除check_html函数。
谢谢你们的帮助伙计们

相关问题