GVKun编程网logo

为什么用lxml处理XHTML文档(在python中)时xpath无法正常工作?(python lxml xpath)

21

以上就是给各位分享为什么用lxml处理XHTML文档,其中也会对在python中时xpath无法正常工作?进行解释,同时本文还将给你拓展apache.cxfCDIwithh@ApplicationPa

以上就是给各位分享为什么用lxml处理XHTML文档,其中也会对在python中时xpath无法正常工作?进行解释,同时本文还将给你拓展apache.cxf CDI withh @ApplicationPath无法正常工作、linux – 为什么密码少ssh无法正常工作?、map在python 3中无法正常工作、Python lxml XPath问题等相关知识,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

为什么用lxml处理XHTML文档(在python中)时xpath无法正常工作?(python lxml xpath)

为什么用lxml处理XHTML文档(在python中)时xpath无法正常工作?(python lxml xpath)

我正在针对以下测试文档进行测试:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"                       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml">   <head>        <title>hi there</title>    </head>    <body>        <imgsrc="bar.png"/>    </body></html>

如果使用lxml.html解析文档,则可以使用xpath获得IMG:

>>> root = lxml.html.fromstring(doc)>>> root.xpath("//img")[<Element img at 1879e30>]

但是,如果我将文档解析为XML并尝试获取IMG标签,则会得到空结果:

>>> tree = etree.parse(StringIO(doc))>>> tree.getroot().xpath("//img")[]

我可以直接导航到该元素:

>>> tree.getroot().getchildren()[1].getchildren()[0]<Element {http://www.w3.org/1999/xhtml}img at f56810>

但这当然无助于我处理任意文档。我还希望能够查询etree以获得直接识别该元素的xpath表达式,从技术上讲,我可以这样做:

>>> tree.getpath(tree.getroot().getchildren()[1].getchildren()[0])''/*/*[2]/*''>>> tree.getroot().xpath(''/*/*[2]/*'')[<Element {http://www.w3.org/1999/xhtml}img at fa1750>]

但是,同样,该xpath显然对解析任意文档没有用。

显然,我在这里缺少一些关键问题,但是我不知道这是什么。我最好的猜测是,它与名称空间有关,但是定义的唯一名称空间是默认名称,我不知道关于名称空间我还需要考虑什么。

那么,我想念什么?

答案1

小编典典

问题是名称空间。当解析为XML时,img标记位于http://www.w3.org/1999/xhtml命名空间中,因为这是该元素的默认命名空间。您在没有名称空间的情况下要求img标记。

尝试这个:

>>> tree.getroot().xpath(...     "//xhtml:img", ...     namespaces={''xhtml'':''http://www.w3.org/1999/xhtml''}...     )[<Element {http://www.w3.org/1999/xhtml}img at 11a29e0>]

apache.cxf CDI withh @ApplicationPath无法正常工作

apache.cxf CDI withh @ApplicationPath无法正常工作

如何解决apache.cxf CDI withh @ApplicationPath无法正常工作?

我正在尝试使用apache.cxf运行tomcat应用程序,但我得到了: 找不到对http:// localhost:8080 / myApp / rest / test / hello / message /的观察者的请求

这是我的代码:

MyApp类:

@ApplicationPath("/test")
public class MyApp extends Application { }

你好班:

@Path("/hello")
 public class Hello {

    @GET
    @Path("/message/")
    @Produces(MediaType.APPLICATION_JSON)
    public Response test() {
        System.out.println("hello there ...");
        return Response.ok().entity("hello there ... ").build();
    }

}

我的Maven依赖项:

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-bundle -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-integration-cdi</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.weld.servlet</groupId>
        <artifactId>weld-servlet</artifactId>
        <version>1.1.10.Final</version>
    </dependency>

</dependencies>

web.xml:

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 
    <listener>
        <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
    </listener>
 
    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <display-name>CXF Servlet</display-name>
        <servlet-class>org.apache.cxf.cdi.CXFCdiservlet</servlet-class>    
        <load-on-startup>1</load-on-startup>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
 
    <resource-env-ref>
        <resource-env-ref-name>BeanManager</resource-env-ref-name>
        <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager
        </resource-env-ref-type>
    </resource-env-ref>
</web-app>

谁能告诉我怎么了?我遵循了文档:

https://cxf.apache.org/docs/using-cxf-and-cdi-11-jsr-346.html

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

linux – 为什么密码少ssh无法正常工作?

linux – 为什么密码少ssh无法正常工作?

我在我的集​​群中连接了3个数据节点(在所有这些数据节点中通过无字工作正常),这些节点工作正常,但是当我尝试连接另一个数据节点时,无字源ssh无法在第四个数据节点中工作.

前三个数据节点的IP地址:

172.20.93.192(name node)
172.20.94.189(data node)
172.20.94.145(data node)
172.20.94.193(data node)

现在我的第四个数据节点的IP地址是172.20.95.6,其中无密码不起作用.

我正在生成密钥

ssh-keygen -t rsa

我正在为第四个数据节点执行与上述三个数据节点相同的过程,但它不起作用.为什么?可能是什么原因?

解决方法

获得“真实”原因需要更多信息.然而,这里有两个我发现的最常见问题,与密钥配置本身无关(考虑到你使用Linux

总结

以上是小编为你收集整理的linux – 为什么密码少ssh无法正常工作?全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

map在python 3中无法正常工作

map在python 3中无法正常工作

新手在这里.

这段代码在python 2.7中有效,但在3.3中没有

def extractFromZipFiles(zipFiles,files,toPath):
    extractFunction = lambda fileName:zipFiles.extract(fileName,toPath) 
    map (extractFunction,files)
    return

没有错误,但文件未被提取.但是,当我用for循环替换工作正常.

def extractFromZipFiles(zipFiles,toPath):
    for fn in files:
        zipFiles.extract(fn,toPath)
#     extractFunction = lambda fileName:zipFiles.extract(fileName,toPath) 
#     map (extractFunction,files)
    return

代码没有错误.

解决方法

通常不鼓励使用map来调用函数,但是说,它不起作用的原因是因为Python 3返回一个生成器,而不是一个列表,所以在你迭代它之前不会调用该函数.确保它调用函数:

list(map(extractFunction,files))

但它正在创建一个未使用的列表.更好的方法是更明确:

for file in files:
    extractFunction(file)

与头部的情况一样,两条线确实可以比一条线好.

Python lxml XPath问题

Python lxml XPath问题

我正在尝试从网页中打印/保存特定元素的HTML。
我已经从萤火虫中检索了所请求元素的XPath。

我只希望将该元素保存到文件中。我似乎没有成功。
(尝试在XPath/text()的结尾加上和不结尾)

我将不胜感激,或者有以往的经验。
10x,大卫

import urllib2,StringIO
from lxml import etree

url='http://www.tutiempo.net/en/Climate/Londres_Heathrow_Airport/12-2009/37720.htm'
seite = urllib2.urlopen(url)
html = seite.read()
seite.close()
parser = etree.HTMLParser()
tree = etree.parse(StringIO.StringIO(html),parser)
xpath = "/html/body/table/tbody/tr/td[2]/div/table/tbody/tr[6]/td/table/tbody/tr/td[3]/table/tbody/tr[3]/td/table/tbody/tr/td/table/tbody/tr/td/table/tbody/text()"
elem = tree.xpath(xpath)


print elem[0].strip().encode("utf-8")

关于为什么用lxml处理XHTML文档在python中时xpath无法正常工作?的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于apache.cxf CDI withh @ApplicationPath无法正常工作、linux – 为什么密码少ssh无法正常工作?、map在python 3中无法正常工作、Python lxml XPath问题等相关内容,可以在本站寻找。

本文标签: