表格元素未在BeautifulSoup中显示 [英] Table element not showing in 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')
当我尝试打印tb
其None
因此,我尝试使用
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屋!