วันพุธที่ 8 สิงหาคม พ.ศ. 2550

HACK#51 การวนลูปเพื่อเพิ่มจำนวนผลลัพธ์

ถ้าหากคุณต้องการผลลัพธ์มากกว่า 10 รายการขึ้นไป คุณจำเป็นต้องใช้การวนลูปเข้าช่วย

ดังที่ทราบว่า Google Web API จะคืนค่าผลลัพธ์ให้เพียง 10 รายการต่อการค้นหาหนึ่งครั้งเท่านั้น ซึ่งอาจจะเพียงพอสำหรับคำถามบางคำถาม แต่สำหรับบางคำถามแล้ว ผลลัพธ์ 10 รายการอาจจะไม่เพียงพอก็เป็นได้ อย่างไรก็ตาม หากคุณต้องการผลลัพธ์ที่มากกว่านั้น คุณสามารถใช้การวนลูปเข้าช่วย โดยการวนลูปเพื่อส่ง query (ดูสคริปต์) เข้าไปสอบถามเพื่อให้ได้ผลลัพธ์ 10 รายการถัดไป ซึ่งก็คือรายการที่ 11 ถึงรายการที่ 20 และรอบถัดไปก็คือรายการที่ 21 ถึงรายการที่ 30 เป็นเช่นนี้ไปเรื่อยๆ ซึ่งจำนวนผลลัพธ์ที่ได้ ขึ้นอยู่กับจำนวนลูปที่คุณกำหนด

หัวข้อนี้เป็นการนำสิ่งที่ได้แนะนำเอาไว้ใน “การเขียนโปรแกรม Google Web API ด้วยภาษา Perl” [Hack #50] มาใช้ ซึ่งการเพิ่มจำนวนผลลัพธ์จากการค้นหาให้ได้มากกว่า 10 รายการนั้น ไม่มีสิ่งใดต้องโปรแกรมเพิ่ม นอกจากการวนลูปเท่านั้น ซึ่งสคริปต์ที่เห็นข้างล่างนี้เขียนด้วยภาษา Perl โดยส่วนที่เป็นตัวหนา (bold) เป็นส่วนที่เพิ่มขึ้นมาจาก 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 10 results at a time

my $loops = 3; # 3 loops x 10 results per loop = top 30 results

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 \n";

# 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-1)*10; $offset += 10) {

# Query Google

my $results = $google_search ->

doGoogleSearch(

$google_key, $query, $offset, 10, "false", "", "false",

"", "latin1", "latin1"

);

# 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

print

join "\n",

++$number,

$result->{title} || "no title",

$result->{URL},

$result->{snippet} || 'no snippet',

"\n";

}

}

สังเกตว่าสคริปต์ข้างบนจะแจ้ง Google ถึงจำนวนรอบที่ต้องการโดยเพียงการผ่านค่าตัวแปร $offset เท่านั้น ซึ่ง $offset += 10 หมายถึงการเพิ่มผลลัพธ์ครั้งละ 10 รายการต่อรอบนั่นเอง

Running the Script

รันสคริปต์นี้ที่ command line โดยการใส่คำที่ต้องการค้นหาลงไปแทน query

$perl looply.pl “query”


ผลลัพธ์

% perl looply.pl

Usage: perl looply.pl

% perl looply.pl "learning perl"

1

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 2001 0-596-00132-0, Order Number ...

...

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 ความคิดเห็น: