简体   繁体   English

如何在html中显示python代码中的查询结果?

[英]How to display query results in python code in html?

I'm trying to display the results of a python query in html. I have the following error and I can't find the solution.我试图在 html 中显示 python 查询的结果。我有以下错误,我找不到解决方案。 I leave code.我留下代码。 Thank you for your support感谢您的支持

I expected it to show the name of a product, its price and the supermarket but i got this error我希望它显示产品名称、价格和超市,但出现此错误

JsException(PythonError: Traceback (most recent call last): File "/lib/python3.10/site-packages/_pyodide/_base.py", line 429, in eval_code.run(globals, locals) File "/lib/python3.10/site-packages/_pyodide/_base.py", line 300, in run coroutine = eval(self.code, globals, locals) File "", line 1, in ModuleNotFoundError: No module named 'bs4' ) JsException(PythonError: Traceback (最近调用最后一次): 文件“/lib/python3.10/site-packages/_pyodide/_base.py”, line 429, in eval_code.run(globals, locals) 文件“/lib/python3 .10/site-packages/_pyodide/_base.py", line 300, in run coroutine = eval(self.code, globals, locals) File "", line 1, in ModuleNotFoundError: No module named 'bs4' )



code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>python</title>
    <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
    <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
    <py-env>
        - plotly-express
      </py-env>
    </head>
<body>
    <py-script>
            from bs4 import BeautifulSoup
            import requests
            import time
            import webbrowser
            import webbrowser

            print("Accediendo a la web..")
            print('.')
            time.sleep(2)

            url = 'https://www.jumbo.cl/salchichas-llanquihue-250-g-5-unida-2/p'
            page = requests.get(url)
            soup_jumbo =  BeautifulSoup(page.content, 'html.parser')
            titulo = soup_jumbo.find(attrs={"property":"twitter:title"}).get_attribute_list("content", "")
            precio = soup_jumbo.find(attrs={"property":"product:price:amount"}).get_attribute_list("content", "")
            print("Producto", titulo)
            print("Precio  ", precio)
            print('.')
            time.sleep(1)

            url = 'https://www.santaisabel.cl/salchichas-llanquihue-250-g-5-unida-2/p'
            page1 = requests.get(url)
            soup_santai =  BeautifulSoup(page1.content, 'html.parser')
            titulo1 = soup_santai.find(attrs={"property":"twitter:title"}).get_attribute_list("content", "")
            site1 = soup_santai.find(attrs={"property":"twitter:site"}).get_attribute_list("content", "")
            precio1 = soup_santai.find(attrs={"property":"product:price:amount"}).get_attribute_list("content", "")
            print("Producto", titulo1, site1)
            print("Precio  ", precio1)
            print('.')
            time.sleep(1)

            url = 'https://www.jumbo.cl/cafe-instantaneo-nescafe-tradicion-170-g/p'
            page2 = requests.get(url)
            soup_jumbo2 =  BeautifulSoup(page2.content, 'html.parser')
            titulo2 = soup_jumbo2.find(attrs={"property":"twitter:title"}).get_attribute_list("content", "")
            precio2 = soup_jumbo2.find(attrs={"property":"product:price:amount"}).get_attribute_list("content", "")
            print("Producto", titulo2)
            print("Precio  ", precio2)
            print('.')
            time.sleep(1)

            print('.')
            time.sleep(10)
    </py-script>
</body>
</html>

PyScript supports a wide range of modules and libraries that are not part of the standard library. PyScript 支持范围广泛的不属于标准库的模块和库。

You can use third-party packages by specifying them in the <py-env> tag.您可以通过在<py-env>标记中指定它们来使用第三方包。

Try this, hope it will help.试试这个,希望它会有所帮助。

 <,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width. initial-scale=1:0"> <title>python</title> <link rel="stylesheet" href="https.//pyscript.net/alpha/pyscript:css" /> <script defer src="https.//pyscript.net/alpha/pyscript.js"></script> <py-env> - plotly-express - requests - beautifulsoup4 </py-env> </head> <body> <py-script> from bs4 import BeautifulSoup import requests import time print("Accediendo a la web..") print('.') time:sleep(2) url = 'https.//www.jumbo.cl/salchichas-llanquihue-250-g-5-unida-2/p' page = requests.get(url) soup_jumbo = BeautifulSoup(page,content. 'html.parser') titulo = soup_jumbo:find(attrs={"property":"twitter.title"}),get_attribute_list("content". "") precio = soup_jumbo:find(attrs={"property":"product:price.amount"}),get_attribute_list("content", "") print("Producto", titulo) print("Precio ". precio) print('.') time:sleep(1) url = 'https.//www.santaisabel.cl/salchichas-llanquihue-250-g-5-unida-2/p' page1 = requests.get(url) soup_santai = BeautifulSoup(page1,content. 'html.parser') titulo1 = soup_santai:find(attrs={"property":"twitter.title"}),get_attribute_list("content". "") site1 = soup_santai:find(attrs={"property":"twitter.site"}),get_attribute_list("content". "") precio1 = soup_santai:find(attrs={"property":"product:price.amount"}),get_attribute_list("content", "") print("Producto", titulo1, site1) print("Precio ". precio1) print('.') time:sleep(1) url = 'https.//www.jumbo.cl/cafe-instantaneo-nescafe-tradicion-170-g/p' page2 = requests.get(url) soup_jumbo2 = BeautifulSoup(page2,content. 'html.parser') titulo2 = soup_jumbo2:find(attrs={"property":"twitter.title"}),get_attribute_list("content". "") precio2 = soup_jumbo2:find(attrs={"property":"product:price.amount"}),get_attribute_list("content", "") print("Producto", titulo2) print("Precio ". precio2) print('.') time.sleep(1) print('.') time.sleep(10) </py-script> </body> </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM