网页抓取获取数据后如何查看输出?

时间:2021-04-13 00:51:15

标签: python html data-visualization

我输入了一些代码来从站点获取一些数据(抓取),我得到了我想要的结果(结果只是数字)。 问题是,如何在我的网站上将结果显示为输出?我在 Vs 代码中使用 python 和 HTML。这是抢购代码:

   import requests
from bs4 import BeautifulSoup

getpage= requests.get('https://www.worldometers.info/coronavirus/country/austria/')
getpage_soup= BeautifulSoup(getpage.text, 'html.parser')
Get_Total_Deaths_Recoverd_Cases= getpage_soup.findAll('div', {'class':'maincounter-number'})

for para in Get_Total_Deaths_Recoverd_Cases:
    print (para.text)




Get_Active_And_Closed= BeautifulSoup(getpage.text, 'html.parser')
All_Numbers2= Get_Active_And_Closed.findAll('div', {'class':'number-table-main'})

for para2 in All_Numbers2:
    print (para2.text)

这些是我想在网站上显示的结果:

<块引用>

577,007

9,687

535,798

31,522 545,485

1 个答案:

答案 0 :(得分:0)

我不知道如何描述这个解决方案,但它仍然是一个解决方案:

import os

lst = [
    577.007, 9.687, 535.798, 31.522, 545.485
]

p = ''
for num in lst:
    p += f'<p>{num}</p>\n'

html = f"""

<!doctype>
<html>
<head>
<title>Test</title>
</head>
<body>
{p}
</body>
</html>

"""

with open('web.html', 'w') as file:
    file.write(html)

os.startfile('web.html')

您不一定非要使用那个 os.startfile,但您明白我希望的想法,您现在拥有 web.html 文件,您可以将其显示在您的网站或其他任何地方,理论上您也可以获得一个普通的 html 文档并将其复制到那里(设置为 html 变量并将 {p} 放在您需要/想要的任何地方),或者甚至转到您的 html 并将其放在某个地方,例如 {%%} (这是 django 使用的但是无论如何),然后读取整个文件并将那些 {%%} 替换为您的值并将其写回。

最后这是一个解决方案,但取决于您使用的内容,例如各种框架,例如 django 或flask,有更简单的方法可以做到这一点

相关问题