首页 > 解决方案 > 请求响应对象:如何检查页面加载完全(动态内容)?

问题描述

我正在做以下事情。创建会话后,我正在对页面进行简单的 GET 操作。问题是,如果这个页面充满了动态部分,所以需要 10-30 秒才能完全生成我感兴趣的 HTML。我用 BeautifulSoup 处理的 HTML。

如果我处理响应对象的速度太快,我就得不到我想要的数据。我已经使用“睡眠”暂停了一段时间,但我认为应该有更好的方法来检查完整的页面加载。我不能依赖状态 200 代码,因为在主页内,动态部分仍在加载。

我的代码:

s = requests.session()
r = s.get('URL')
time.sleep(20)
... code to process response object...

我试图通过 BeautifulSoup 搜索更“优雅”地检查某个标签,但似乎不起作用。

我的代码:

title_found = False
while title_found == False:
        soupje = BeautifulSoup(r.text, 'html.parser')
        title_found_in_html_full = soupje.find(id='titleView!1Title')
        if title_found_in_html_full is not None:
            title_found_in_html = title_found_in_html_full.get('id')
            if title_found_in_html == 'titleView!1Title':
                title_found = True

随着页面加载,响应对象是否会随着时间而变化?

有什么建议么?谢谢

标签: beautifulsouppython-requests

解决方案


推荐阅读