标签: lxml

Py2exe lxml的问题

我有一个依赖于lxml的wxpython应用程序,并且在通过python解释器运行时效果很好.但是,当使用py2exe创建一个exe时,我收到了这个错误

ImportError: No module named _elementpath
Run Code Online (Sandbox Code Playgroud)

我然后使用了python setup.py py2exe -p lxml ,我没有得到上述错误,但另一个说

ImportError: No module named gzip
Run Code Online (Sandbox Code Playgroud)

任何人都可以让我知道问题是什么以及如何解决它.我还应该在我的dist文件夹中放置libxml2,libxslt等任何dll文件吗?我搜索了计算机并没有找到这些文件,所以也许他们不需要?

谢谢.

编辑:我刚试过,python setup.py py2exe -p -i gzip并创建了exe.但是生成的exe不会运行.我双击它,它什么也没做.

这是我正在使用的setup.py脚本

from py2exe.build_exe import py2exe
from distutils.core import setup

setup( windows=[{"script": "gui.py"}] )
Run Code Online (Sandbox Code Playgroud)



Edit2:我尝试使用cx_freeze作为替代方案,但得到了相同的结果

ImportError: No module named _elementpath
Run Code Online (Sandbox Code Playgroud)

错误.之后不知道该怎么办.

python lxml wxpython py2exe

25
推荐指数
1
解决办法
9343
查看次数

如何在lxml xpath查询中使用空名称空间?

我有一个xml文档,格式如下:

<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:gsa="http://schemas.google.com/gsa/2007">
  ...
  <entry>
    <id>https://ip.ad.dr.ess:8000/feeds/diagnostics/smb://ip.ad.dr.ess/path/to/file</id>
    <updated>2011-11-07T21:32:39.795Z</updated>
    <app:edited xmlns:app="http://purl.org/atom/app#">2011-11-07T21:32:39.795Z</app:edited>
    <link rel="self" type="application/atom+xml" href="https://ip.ad.dr.ess:8000/feeds/diagnostics"/>
    <link rel="edit" type="application/atom+xml" href="https://ip.ad.dr.ess:8000/feeds/diagnostics"/>
    <gsa:content name="entryID">smb://ip.ad.dr.ess/path/to/directory</gsa:content>
    <gsa:content name="numCrawledURLs">7</gsa:content>
    <gsa:content name="numExcludedURLs">0</gsa:content>
    <gsa:content name="type">DirectoryContentData</gsa:content>
    <gsa:content name="numRetrievalErrors">0</gsa:content>
  </entry>
  <entry>
    ...
  </entry>
  ...
</feed>
Run Code Online (Sandbox Code Playgroud)

我需要entry在lxml中使用xpath 检索所有元素.我的问题是我无法弄清楚如何使用空名称空间.我尝试过以下示例,但都没有效果.请指教.

import lxml.etree as et

tree=et.fromstring(xml)    
Run Code Online (Sandbox Code Playgroud)

我尝试过的各种事情是:

for node in tree.xpath('//entry'):
Run Code Online (Sandbox Code Playgroud)

要么

namespaces = {None:"http://www.w3.org/2005/Atom" ,"openSearch":"http://a9.com/-/spec/opensearchrss/1.0/" ,"gsa":"http://schemas.google.com/gsa/2007"}

for node in tree.xpath('//entry', namespaces=ns):
Run Code Online (Sandbox Code Playgroud)

要么

for node in tree.xpath('//\"{http://www.w3.org/2005/Atom}entry\"'):
Run Code Online (Sandbox Code Playgroud)

在这一点上,我只是不知道该尝试什么.任何帮助是极大的赞赏.

python xml xpath lxml

25
推荐指数
1
解决办法
1万
查看次数

Windows上的Python 2.7上的easy_install lxml

我在Windows上使用python 2.7.当我尝试使用[setuptools] [2]的easy_install安装[lxml] [1]时,怎么会出现以下错误?

C:\>easy_install lxml
Searching for lxml
Reading http://pypi.python.org/simple/lxml/
Reading http://codespeak.net/lxml
Best match: lxml 2.3.3
Downloading http://lxml.de/files/lxml-2.3.3.tgz
Processing lxml-2.3.3.tgz
Running lxml-2.3.3\setup.py -q bdist_egg --dist-dir c:\users\my_user\appdata\local\temp\easy_install-mtrdj2\lxml-2.3.3\egg-dist-tmp-tq8rx4
Building lxml version 2.3.3.
Building without Cython.
ERROR: 'xslt-config' is not recognized as an internal or external command,
operable program or batch file.

** make sure the development packages of libxml2 and libxslt are installed **

Using build configuration of libxslt
warning: no files found matching 'lxml.etree.c' under directory 'src\lxml'
warning: no files …
Run Code Online (Sandbox Code Playgroud)

python lxml setuptools easy-install python-2.7

25
推荐指数
3
解决办法
6万
查看次数

使用lxml.html解析HTML时,相当于InnerHTML

我正在使用lxml.html编写一个脚本来解析网页.我在我的时间里做了很多BeautifulSoup,但由于它的速度,我现在正在尝试lxml.

我想知道库中最明智的方法是做相当于Javascript的InnerHtml - 即检索或设置标签的完整内容.

<body>
<h1>A title</h1>
<p>Some text</p>
</body>
Run Code Online (Sandbox Code Playgroud)

因此InnerHtml是:

<h1>A title</h1>
<p>Some text</p>
Run Code Online (Sandbox Code Playgroud)

我可以使用黑客(转换为字符串/正则表达式等)来做到这一点,但我假设有一个正确的方法来使用由于不熟悉我缺少的库.谢谢你的帮助.

编辑:感谢pobk如此快速有效地向我展示了这方面的方法.对于任何尝试相同的人,这是我最终得到的:

from lxml import html
from cStringIO import StringIO
t = html.parse(StringIO(
"""<body>
<h1>A title</h1>
<p>Some text</p>
Untagged text
<p>
Unclosed p tag
</body>"""))
root = t.getroot()
body = root.body
print (element.text or '') + ''.join([html.tostring(child) for child in body.iterdescendants()])
Run Code Online (Sandbox Code Playgroud)

请注意,lxml.html解析器将修复未关闭的标记,因此请注意这是否存在问题.

python parsing lxml

24
推荐指数
2
解决办法
1万
查看次数

使用python和lxml模块从html中删除所有javascript标签和样式标签

我正在使用http://lxml.de/库解析一个html文档.到目前为止,我已经想出如何从html文档中剥离标签在lxml中,如何删除标签但保留所有内容?但是该帖子中描述的方法会留下所有文本,剥离标签而不删除实际的脚本.我还发现了一类参考lxml.html.clean.Cleaner http://lxml.de/api/lxml.html.clean.Cleaner-class.html,但是这是明确的泥至于如何实际使用的类清理文件.任何帮助,也许是一个简短的例子对我有帮助!

html python lxml

24
推荐指数
2
解决办法
2万
查看次数

获取lxml中元素的内部HTML

我试图在Python中使用lxml和xpath获取子节点的HTML内容.如下面的代码所示,我想找到每个产品节点的html内容.它有像product.html这样的方法吗?

productGrids = tree.xpath("//div[@class='name']/parent::*")
for product in productGrids:
    print #html content of product
Run Code Online (Sandbox Code Playgroud)

python xpath lxml

24
推荐指数
2
解决办法
3万
查看次数

如何在lxml xpath中使用正则表达式?

我正在使用这样的结构:

doc = parse(url).getroot()
links = doc.xpath("//a[text()='some text']")
Run Code Online (Sandbox Code Playgroud)

但我需要选择所有包含以"some text"开头的文本的链接,所以我想知道有没有办法在这里使用regexp?在lxml文档中没有找到任何内容

python regex xpath lxml

23
推荐指数
2
解决办法
2万
查看次数

在Windows 8.1上安装lxml,libxml2,libxslt

经过额外的探索,我找到了一个解决方案,用pip和wheel安装lxml.欢迎就方法提出补充意见.

我发现Linux发行版的现有Python文档非常出色.对于Windows ......不是那么多.我已经配置好我的Linux系统,但我需要一些帮助才能准备好Windows 8.1平板电脑.

我的项目需要Python 3.4的lxml模块.我发现了很多关于如何安装lxml的教程,但每个教程都失败了.

https://docs.python.org/3/installing/ 我已经下载了"get-pip.py"并成功地从Windows cmd行运行了它,结果如下:

Requirement already up-to-date: pip in c:\python34\lib\site-packages
Run Code Online (Sandbox Code Playgroud)

所以我认为我没有"点"问题.从那里我从Windows cmd行运行以下命令:

python -m pip install lxml
Run Code Online (Sandbox Code Playgroud)

一长串命令滚动,但有几行似乎是我无法解决的错误.

  ERROR: b"'xslt-config' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n"
  ** make sure the development packages of libxml2 and libxslt are installed **
Run Code Online (Sandbox Code Playgroud)

Failed building wheel for lxml
Run Code Online (Sandbox Code Playgroud)

最后一部分都是红色的

Command "C:\Python34\python.exe -c "import setuptools, tokenize;__file__='C:\\Users\\Owner\\AppData\\Local\\Temp\\pip-build-ya3n6wkd\\lxml\\setup.py';exec(compi
le(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record C:\Users\Owner\AppData\Local\Temp\pip-ytybzl9l-r
ecord\install-record.txt --single-version-externally-managed --compile" failed with error code …
Run Code Online (Sandbox Code Playgroud)

python windows installation lxml module

23
推荐指数
2
解决办法
4万
查看次数

安装easy_install ...以安装lxml

我已经开始认识到ElementTree不会按照我的意愿去做.我已经查看了lxml的文档,看来它将满足我的目的.要获得lxml,我需要获得easy_install.所以我从这里下载了它,然后把它放进去/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/.然后我去了那个文件夹,跑了sh setuptools-0.6c11-py2.6.egg.

安装成功.然后我很兴奋,因为我认为easy_install的重点是我可以通过easy_install lxml进行安装,并且lxml会神奇地下载,构建和正确安装,为我的导入享受做好准备.所以我跑了easy_install lxml.我粘贴了下面的结果.我该怎么办?

easy_install lxml
Searching for lxml
Reading http://pypi.python.org/simple/lxml/
Reading http://codespeak.net/lxml
Best match: lxml 2.2.6
Downloading http://codespeak.net/lxml/lxml-2.2.6.tgz
Processing lxml-2.2.6.tgz
Running lxml-2.2.6/setup.py -q bdist_egg --dist-dir /var/folders/49/49N0+g5QFKCm51AbzMtghE+++TI/-Tmp-/easy_install-rxbP6K/lxml-2.2.6/egg-dist-tmp-fjakR0
Building lxml version 2.2.6.
NOTE: Trying to build without Cython, pre-generated 'src/lxml/lxml.etree.c' needs to be available.
Using build configuration of libxslt 1.1.12
Building against libxml2/libxslt in the following directory: /usr/lib
unable to execute gcc-4.0: No such file or directory
error: Setup script exited with …
Run Code Online (Sandbox Code Playgroud)

python lxml easy-install

22
推荐指数
3
解决办法
2万
查看次数

使用Python lxml时出错"加载外部实体失败"

我正在尝试解析从Web检索的XML文档,但在解析此错误后崩溃了:

': failed to load external entity "<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="GreenButtonDataStyleSheet.xslt"?>
Run Code Online (Sandbox Code Playgroud)

这是下载的XML中的第二行.有没有办法阻止解析器尝试加载外部实体,或者另一种方法来解决这个问题?这是我到目前为止的代码:

import urllib2
import lxml.etree as etree

file = urllib2.urlopen("http://www.greenbuttondata.org/data/15MinLP_15Days.xml")
data = file.read()
file.close()

tree = etree.parse(data)
Run Code Online (Sandbox Code Playgroud)

python xml lxml elementtree

22
推荐指数
3
解决办法
3万
查看次数