
905e92fe542a15129b638b24afba5fc8.ppt
- Количество слайдов: 9
Text Processing Google APi Basi di Dati Multimediali - Giugno 2005 Marco Ernandes: ernandes@dii. unisi. it
Introduzione v Google Web APIs è una servizio software (beta) che permette a un qualsiasi programma di collegarsi direttamente al motore di ricerca Google. v E’ destinato a tutti gli sviluppatori di software che vogliono usare Google come risorsa. v L’interfaccia tra un programma e Google avviene attraverso gli standard SOAP e WSDL. v Sono supportati i seguenti ambienti di programmazione: Java, Perl, Visual Studio. NET v Download delle API e della documentazione da: http: //www. google. com/apis/
Istallazione 1. Download the developer's kit: • The Google Web APIs developer's kit provides documentation and example code for using the Google Web APIs service. The download includes Java and. NET programming examples and a WSDL file for writing programs on any platform that supports web services. 2. Create a Google Account • To access the Google Web APIs service, you must create a Google Account and obtain a license key. Your Google Account and license key entitle you to 1, 000 automated queries per day. 3. Write your program using your license key • Your program must include your license key with each query you submit to the Google Web APIs service. Check out our Getting Help page or read the FAQs for more information.
Istallazione v. Il file googleapi. zip (developer’s kit) contiene: n n n n googleapi. jar – Libreria Java Google Web APIs service. Google. APIDemo. java – Programma esempio che usa googleapi. jar Example. NET – programmi esempio in VBasic e C# che usano Google Web APIs_Reference. html - Documentazione generale delle API. Javadoc - Documentazione della libreria JAVA. Google. Search. wsdl – Descrizione WSDL delle Google SOAP API. soap-samples/ - Esempi di messaggi SOAP al servizio.
WSDL e SOAP v WSDL: n n n Web Services Description Language The standard format for describing a web service. Expressed in XML, a WSDL definition describes how to access a web service and what operations it will perform. v SOAP: n n Simple Object Access Protocol. È un protocollo per comunicazioni tra applicazioni. Descrive un formato per spedire messaggi. Basato su XML, è language e platform-independent. <soap 11: Envelope xmlns="urn: Google. Search“ xmlns: soap 11="http: //. . . ”> <soap 11: Body> <do. Google. Search> <key>e 7 gx. V 1…</key> <q>telephone inventor </q> <start>0</start> <max. Results>10</max. Results> </do. Google. Search> </soap 11: Body> </soap 11: Envelope>
Google. APIDemo. java public class Google. APIDemo { public static void main(String[] args) { // Parametri passati come argomenti String client. Key = args[0]; String directive = args[1]; String directive. Arg = args[2]; // Create l’object Google Search Google. Search s = new Google. Search(); // set our authorization key s. set. Key(client. Key); // argomento “directive” stabilisce se si // si vuol fare una search o una cached query try { // se è stata selezionata una “search” query if (directive. equals. Ignore. Case("search")) { s. set. Query. String(directive. Arg); Google. Search. Result r = s. do. Search(); // stampa i risultati del search System. out. println("Google Search Results: "); System. out. println(r. to. String()); } // se è stata selezionata una “cached” query else if (directive. equals. Ignore. Case("cached")) { byte [] cb = s. do. Get. Cached. Page(directive. Arg); // stampa il documento cached System. out. println("Cached page: "); String cached. String = new String(cb); System. out. println(cached. String); } // catch di una eccezione nella ricerca Google } catch (Google. Search. Fault f) { System. out. println("The call to the Google Web APIs failed: "); System. out. println(f. to. String()); } } }
RICERCA con la classe Google. Search // specifica la chiave per l’autorizzazione void set. Key(String key) // specifica la lingua // es: s. set. Language. Restricts("lang_it"); void set. Language. Restricts(String lr) // setta il numero max. di risultati: ogni // query ritorna al massimo 10 documenti // quindi un set. Max. Results(100) mette in // moto 10 query diverse !!! void set. Max. Results(int max. Results) // settano il proxy (se necessario) void set. Proxy. Host void set. Proxy. Password … // stabilisce la stringa di query da // passare a Google void set. Query. String(String query) // stabilisce la stringa di restrizione // es: “site: www. repubblica. it” oppure // es: “intitle: crosswords” void set. Restrict(String restrict) // setta la ricerca sicura // (evita pornografia) void set. Safe. Search(boolean safe) // stabilisce da che punto far // cominciare i risultati void set. Start. Result(int start) // invoca una ricerca che ritorna un // oggetto Google. Search. Result do. Search() // scarica dalla cache di Google il // documento con indirizzo “url” byte[] do. Get. Cached. Page(String url)
Managing dei risultati v Google. Search. Result: classe v Google. Search. Result. Element: che contiene tutta classe che contiene l’info. di l’informazione presente nella una singola risposta. pagina html di risposta. // numero stimato di risultati ottenuti int get. Estimated. Total. Results. Count() // ritorna l’array dei Result. Elements Google. Search. Result. Element[] get. Result. Elements() // dimensione (in Kb) del file in cache String get. Cached. Size() // nome directory Google in cui si trova String get. Directory. Title() // ritorna la snippet del documento String get. Snippet() // ritorna il tempo di ricerca in secondi double get. Search. Time() // ritorna il titolo del documento String get. Title() // fornisce suggerimenti per la ricerca String get. Search. Tips() // ritorna la URL del documento String get. URL()
Come “sgraffignare” Google v Usare le Google API è il modo corretto di sfruttare il motore di ricerca. Ma 1000 queries sono pochine … v Se da un programma si fa una richiesta HTTP a Google mettendo la query nell’indirizzo la richiesta viene rifiutata! (es: www. google. it/search? q=telephone+inventor) v Per aggirare il problema il trucco è semplice: far finta di essere un browser! URL web. Page = new URL(url. String); // effettua il fake di Mozilla per ingannare il motore di ricerca Http. URLConnection url. Conn = (Http. URLConnection) web. Page. open. Connection(); url. Conn. set. Request. Property("User-Agent", "Mozilla/4. 0 (compatible; MSIE 6. 0; Windows NT 5. 1; . NET CLR 1. 1. 4322)"); Input. Stream input. Stream = url. Conn. get. Input. Stream(); NON FATELO!!!! Buffered. Reader in = new Buffered. Reader(new Input. Stream. Reader(input. Stream)); ALTRIMENTI GOOGLE String input. Line; METTE IL VOSTRO IP // leggi tutto il file while ( (input. Line = in. read. Line()) != null) retrieved += input. Line; IN UNA BLACKLIST in. close();
905e92fe542a15129b638b24afba5fc8.ppt