使用 py2exe 和 BeautifulSoup。脚本 运行 很好,但在转换为 .exe 后,它显示错误

Working with py2exe and BeautifulSoup. Script run fine but after converting into .exe , it shows error

我用 beautifulSoup 创建了一个脚本来废弃网站。当我 运行 编写脚本时,我得到了我想要的。 因此,继续前进,我决定将其转换为 .exe 文件。转换已经完成,除了它显示 --- the following modules appear to be missing '_scproxy', 'builder.parserRejectedMarkup','builder.builder_registry', 'cchardet', 'chardet', 'html.parser', 'htmlslib', 'html5lib', 'iconvz' etc.

但是它创建了 .exe 文件,当我尝试 运行 .exe 文件时它显示--

bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?

我尝试卸载并重新安装软件包并尝试搜索可用内容,但这没有帮助。 这是 Script(p3_extract.py):

的代码
from bs4 import BeautifulSoup
import urllib2
import lxml
url="http://fuckinghomepage.com/"
page= urllib2.urlopen(url)
soup_package = BeautifulSoup(page, "lxml")
p1_soup= soup_package.find("p")
p2_soup = p1_soup.next_sibling
p3_soup = p2_soup.next_sibling
print p3_soup.string

这里是 setup.py:

from distutils.core import setup
import py2exe
import lxml
setup(console=['p3_extract.py'])

帮帮我。(我在 win10-32bit 和 python 2.7 上这样做。) 谢谢

我在编译使用 lxmlselenium(而不是 bs4)的项目时遇到了类似的问题。解决方案是在您的 setup.py py2exe 选项中导入包,而不是像您那样在脚本中导入它们...

为您的 setup.py 添加选项

您可以使用 more py2exe options 以确保您正在导入项目所需的所有模块和包。例如

# setup.py
from distutils.core import setup
import py2exe
setup(console=["p3_exctract.py"],
      options={
              "py2exe":{
                    "packages": ["lxml"] # List of the package you want to make sure that will be imported
               }
       }
    )

通过这种方式,您可以强制导入您的项目缺少的脚本