การเขียนโปรแกรม 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[
query = sys.argv[
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')[-
# 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/354
<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 (#
[GCC Apple cpp-precomp 6.
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
>>> 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-
<BASE HREF="http://www.oreilly.com/catalog/lpython/"><table border=
...
>>> google.doSpellingSuggestion('lurn piethon')
'learn python'

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