Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
什么';这个html dom php代码中有什么错误?_Php_Html_Dom_Domdocument - Fatal编程技术网

什么';这个html dom php代码中有什么错误?

什么';这个html dom php代码中有什么错误?,php,html,dom,domdocument,Php,Html,Dom,Domdocument,我正在尝试执行一个代码,该代码将使用itemprop=“price”从某个链接打印所有元素的内容,但它不起作用,我不知道为什么,这是代码: <?php error_reporting(0); ini_set('display_errors', 0); $doc = new DOMDocument(); $allscan = array( 'http://www.mobile54.co.il/30786', 'http://www.mobile54.co.il/3

我正在尝试执行一个代码,该代码将使用
itemprop=“price”
从某个链接打印所有元素的内容,但它不起作用,我不知道为什么,这是代码:

<?php
error_reporting(0);
ini_set('display_errors', 0);
$doc      = new DOMDocument();
$allscan  = array(
    'http://www.mobile54.co.il/30786',
    'http://www.mobile54.co.il/35873',
    'http://www.mobile54.co.il/34722'
);
$alllinks = array();
$html     = file_get_contents($allscan[0]);
$doc->loadHTML($html);
$href = $doc->getElementsByTagName('a');
for ($j = 0; $j < count($allscan); $j++) {
    $html = file_get_contents($allscan[$j]);
    $doc->loadHTML($html);
    $href = $doc->getElementsByTagName('a');
    for ($i = 0; $i < $href->length; $i++) {
        $link = $href->item($i)->getAttribute("href");
        $lin  = preg_replace('/\s+/', '', 'http://www.mobile54.co.il' . $link . "<br />");
        if (strpos($link, 'items/') && !strpos($link, '#techDetailsAName')) {
            if (!in_array($lin, $alllinks)) {
                $alllinks[] = $lin;
            }
        }
    }
}

for ($i = 0; $i < count($alllinks); $i++) {
    echo $alllinks[$i];
}
for ($i = 0; $i < count($alllinks); $i++) {
    $lin  = "$alllinks[$i]";
    $html = file_get_contents($lin);
    $doc->loadHTML('<?xml encoding="UTF-8"?>' . $html);
    $span = $doc->getElementsByTagName('span');
    for ($j = 0; $j < $span->length; $j++) {
        $attr = $span->item($j)->getAttribute('itemprop');
        if ($attr == "price") {
            echo $span->item($j)->textContent . "<br />";
        }
    }
}


?> 


当我粘贴“someurl”而不是
$lin
时,它可以工作,但另一种方式不行。我尝试过做
$html=file\u get\u contents($alllinks[$I])
但它不起作用,我不知道为什么。

我想你的问题可能是因为某种原因,你在URL的末尾附加了一个

。但是,有很多机会可以通过使用来改进代码。(还请注意,您可以直接将URL传递给DomDocument对象。)

首先,我们提取所有的
。我们获取URL,然后在其中搜索具有完全匹配的
itemprop
属性的元素,并获取它们的名称


我认为您的问题可能是由于某种原因在URL的末尾附加了一个

。但是,有很多机会可以通过使用来改进代码。(还请注意,您可以直接将URL传递给DomDocument对象。)

首先,我们提取所有的
。我们获取URL,然后在其中搜索具有完全匹配的
itemprop
属性的元素,并获取它们的名称


首先非常感谢!它解决了问题,我没有看到。第二,我真的不明白你的代码,我不知道你在那里做了什么。。。它怎么比另一个更好?(更快?)您可以看到代码更短,但也更易于阅读,并提供更好的性能。学习XPath可能很棘手,但是网上有很多参考资料。如果你知道的话,你能给我发一份参考资料吗?你能帮我做另一件事吗?首先非常感谢!它解决了问题,我没有看到。第二,我真的不明白你的代码,我不知道你在那里做了什么。。。它怎么比另一个更好?(更快?)您可以看到代码更短,但也更易于阅读,并提供更好的性能。学习XPath可能很棘手,但是网上有很多参考资料。如果你知道的话,你能给我发一份参考资料吗?嗯,你能帮我做另一件事吗?
<?php
$url = "http://www.mobile54.co.il/30786";
$prices = [];
$hrefs = [];
$combined = [];

$dom = new DomDocument;
libxml_use_internal_errors(true);
$dom->loadHtmlFile($url);
$xpath = new DomXPath($dom);
// get <a> elements with href containing items/ but not #techDetailsAName
$nodes = $xpath->query("//a[contains(@href, 'items/') and not(contains(@href, '#techDetailsAName'))]/@href");
foreach ($nodes as $node) {
    $hrefs[] = trim($node->value);
}

// now you have a list of URLs
foreach ($hrefs as $k=>&$href) {
    $href = "http://www.mobile54.co.il$href";
    $dom->loadHtmlFile($href);
    $xpath = new DomXPath($dom);
    // get any element with itemprop of price
    $nodes = $xpath->query("//*[@itemprop='price']");
    $prices[$k] = $nodes->item(0)->textContent;
}

// now you have $urls and $prices, combine them:
foreach ($hrefs as $k=>$v) {
    $combined[$k] = [$hrefs[$k], $prices[$k]];
}
print_r($combined);