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

发布于 2025-01-03 06:06:20 字数 1450 浏览 4 评论 0原文

我正在抓紧我的头发试图让它与 php 一起工作。

问题:我只是想从网站上抓取产品并将它们显示为产品列表,而没有任何其他我可以在 css 中设置样式的内容。我想输出的是

>
gt ;

$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>";
}

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

I am pulling my hair trying to get this to work with php.

The problem: I am just trying to scrape products off a site and have them display as a list of products without anything else that I can style in css. What I'd like to output is <div id='product'><a href= $link ><img src= $image /></a><br/><p>$productText</p></div> as a list of products for a site (basically scrape them). This is a project I am trying for fun, here is the code:

$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>";
}

The truth is I have no clue how to get the right results I want. Let me know if this needs further explaining. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅沫记忆 2025-01-10 06:06:20

修复并测试:

<?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>';
    }
}

Fixed and tested:

<?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>';
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文