从站点获取节点值,并仅重新输出可设置样式的选定节点标签
我正在抓紧我的头发试图让它与 php 一起工作。
问题:我只是想从网站上抓取产品并将它们显示为产品列表,而没有任何其他我可以在 css 中设置样式的内容。我想输出的是 作为网站的产品列表(基本上是抓取它们)。这是我为了好玩而尝试的一个项目,代码如下:
$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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
修复并测试:
Fixed and tested: