【发布时间】:2017-08-02 00:13:38
【问题描述】:
好的。我再次陷入困境,似乎互联网刚刚用完 HTML DOM 教程遍历 dom。我有这个页面(http://www.nasdaqomxbaltic.com/market/?pg=news&news_id=250910),我想要做的是将文本The statement of shareholders for shares sale and for shares purchase attached. 和附件放入一个变量中。我正在尝试以最有效的方式做到这一点,所以我没有使用 simple_html_dom。如果我有选择或者它会更快,我不会使用 xpath,但我不确定:)
编辑:尝试了 Phil 的代码。似乎无法弄清楚为什么它仍然不起作用。
<?
$dom = new DOMDocument();
@$dom->loadHTMLFile("http://www.nasdaqomxbaltic.com/market/?pg=news&news_id=250910");
$xpath = new DOMXPath($dom);
$paragraph = $xpath->query('//table[@id="previewTable"]/tbody/tr[2]/td/p');//tried removing tbody, doesn't fix, why is it there?
if ($paragraph->length == 1) {//what is this?
$sentence = $paragraph->nodeValue;
print_r($sentence);//doesnt work (blank)
}
$links = $xpath->query('//table[@id="previewTable"]//td[@class="tdAttachment"]//a');
foreach ($links as $link) {
$linkName = $link->nodeValue;
$linkUrl = $link->getAttribute('href');
echo $linkName;
echo $linkUrl;//works
}
?>
【问题讨论】:
-
不——它不能那样工作
-
你不需要
file_get_contents(),只需使用DOMDocument::loadHTMLFile()