Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.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
Php 从站点获取节点值,并仅重新输出可设置样式的选定节点标记_Php_Regex_Xpath_Screen Scraping_Nodevalue - Fatal编程技术网

Php 从站点获取节点值,并仅重新输出可设置样式的选定节点标记

Php 从站点获取节点值,并仅重新输出可设置样式的选定节点标记,php,regex,xpath,screen-scraping,nodevalue,Php,Regex,Xpath,Screen Scraping,Nodevalue,我正在努力使它与php一起工作 问题是:我只是想从一个网站上刮下产品,让它们显示为一个产品列表,而没有任何其他我可以用css样式的东西。我想输出的是$productText作为站点的产品列表(基本上是将其删除)。这是一个我正在尝试的有趣的项目,下面是代码: $html = new DOMDocument(); @$html->loadHtmlFile('http://www.amazon.com/gp/search/ref=sr_nr_p_8_3?rh=n%3A2619533011%2Ck

我正在努力使它与php一起工作

问题是:我只是想从一个网站上刮下产品,让它们显示为一个产品列表,而没有任何其他我可以用css样式的东西。我想输出的是

$productText

作为站点的产品列表(基本上是将其删除)。这是一个我正在尝试的有趣的项目,下面是代码:

$html = new DOMDocument();
@$html->loadHtmlFile('http://www.amazon.com/gp/search/ref=sr_nr_p_8_3?rh=n%3A2619533011%2Ck%3Apet+products%2Cn%3A%212619534011%2Cn%3A2975312011%2Cp_72%3A2661618011%2Cp_8%3A2661607011&bbn=2975312011&keywords=pet+products&ie=UTF8&qid=1328429080&rnid=2661603011#/ref=sr_st?bbn=2975312011&keywords=pet+products&qid=1328429127&rh=n%3A2619533011%2Ck%3Apet+products%2Cn%3A!2619534011%2Cn%3A2975312011%2Cp_72%3A2661618011%2Cp_8%3A2661607011');
$xpath = new DOMXPath( $html );
$productName = $xpath->query( "//div[@id='btfResults']/div/div[4]/div[1]/a/text()" );
$link = $xpath->query( "//div[@id='btfResults']/div/div[3]/a/@href" );
$image = $xpath->query( "//div[@id='btfResults']/div/div[3]/a/img/@src" );

foreach ($productName as $n){
$productText = $n->nodeValue;
}

foreach ($image as $n){
$imageLink = $n->nodeValue;
}

foreach ($link as $n){
$linkLink = $n->nodeValue;
}
 foreach ($link as $n)
{
echo "<div id='product'><a href= $linkLink ><img src= $imageLink /></a><br/><p>$productText</p></div>";
}
$html=newDOMDocument();
@$html->loadHtmlFile('http://www.amazon.com/gp/search/ref=sr_nr_p_8_3?rh=n%3A2619533011%2Ck%3Apet+产品%2Cn%3A%212619534011%2Cn%3A297532011%2Cp\U 72%3A2661618011%2Cp\U 8%3A2661607011&bbn=2975311011&keywords=pet+products&ie=UTF8&qid=1328429080&rnid=2661603011/ref=sr\U st?bbn=29753112011&keywords=pet+products&qid=1328429127&rh=n%3A2619533011%3AK+products%2Cn%3A!2619534011%2Cn%3A297532011%2Cp_72%3A2661618011%2Cp_8%3A2661607011’;
$xpath=newdomxpath($html);
$productName=$xpath->query(“//div[@id='btfResults']/div/div[4]/div[1]/a/text()”;
$link=$xpath->query(“//div[@id='btfResults']/div/div[3]/a/@href”);
$image=$xpath->query(//div[@id='btfResults']/div/div[3]/a/img/@src”);
foreach($productName作为$n){
$productText=$n->nodeValue;
}
foreach($n图像){
$imageLink=$n->nodeValue;
}
foreach($n链接){
$linkLink=$n->nodeValue;
}
foreach($n链接)
{
回显“
$productText

”; }
事实上,我不知道如何获得想要的正确结果。如果需要进一步解释,请告诉我。谢谢!

修复并测试:

<?php
$html = new DOMDocument();
@$html->loadHtmlFile('http://www.amazon.com/gp/search/ref=sr_nr_p_8_3?rh=n%3A2619533011%2Ck%3Apet+products%2Cn%3A%212619534011%2Cn%3A2975312011%2Cp_72%3A2661618011%2Cp_8%3A2661607011&bbn=2975312011&keywords=pet+products&ie=UTF8&qid=1328429080&rnid=2661603011#/ref=sr_st?bbn=2975312011&keywords=pet+products&qid=1328429127&rh=n%3A2619533011%2Ck%3Apet+products%2Cn%3A!2619534011%2Cn%3A2975312011%2Cp_72%3A2661618011%2Cp_8%3A2661607011');
$xpath = new DOMXPath( $html );
$btfResults = $xpath->query("//div[@id='btfResults']/div"); // get all item nodes
foreach ($btfResults as $node) // iterate through all items
{
    $productText = $linkLink = $imageLink = null; // reset result variables for each loop
    if ($productName = $xpath->query("./div[4]/div[1]/a/text()", $node)->item(0)) // fetch productName node from the item
    {
        $productText = $productName->nodeValue;
    }
    if ($link = $xpath->query("./div[3]/a/@href", $node)->item(0)) // fetch link node from the item
    {
        $linkLink = $link->nodeValue;
    }
    if ($image = $xpath->query("./div[3]/a/img/@src", $node)->item(0)) // fetch image node from the item
    {
        $imageLink = $image->nodeValue;
    }

    if ($productText && $linkLink && $imageLink) // only return a result when all variables are set.
    {
        echo '<div id="product"><a href="'.$linkLink.'"><img src="'.$imageLink.'"/></a><br/><p>'.$productText.'</p></div>';
    }
}