我一直在试图编写一个Perl脚本来报废亚马逊并下载产品评论,但我一直无法这样做。 我一直在使用perl模块LWP :: Simple和HTML :: TreeBuilder :: XPath来实现这一点。如何从HTML中提取亚马逊评论?
对于HTML
<div id="revData-dpReviewsMostHelpfulAUI-R1GQHD9GMGBDXP" class="a-row a-spacing-small">
<span class="a-size-mini a-color-state a-text-bold">
Verified Purchase
</span>
<div class="a-section">
I bought this to replace an earlier model that got lost in transit when we moved. It is a real handy helper to have when making tortillas. Follow the recipe for flour tortillas in the little recipe book that comes with it. I make a few changes
</div>
</div>
</div>
</div>
我想提取产品的审查。对于这个我写道: -
use LWP::Simple;
#use HTML::TreeBuilder;
use HTML::TreeBuilder::XPath;
# Take the ASIN from the command line.
my $asin = shift @ARGV or die "Usage: perl get_reviews.pl <asin>\n";
# Assemble the URL from the passed ASIN.
my $url = "http://amazon.com/o/tg/detail/-/$asin/?vi=customer-reviews";
# Set up unescape-HTML rules. Quicker than URI::Escape.
my %unescape = ('"'=>'"', '&'=>'&', ' '=>' ');
my $unescape_re = join '|' => keys %unescape;
# Request the URL.
my $content = get($url);
die "Could not retrieve $url" unless $content;
my $tree = HTML::TreeBuilder::XPath->new_from_content($content);
my @data = $tree->findvalues('div[@class ="a-section"]');
foreach (@data)
{
print "$_\n";
}
但我没有得到任何输出。任何人都可以指出我的错误吗?
你应该坚持'uri_unescape'从HTML中删除字符实体。与全球正则表达式一起使用的散列可能会更快,但与从互联网上恢复HTML所花费的时间相比,可能会更快。而'uri_unescape'则更加简洁和自我记录。 – Borodin 2015-04-01 13:14:34
为什么刮亚马逊?你知道他们有一个[产品API](https://metacpan.org/release/Net-Amazon)? – 2015-04-08 16:04:55