使用DOMDocument解析html(parsing html with DOMDocument)
我在php中使用DOMDocument解析html。
我发现我无法使用xpath查询选择所有内容。 但是getElementsByTagName()方法工作正常。
这是代码:
$xml = new DOMDocument(); $xml->load("file.html"); $xpath = new DOMXPath($xml); $links = $xpath->query("//a"); $links2 = $xml->getElementsByTagName("a"); foreach($links as $link){ echo "<br>$k: ".$link->nodeValue; // this doesn't print the node value. $links is empty } foreach($links2 as $link){ echo "<br>$k: ".$link->nodeValue; // this prints OK the node value }
我原以为xpath-> query(“// a”)与getElementsByTagname(“a”)相同,但显然不是。
谁能告诉我为什么他们不一样了。 或者,如果它们是,我使用xpath查询选择节点我做错了什么?
谢谢
I'm parsing html with DOMDocument in php.
I found I'm unable to select all using an xpath query. However the getElementsByTagName() method works fine.
Here is the code:
$xml = new DOMDocument(); $xml->load("file.html"); $xpath = new DOMXPath($xml); $links = $xpath->query("//a"); $links2 = $xml->getElementsByTagName("a"); foreach($links as $link){ echo "<br>$k: ".$link->nodeValue; // this doesn't print the node value. $links is empty } foreach($links2 as $link){ echo "<br>$k: ".$link->nodeValue; // this prints OK the node value }
I'd have thought xpath->query("//a") would be the same as getElementsByTagname("a") but apparently isn't.
Could anybody tell me why they aren't the same. Or if they are, what am I doing wrong to select the nodes using the xpath query?
Thank you
原文:https://stackoverflow.com/questions/6730555
最满意答案
按照以下说明在Anaconda 2.4中安装Kivy 1.9:
http://kivy.org/docs/installation/installation-osx.html
您还需要pygame,并且需要按照其网站上的OS X说明进行操作。
Follow these instructions to install Kivy 1.9 in Anaconda 2.4:
http://kivy.org/docs/installation/installation-osx.html
You will also need pygame and need to follow instructions on their website for OS X.
相关问答
更多-
好吧,自己解决它。 事实证明,如果我在工厂中为孩子小部件提供大小,它们的位置就会正确。 虽然它解决了我所遇到的问题,但如果有人能够解释这个怪癖从何而来,我仍然会很感激。 Okay, solved it myself. It turns out that if I supply the size to child widgets in factory, they are positioned properly. Although it solves the problem I have, I'd still ...
-
无法在kivy启动器中为Android运行“hello world”python代码(can't run “hello world” python code in kivy launcher for android)[2023-01-25]
尝试这个: import kivy kivy.require('1.9.0') from kivy.app import App from kivy.lang import Builder kv = ''' Label: text: "Hello World" ''' class TestApp(App): def build(self): return Builder.load_string(kv) if __name__ == '__main__': Test ... -
看起来错误发生在jsfiddle的代码中:'heyoffline.js'有一个addEventListener调用,在IE8中不支持。 注意:我可以使用所选的其他库重现它,因此它与jQuery1.9无关。 Looks like the error happens in jsfiddle's code: 'heyoffline.js' has an addEventListener call which is not supported in IE8. Note: I can reproduce it wit ...
-
python3的pygame安装是使用以下代码来解决它的问题: hg clone https://bitbucket.org/pygame/pygame cd pygame python3 setup.py build sudo python3 setup.py install cd .. sudo rm -rf pygame 基于kivy安装说明 The pygame installation for python3 was the problem use following code to solve ...
-
好吧,经过一些谷歌搜索,我终于找到了答案。 我按照本指南开始工作: 如何使用PTVS开发和运行Kivy应用程序 而且从2013年开始 ,这表明您不能将Kivy用作常规模块。 如何使用PyDev开发和运行Kivy应用程序 Okay so after some googling, i finally found my answer. I followed this guide and got everything to work: How to use PTVS to develop and run Kivy ...
-
RVM是迄今为止最好的方法。 请参阅此处的优秀说明 使用RVM,我可以在一个非常有效且易于使用的Sandbox中使用多个Ruby和相关gem的安装。 您可能会或可能不会欣赏的一个功能是它打破了apt的链接,根据需要编译源代码。 我在Ubuntu 10.10上使用它 RVM is by far the best way to do this. See the excellent instructions here With RVM I can use multiple installs of Ruby and ...
-
按照以下说明在Anaconda 2.4中安装Kivy 1.9: http://kivy.org/docs/installation/installation-osx.html 您还需要pygame,并且需要按照其网站上的OS X说明进行操作。 Follow these instructions to install Kivy 1.9 in Anaconda 2.4: http://kivy.org/docs/installation/installation-osx.html You will also n ...
-
带有API Manager 1.9.0的WSO2 DAS 3.0.0无法运行(WSO2 DAS 3.0.0 with API Manager 1.9.0 not working)[2022-02-28]
将所有库(jar)移动到项目的/ WEB-INF / lib中。 现在/ WEB-INF / lib下的所有库/ jar都将归入classpath。 使用snappy-java jar文件,它将按你的意愿工作。 Move all the libraries(jars) into your project's /WEB-INF/lib. Now all the libraries/jars under /WEB-INF/lib will come under classpath. use snappy-jav ... -
这个问题也发生在16.04我。 我的解决方案,删除ubuntu软件包(保存一边),并用pip安装: sudo apt-get remove python3-kivy sudo pip3 install kivy As inclement said in his comment switching to kivy-daily resolved the problem! I had to do a dist-upgrade on my machine to resolve some dependencies. ...
-
Python / Kivy属性错误(Python/Kivy Attribute Error)[2023-01-19]
您的代码有以下错误: 你在kv文件中写了sendButton而不是SendButton 。 同样在kv文件中,您编写了db(50)而不是dp(50) 。 使用字符串'50dp'也是一个有效的选项。 Your code has these errors: You wrote sendButton instead of SendButton in kv file. Also in kv file, you wrote db(50) instead of dp(50). Using string '50dp' w ...