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

HACK#57 การเขียนโปรแกรม Google Web API ด้วย Python

การโปรแกรม Google Web API ด้วย Python นี้ค่อนข้างง่าย ลองดูไปตามที่สคริปต์และตัวอย่างนี้ได้สาธิตเอาไว้

การเขียนโปรแกรม Google Web API ด้วย Python เป็นสิ่งที่ง่ายเป็นอย่างมาก และต้องขอขอบคุณ PyGoogle wrapper module ของ Mark Pilgrim (http://diveintomark.org/projects/pygoogle/) ซึ่งได้เตรียมสิ่งต่างๆสำหรับรองรับ SOAP, XML และ request/response layer ช่วยให้คุณมีเวลาเหลือสำหรับศึกษาข้อมูลจากสิ่งเหล่านั้นมากขึ้น

การติดตั้ง PyGoogle

ให้ดาวน์โหลด PyGoogle มา แล้วทำตามคำแนะนำในไฟล์ readme.txt (http://diveintomark.org/projects/pygoogle/readme.txt) ซึ่งถ้าไม่ติดขัดประการใด การติดตั้งไม่น่าจะยุ่งยากไปกว่าเพียงแค่ใช้คำสั่ง

% python setup.py install

อีกวิธีหนึ่ง ถ้าคุณต้องการไปอย่างรวดเร็วและไม่ต้องการติดตั้ง PyGoogle หรือต้องการติดตั้งแต่ไม่ได้รับอนุญาต(Permission) ในการติดตั้งในระบบของคุณ คุณก็เพียงแต่บันทึกไฟล์ SOAP.py และ google.py ลงไปในไดเรกทอรีเดียวกันเท่านั้น

โค้ดตัวอย่าง

#!/usr/bin/python

# googly.py

# A typical Google Web API Python script using Mark Pilgrim's

# PyGoogle Google Web API wrapper

# [http://diveintomark.org/projects/pygoogle/]

# Usage: python googly.py

import sys, string, codecs

# Use the PyGoogle module

import google

# Grab the query from the command-line

if sys.argv[1:]:

query = sys.argv[1]

else:

sys.exit('Usage: python googly.py ')

# Your Google API developer's key

google.LICENSE_KEY = 'insert key here'

# Query Google

data = google.doGoogleSearch(query)

# Teach standard output to deal with utf-8 encoding in the results

sys.stdout = codecs.lookup('utf-8')[-1](sys.stdout)

# Output

for result in data.results:

print string.join( (result.title, result.URL, result.snippet), "\n"), "\n"

Running the Hack

สั่งสคริปต์ให้ทำงานที่ command line ด้วยคำสั่งดังนี้

% python googly.py "query words"

ผลลัพธ์

% python googly.py "learning python"

oreilly.com -- Online Catalog: <b>Learning</b>

<b>Python</b>

http://www.oreilly.com/catalog/lpython/

<b>Learning</b> <b>Python</b> is an

introduction to the increasingly popular interpreted programming

language that's portable, powerful, and remarkably easy to use in both

<b>...</b>

...

Book Review: <b>Learning</b> <b>Python</b>

http://www2.linuxjournal.com/lj-issues/issue66/3541.html

<b>...</b> Issue 66: Book Review: <b>Learning</b>

<b>Python</b> <b>...</b> Enter

<b>Learning</b> <b>Python</b>. My executive summary

is that this is the right book for me and probably for many others

as well. <b>...</b>

Hacking the Hack

Python มีความสามารถในการเชื่อมต่อการทำงานแบบ Interactive ร่วมกับตัวแปลภาษา (Interpreter) ได้อย่างดีเยี่ยม ซึ่งเป็นที่ที่ดีในการทดลองกับโมดูลต่างๆอย่างเช่น PyGoogle เป็นต้น ซึ่งจะทำให้การส่งค่า Google Web API อย่างรวดเร็ว รวมทั้งค้นหาตลอดโครงสร้างข้อมูล (Data Structure) ที่ได้รับมา

ข้างล่างนี้คือตัวอย่างแบบ Interactive ของ PyGoogle Session ซึ่งใช้ฟังก์ชัน doGoogleSearch, deGetCatchedPage และ doSpellingSuggestion ในการทำงาน

% python

Python 2.2 (#1, 07/14/02, 23:25:09)

[GCC Apple cpp-precomp 6.14] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>> import google

>>> google.LICENSE_KEY = 'insert key here'

>>> data = google.doGoogleSearch("Learning Python")

>>> dir(data.meta)

['_ _doc_ _', '_ _init_ _', '_ _module_ _', 'directoryCategories',

'documentFiltering', 'endIndex', 'estimateIsExact',

'estimatedTotalResultsCount', 'searchComments', 'searchQuery',

'searchTime', 'searchTips', 'startIndex']

>>> data.meta.estimatedTotalResultsCount

115000

>>> data.meta.directoryCategories

[{u'specialEncoding': '', u'fullViewableName': "Top/Business/Industries/

Publishing/Publishers/Nonfiction/Business/O'Reilly_and_Associates/

Technical_Books/Python"}]

>>> dir(data.results[5])

['URL', '_ _doc_ _', '_ _init_ _', '_ _module_ _', 'cachedSize',

'directoryCategory', 'directoryTitle', 'hostName',

'relatedInformationPresent', 'snippet', 'summary', 'title']

>>> data.results[0].title

'oreilly.com -- Online Catalog: <b>Learning</b> <b>Python'

>>> data.results[0].URL

'http://www.oreilly.com/catalog/lpython/'

>>> google.doGetCachedPage(data.results[0].URL)

'<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">\n

<BASE HREF="http://www.oreilly.com/catalog/lpython/"><table border=1

...

>>> google.doSpellingSuggestion('lurn piethon')

'learn python'

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