在空闲时间,我一直在尝试通过编写一个脚本来提高我的perl能力,该脚本使用LWP::Simple来轮询特定网站的产品页面以检查产品的价格(我在某种程度上是perl新手)。此脚本还保留了该项目的最新价格的一个非常简单的积压(因为价格经常变化)。
我想知道是否有任何方法可以进一步自动化脚本,这样我就不必显式地将页面的URL添加到初始哈希中(例如,保留关键字的数组并执行搜索查询amazon来查找页面或价格?)。我有没有办法做到这一点,而不是只需要复制亚马逊的搜索URL并解析我的关键字?(我知道使用正则表达式处理HTML通常是不好的形式,我只是使用它,因为我只需要一小段数据)。
#!usr/bin/perl
use strict;
use warnings;
use LWP::Simple;
my %oldPrice;
my %nameURL = (
"Archer Season 1" => "http://www.amazon.com/Archer-Season-H-Jon-Benjamin/dp/B00475B0G2/ref=sr_1_1?ie=UTF8&qid=1297282236&sr=8-1",
"Code Complete" => "http://www.amazon.com/Code-Complete-Practical-Handbook-Construction/dp/0735619670/ref=sr_1_1?ie=UTF8&qid=1296841986&sr=8-1",
"Intermediate Perl" => "http://www.amazon.com/Intermediate-Perl-Randal-L-Schwartz/dp/0596102062/ref=sr_1_1?s=books&ie=UTF8&qid=1297283720&sr=1-1",
"Inglorious Basterds (2-Disc)" => "http://www.amazon.com/Inglourious-Basterds-Two-Disc-Special-Brad/dp/B002T9H2LK/ref=sr_1_3?ie=UTF8&qid=1297283816&sr=8-3"
);
if (-e "backlog.txt"){
open (LOG, "backlog.txt");
while(){
chomp;
my @temp = split(/:\s/);
$oldPrice{$temp[0]} = $temp[1];
}
close(LOG);
}
print "\nChecking Daily Amazon Prices:\n";
open(LOG, ">backlog.txt");
foreach my $key (sort keys %nameURL){
my $content = get $nameURL{$key} or die;
$content =~ m{\s*\$(\d+.\d+)} || die;
if (exists $oldPrice{$key} && $oldPrice{$key} != $1){
print "$key: \$$1 (Was $oldPrice{$key})\n";
}
else{
print "\n$key: $1\n";
}
print LOG "$key: $1\n";
}
close(LOG);发布于 2011-02-19 01:37:36
我做了一个简单的脚本来演示亚马逊搜索自动化。所有部门的搜索url已更改为转义搜索词。剩下的代码是使用HTML::TreeBuilder进行简单的解析。有问题的超文本标记语言的结构可以很容易地用dump方法检查(参见注释掉的行)。
use strict; use warnings;
use LWP::Simple;
use URI::Escape;
use HTML::TreeBuilder;
use Try::Tiny;
my $look_for = "Archer Season 1";
my $contents
= get "http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords="
. uri_escape($look_for);
my $html = HTML::TreeBuilder->new_from_content($contents);
for my $item ($html->look_down(id => qr/result_\d+/)) {
# $item->dump; # find out structure of HTML
my $title = try { $item->look_down(class => 'productTitle')->as_trimmed_text };
my $price = try { $item->look_down(class => 'newPrice')->find('span')->as_text };
print "$title\n$price\n\n";
}
$html->delete;发布于 2011-02-19 01:44:39
是的,设计是可以改进的。最好是删除所有内容,并从现有的功能齐全的web抓取应用程序或框架重新开始,但既然你想学习:
其他堆积者,如果你想用每条建议的基本原理来修改我的帖子,请继续编辑它。
https://stackoverflow.com/questions/5044106
复制相似问题