表格元素未在BeautifulSoup中显示 [英] Table element not showing in BeautifulSoup

查看:65
本文介绍了表格元素未在BeautifulSoup中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从此网站

以下是代码-

import requests
from bs4 import BeautifulSoup as bs

page = requests.get('https://www.vitalityservicing.com/serviceapi/Monitoring/QueueDepth?tenantId=1')

soup = bs(page.text, "html.parser")

#None of the following method works
tb = soup.table 
#tb = soup.body.table
#tb = soup.find_all('table')

当我尝试打印tbNone

因此,我尝试使用

print(soup.body.prettify())

我没有看到table元素或其子元素.仅存在<body><script>元素:

I dont see the table elements or it's child elements . Only <body> and <script> elements are present:

但是当我检查Chrome中的页面时,我看到了所有元素:

But when I inspect the page in chrome I see all the elements:

当我将页面加载到chrome上时,我不明白为什么table元素没有在requests.get中下载,而

I don't understand why the table element is not being downloaded with requests.get while its there when I load the page on chrome

推荐答案

您没有得到该内容,因为在执行请求时,该请求不在页面中. .

You are not getting that content because, when you execute the request, it is not present in the page. Yet.

如果检查脚本标记之间的javascript代码,则可以看到它正在动态生成表.因此,在requests不是浏览器并且不会执行js的情况下,您会在发生这种情况之前收到html代码.

If you check the javascript code between the script tags, you can see it is generating the table dynamically. So, you receive the html code before that has happened, since requests is not a browser and won't execute the js, and you don't get to see the table.

现在您知道了为什么看不到表格,您的下一个问题是如何在JavaScript执行后获取生成的HTML.不要晕倒,这是可行的.您可能会在此问题中找到解决方案有趣.

Now that you know why you can't see the table, your next problem is how to get the HTML produced after the javascript execution. Don't faint, it is feasible. You might find the solutions in this question interesting.

祝你好运

这篇关于表格元素未在BeautifulSoup中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆