【发布时间】:2014-09-15 04:27:09
【问题描述】:
我正在用 php 中的 dom 解析器解析一些 iTunes 链接。对于大多数链接,它可以完美运行。其他完全一样的类型它不?!我需要“img”标签和“src-swap-high-dpi”属性。它让我发疯。这是我的 php 代码的一部分
$url = "https://itunes.apple.com/us/podcast/id278981407";
$htmlContent = str_get_html(file_get_contents($url));
foreach ($htmlContent->find("img") as $element) {
$value = $element->getAttribute("src-swap-high-dpi");
echo $value;
}
例如我可以解析以下链接: https://itunes.apple.com/us/podcast/id201671138
https://itunes.apple.com/us/podcast/id523121474
https://itunes.apple.com/us/podcast/id152249110
但是这个例如不是:
https://itunes.apple.com/us/podcast/id278981407
我没有得到任何输出。
编辑:
新代码也不起作用:
仍然不适合我。很奇怪。这就是我现在的新完整代码:
<?php
ini_set("display_errors",1); error_reporting(E_ALL);
require_once ('simple_html_dom.php');
$url = "https://itunes.apple.com/us/podcast/id278981407";
$htmlContent = str_get_html(file_get_contents($url));
foreach($htmlContent->find("div.artwork") as $div) {
$value = $div->find("img",0)->getAttribute("src-swap-high-dpi");
echo $value."<br/>";
}
?>
我得到了输出:
Fatal error: Call to a member function find() on a non-object in /home/www/whatever/delete.php on line 10
第 10 行是以“foreach”开头的行。您的代码可以与上面提供的链接正常工作,我声明它们可以正常工作。但是,一旦我选择了其中一个不起作用的指定的,我就会收到上面提供的错误消息。 ?!
【问题讨论】: