You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用PyScript在HTML中展示Python查询结果及解决模块报错

解决PyScript中bs4模块找不到的问题

错误原因

你代码里用到了BeautifulSoup(来自bs4库)和requests库,但在<py-env>配置块里只声明了plotly-express,PyScript运行时不会自动安装未明确声明的第三方库,因此触发ModuleNotFoundError。

解决方案

  1. 更新<py-env>配置:添加beautifulsoup4(bs4是该库的常用别名)和requests,确保PyScript加载时安装这两个依赖。
  2. 优化代码冗余:删除重复导入的webbrowser语句(不影响功能,但让代码更整洁)。

修改后的完整代码

<!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
        - beautifulsoup4
        - requests
    </py-env>
</head>
<body>
    <py-script>
            from bs4 import BeautifulSoup
            import requests
            import time
            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依赖<py-env>块来管理第三方包,所有代码中用到的非标准库必须在此声明,它会自动处理包的下载和安装流程。修改后运行代码,就能正常获取并展示商品的名称、价格和超市信息。

内容的提问来源于stack exchange,提问作者carlos lunar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 00:46:02