标题总结了它.我正在尝试查询HTML文件以查找包含该类result且不包含该类的所有div标记grid.
<div class="result grid">skip this div</div>
<div class="result">grab this one</div>
Run Code Online (Sandbox Code Playgroud)
谢谢!
所以我有一个像这样的HTML字符串:
<td class="name">
<a href="/blah/somename23123">Some Name</a>
</td>
<td class="name">
<a href="/blah/somename28787">Some Name2</a>
</td>
Run Code Online (Sandbox Code Playgroud)
使用XPath我可以使用此Xpath查询获取href属性的值:
$domXpath = new \DOMXPath($this->domPage);
$hrefs = $domXpath->query("//td[@class='name']/a/@href");
foreach($hrefs as $href) {...}
Run Code Online (Sandbox Code Playgroud)
而获取文本值更容易,如下所示:
// Xpath auto. strips any html tags so we are
// left with clean text value of a element
$domXpath = new \DOMXPath($this->domPage);
$names = $domXpath->query("//td[@class='name']/");
foreach($names as $name) {...}
Run Code Online (Sandbox Code Playgroud)
现在我很想知道,我怎样才能将这两个查询结合起来,只用一个查询来获取两个值(如果它是类似的甚至是可行的?).
使用 DOMXPath::query 是否可以只获取一层深度的子节点?
例如,如果我有一个类似的文档:
<div>
<span>
<cite>
</cite>
</span>
<span>
<cite>
</cite>
</span>
</div>
Run Code Online (Sandbox Code Playgroud)
我希望 NodeList 仅包含跨度而不包含引用。
还应该提到的是,它并不总是相同的元素(div、span 等)。我需要它与任何类型的元素一起使用。
这是我尝试过的,但似乎不起作用:
//*[not(ancestor::div)]
Run Code Online (Sandbox Code Playgroud) 我试图从网站上删除一些内容,但下面的代码不起作用(没有显示任何输出).这是代码
$url="some url";
$otherHeaders=""; //here i am using some other headers like content-type,userAgent,etc
some curl to get the webpage
...
..
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$content=curl_exec($ch);curl_close($ch);
$page=new DOMDocument();
$xpath=new DOMXPath($page);
$content=getXHTML($content); //this is a tidy function to convert bad html to xhtml
$page->loadHTML($content); // its okay till here when i echo $page->saveHTML the page is displayed
$path1="//body/table[4]/tbody/tr[3]/td[4]";
$path2="//body/table[4]/tbody/tr[1]/td[4]";
$item1=$xpath->query($path1);
$item2=$xpath->query($path2);
echo $item1->length; //this shows zero
echo $item2->length; //this shows zero
foreach($item1 as $t)
echo $t->nodeValue; //doesnt show anything
foreach($item2 as $p)
echo $p->nodeValue; …Run Code Online (Sandbox Code Playgroud) 我在理解childNodes中存储的内容时遇到了一些麻烦.理想情况下,我想在每个子节点上做另一个xquery,但似乎无法直截了当.这是我的方案:数据:
<div class="something">
<h3>
<a href="link1.html">Link text 1</a>
</h3>
<div class"somethingelse">Something else text 1</div>
</div>
<div class="something">
<h3>
<a href="link2.html">Link text 2</a>
</h3>
<div class"somethingelse">Something else text 2</div>
</div>
<div class="something">
<h3>
<a href="link3.html">Link text 3</a>
</h3>
<div class"somethingelse">Something else text 3</div>
</div>
Run Code Online (Sandbox Code Playgroud)
和代码:
$html = new DOMDocument();
$html->loadHtmlFile($local_file);
$xpath = new DOMXPath( $html );
$nodelist = $xpath->query( "//div[@class='something']");
foreach ($nodelist as $n) {
Can I run another query here? }
Run Code Online (Sandbox Code Playgroud)
对于"某事"的每个元素(即$ n),我想访问两段文本和href的值.我尝试使用childNode和另一个xquery,但无法获得任何工作.任何帮助将不胜感激!
我正在寻找一种方法来选择PHP最里面的div
例如:
<div>
<div>
<div>
-
</div>
</div>
<div>
<div>
<div>
-
</div>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
在DIV含有的-将在被选择NodeList
我使用DOMDocument和DOMXpath来抛出html,继承人和我的一个方法的例子,这样你就可以看到我的类的创建方式.
public function getkeywords()
{
foreach($this->Xpath->query('/html/head/meta[@content][@name="keywords"][1]') as $node)
{
$words = $node->getAttribute('content');
if($words)
{
return explode(',',str_replace(array(", "," ,"),",",$words));
}
return false;
}
return false;
}
Run Code Online (Sandbox Code Playgroud) <br/>如果之前或之后没有文字,我该如何删除?
例如,
<p><br/>hello</p>
<p>hello<br/></p>
Run Code Online (Sandbox Code Playgroud)
他们应该像这样重写,
<p>hello</p>
<p>hello</p>
Run Code Online (Sandbox Code Playgroud)
我应该使用DOMxpath还是正则表达式会更好?
(注意:我有一篇关于先前使用DOMxpath 删除的帖子<p><br/></p>,然后我遇到了这个问题!)
编辑:
如果我在输入中有这个,
$content = '<p><br/>hello<br/>hello<br/></p>';
Run Code Online (Sandbox Code Playgroud)
那应该是
<p>hello<br/>hello</p>'
Run Code Online (Sandbox Code Playgroud) 我正在构建一个使用XPath来分析HTML的命令行php scraping app - 问题是每次在循环中加载新的DOMXPath类实例时我的内存丢失大致等于正在加载的XML的大小.该脚本运行并运行,慢慢增加内存使用量,直到达到限制并退出.
我已经尝试强制垃圾收集,gc_collect_cycles()PHP仍然没有从旧的Xpath请求中获取内存.实际上,DOMXPath类的定义似乎甚至不包含析构函数?
所以我的问题是...... DOMXPath在我已经提取了必要的数据后,有没有办法强制垃圾清理?在类实例上使用unset可以预测不会做任何事情.
代码没什么特别的,只是标准的Xpath东西:
//Loaded outside of loop
$this->dom = new DOMDocument();
//Inside Loop
$this->dom->loadHTML($output);
$xpath = new DOMXPath($this->dom);
$nodes = $xpath->query("//span[@class='ckass']");
//unset($this->dom) and unset($xpath) doesn't seem to have any effect
Run Code Online (Sandbox Code Playgroud)
正如您在上面所看到的,我已经DOMDocument在循环之外保留了新类的实例化,尽管这似乎并没有提高性能.我甚至尝试将$xpath类实例从循环中取出并使用该__constructor方法直接将DOM加载到Xpath中,内存丢失是相同的.
可能重复:
如何使用XPath在XML文档中选择多组属性?
我的HTML代码:
<table width="100%" cellpadding="6" cellspacing="0">
Run Code Online (Sandbox Code Playgroud)
我想通过不仅指定宽度而且通过cellpadding和cellspacing选择此表.
我正在使用这个PHP代码:
$query = $xpath->query('//table[@width|@cellpadding|@cellspacing]');
Run Code Online (Sandbox Code Playgroud)
但它仍然显示整个HTML源而不是我想要的...
请帮助我..
domxpath ×10
php ×10
xpath ×9
dom ×2
html ×2
domdocument ×1
evaluate ×1
regex ×1
web-scraping ×1
xhtml ×1
xpathquery ×1