首页 \ 问答 \ 使用DOMDocument解析html(parsing html with DOMDocument)

使用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
更新时间:2023-02-23 16:02

最满意答案

按照以下说明在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 ...
  • 尝试这个: 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 ...
  • 将所有库(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. ...
  • 您的代码有以下错误: 你在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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)