php dom scraping - 抓取产品价格的最佳方法

发布于 2024-12-20 04:20:55 字数 637 浏览 3 评论 0原文

我正在使用 simpleHtmlDom 进行一些基本的屏幕抓取。不过,我在获取产品价格方面遇到了一些问题。有时我可以让它工作,有时却不能。另外,有时我会得到多个价格...例如,网站上有类似“通常为 100 美元...现在为 79.99 美元”的内容,有什么建议吗?目前,我正在使用这个:

$prices = array();
$prices[] = $html->find("[class*=price]", 0)->innertext;
$prices[] = $html->find("[class*=msrp]", 0)->innertext;
$prices[] = $html->find("[id*=price]", 0)->innertext;
$prices[] = $html->find("[id*=msrp]", 0)->innertext;
$prices[] = $html->find("[name*=price]", 0)->innertext;
$prices[] = $html->find("[name*=msrp]", 0)->innertext;

维多利亚的秘密(Victoria Secret)是我不知道如何获取价格的一个网站......价格看起来只是在随机 HTML 中浮动。

I'm using simpleHtmlDom to do some basic screen scraping. I'm having some problems with grabbing product prices though. Sometimes I can get it to work, sometimes I can't. Also, sometimes I'm getting multiple prices... say for example the website has something like "normally $100... now $79.99" Any suggestions out there? Currently, I'm using this:

$prices = array();
$prices[] = $html->find("[class*=price]", 0)->innertext;
$prices[] = $html->find("[class*=msrp]", 0)->innertext;
$prices[] = $html->find("[id*=price]", 0)->innertext;
$prices[] = $html->find("[id*=msrp]", 0)->innertext;
$prices[] = $html->find("[name*=price]", 0)->innertext;
$prices[] = $html->find("[name*=msrp]", 0)->innertext;

One website that I have no idea of how to grab the price from is Victoria Secret.... the price looks like it's just floating around in random HTML.

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

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

发布评论

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

评论(1

清风挽心 2024-12-27 04:20:55

首先,不要使用 simplehtmldom。使用内置的 dom 函数或基于它们的库。如果您想从页面中提取所有价格,您可以尝试如下操作:

$html = "<html><body>normally $100... now $79.99</body></html>";
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DomXpath($dom);

foreach($xpath->query('//text()[contains(.,"$")]') as $node){
    preg_match_all('/(\$[\d,.]+)/', $node->nodeValue, $m);
    print_r($m);
}

First of all, don't use simplehtmldom. Use the built in dom functions or a library that's based on them. If you want to extract all prices from a page you could try something like this:

$html = "<html><body>normally $100... now $79.99</body></html>";
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DomXpath($dom);

foreach($xpath->query('//text()[contains(.,"$")]') as $node){
    preg_match_all('/(\$[\d,.]+)/', $node->nodeValue, $m);
    print_r($m);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文