ดังที่ทราบว่า Google Web API จะคืนค่าผลลัพธ์ให้เพียง
หัวข้อนี้เป็นการนำสิ่งที่ได้แนะนำเอาไว้ใน “การเขียนโปรแกรม Google Web API ด้วยภาษา Perl” [Hack #50] มาใช้ ซึ่งการเพิ่มจำนวนผลลัพธ์จากการค้นหาให้ได้มากกว่า
โค้ดตัวอย่าง
#!/usr/local/bin/perl
# looply.pl
# A typical Google Web API Perl script
# Usage: perl looply.pl
# Your Google API developer's key
my $google_key='insert key here';
# Location of the GoogleSearch WSDL file
my $google_wdsl = "./GoogleSearch.wsdl";
# Number of times to loop, retrieving
my $loops = 3; # 3 loops x
use strict;
# Use the SOAP::Lite Perl module
use SOAP::Lite;
# Take the query from the command-line
my $query = shift @ARGV or die "Usage: perl looply.pl
# Create a new SOAP::Lite instance, feeding it GoogleSearch.wsdl
my $google_search = SOAP::Lite->service("file:$google_wdsl");
# Keep track of result number
my $number = 0;
for (my $offset = 0; $offset <= ($loops-
# Query Google
my $results = $google_search ->
doGoogleSearch(
$google_key, $query, $offset,
"", "latin
);
# No sense continuing unless there are more results
last unless @{$results->{resultElements}};
# Loop through the results
foreach my $result (@{$results->{'resultElements'}}) {
# Print out the main bits of each result
join "\n",
++$number,
$result->{title} || "no title",
$result->{URL},
$result->{snippet} || 'no snippet',
"\n";
}
}
สังเกตว่าสคริปต์ข้างบนจะแจ้ง Google ถึงจำนวนรอบที่ต้องการโดยเพียงการผ่านค่าตัวแปร $offset เท่านั้น ซึ่ง $offset +=
Running the Script
รันสคริปต์นี้ที่ command line โดยการใส่คำที่ต้องการค้นหาลงไปแทน query
$perl looply.pl “query”
% perl looply.pl
Usage: perl looply.pl
% perl looply.pl "learning perl"
oreilly.com -- Online Catalog: Learning Perl, 3rd Edition
http://www.oreilly.com/catalog/lperl3/
... Learning Perl, 3rd Edition Making Easy Things
Easy and Hard Things Possible By Randal
L. Schwartz, Tom Phoenix
3rd Edition July 200
...
29
Intro to Perl for CGI
http://hotwired.lycos.com/webmonkey/98/47/index2a.html
... Some people feel that the benefits of learning
Perl scripting are few.
But ... part. That's right.
Learning Perl is just like being a cop. ...
30
WebDeveloper.com ฎ: Where Web Developers and Designers Learn How ...
http://www.webdeveloper.com/reviews/book6.html
... Registration CreditCard Processing Compare Prices.
Learning Perl. Learning
Perl, 2nd Edition.
Publisher: O'Reilly Author: Randal Schwartz ...
ดูเพิ่มเติม
การเขียนโปรแกรม Google Web API ด้วย Perl [Hack #50]

0 ความคิดเห็น:
แสดงความคิดเห็น