Tải bản đầy đủ - 0 (trang)
Hack 70. Program Yahoo! with Python

Hack 70. Program Yahoo! with Python

Tải bản đầy đủ - 0trang

Oncesetup.pyisreadytogoforyoursystem,installitfromthe

commandpromptwiththesetwocommands:

pythonsetup.pybuild

pythonsetup.pyinstall



Ifeverythingworksasitshould,thepYsearchlibrarywillnow

beavailabletoyourPythonscripts.



4.5.1.TheCode

ThissimplePythonscriptusesthepYsearchlibrarytoreturn

Yahoo!WebSearchresponses.Savethiscodetoafilecalled

yahoo_search.pyandbesuretoaddyourownunique

applicationID:

#!/usr/bin/python

#yahoo_search.py

#AquickYahoo!WebSearchscriptusingYahoo!'s

#pYsearchlibraryavailbleintheY!WSSDK

#[http://developer.yahoo.net/download/]

#Usage:pythonyahoo_search.py

importsys,string,codecs

#UsethepYsearchfunctions

fromyahoo.searchimportwebservices

#Grabthequeryfromthecommandline

ifsys.argv[1:]:

query=sys.argv[1]

else:



sys.exit('Usage:pythonyahoo_search.py')

#IncludeyouruniqueapplicationID

appID='insertyourappID'

#QueryYahoo!

search=webservices.create_search('web',appID)

search.language="en"

search.results=10

search.start=1

search.query=query

#Parsetheresults

try:

results=search.parse_results()

exceptException,err:



print"Gotanerror:",err



sys.exit(1)

#Tellstandardoutputtohandleutf-8encoding

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

#Startcounter

count=search.start



#Printouttheresults

forresultinresults:



print"%s.%s\n%s\n%s\n\n"%(count,result.Title,resu

result.Url)



count+=1



ThekeytousingthepYsearchlibraryisimportingthewebservices

moduleatthetopofthescript.Fromthere,thescriptcallsthe



create_searchfunction,setssomeparameters,anduses

parse_resultstogettheentireYahoo!response.Andthelastprint



commandformatstheresponseanddisplaysitfortheuser.

Thewebsearchtypeisspecifiedinthecreate_searchfunction,but

keepinmindthatyoucoulduseanyofYahoo!'ssearchservices

here.



4.5.2.RunningtheHack

Runthescriptonthecommandline:

pythonyahoo_search.pyinsertword



And,asusual,enclosemultiplewordsinquotationmarks:

pythonyahoo_search.py"insertmultiwordphrase"



Figure4-5showstheresultsfor"learningPython".

Pythonisknownasalanguagethatcanassemblecomplex

applicationsquickly;ifyou'replanningtointegrateYahoo!data

withaPythonapplication,it'sthatmuchfasterbecausemostof

thehardworkisdoneforyoualreadyinthepYsearchlibrary.



Figure4-5.Yahoo!Searchresultsfor"learning

Python"



Hack71.ProgramYahoo!withVBScript



BuildYahoo!searchesintoWindowsprogramsorASP

pageswithVBScript.

VBScriptisageneral-purposescriptinglanguageforWindows,

anditgetsitsnamefromVisualBasic,itsbigbrotherofa

programminglanguage.Withafewtweakshereandthere,the

codeinthishackcouldaddYahoo!searchingtoOffice

applicationsoranASP-poweredwebpage.Thishackiswritten

torunasaMicrosoftWindowsScriptanditprovidesjustthe

basicsforbuildingaYahoo!Searchqueryandpresentingthe

results.

MicrosoftWindowsScriptisbuiltintothefabricoftheWindows

operatingsystemandisusedprimarilybysystem

administratorstoautomatesometasksinvolvedwithyou

guesseditsystemadministration.ButMicrosoftWindowsScripts

canalsobeusedtoautomateapplicationsandsenddataback

andforthbetweenprograms.



4.6.1.WhatYouNeed

IfyourWindowsinstallationisup-to-date,youshouldn'tneed

toinstallanythingextratorunthishack.Butifit'sbeenawhile

sinceyou'verunWindowsUpdate,youmightwanttograbthe

latestversionofMicrosoftWindowsScriptat

http://www.microsoft.com/scripting.Fromthatpage,click

DownloadsandchooseMicrosoftWindowsScript5.6orlaterfor

yourversionofWindows.

ThishackalsoreliesontheMicrosoftXMLParsertosortthe



resultsfromYahoo!.Yoursystemshouldalreadyhaveaversion

oftheparserinstalled,butifyourunintotrouble,youcan

alwaysdownloadthelatestversionat

http://msdn.microsoft.com/xml.Onceyou'rethere,clickXML

DownloadsandchoosethelatestXMLCoreServicespackage

youcanfind.



Asalways,besuretograbauniqueYahoo!applicationIDforthisscript

athttp://api.search.yahoo.com/webservices/register_application.



4.6.2.TheCode

Likeanyotherscript,thecodeissimplyplaintextinastandard

textfile.YoucouldevenuseNotepadtosavethefollowingcode

toafilecalledyahoo_search.vbs:

'yahoo_search.vbs

'Acceptsasearchtermandshowsthetopresults.

'Usage:cscriptyahoo_search.vbs//I

'

'YoucancreateanAppID,andreadthefulldocumentation

'forYahoo!WebServicesathttp://developer.yahoo.net/

'SetyouruniqueYahoo!ApplicationID

ConstAPP_ID="insertyourappID"

'Grabtheincomingsearchqueryoraskforone

IfWScript.Arguments.Length=0Then



strQuery=InputBox("EnterasearchTerm")

Else



strQuery=WScript.Arguments(0)

EndIf



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Hack 70. Program Yahoo! with Python

Tải bản đầy đủ ngay(0 tr)

×