Google I’m Feeling Lucky: fazer uma requisição para http://www.google.com.br, pesquisando pelo nome da linguagem(java,ruby,…). Exibir como retorno o endereço, título e descrição do primeiro resultado encontrado.
Ruby (meu):
require 'open-uri'
open('http://www.google.com.br/search?q=ruby').read =~ %r{class=l href="(.*?)">(.*?)</a.*?j><font.*?>(.*?)<br><}m
puts ($2 + "\n" + $1 + "\n" + $3 + "\n").gsub(/<.*?\>/, '')
Java (do Giuliano, bastante melhorada depois pelo Urubatan):
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
public class HTTPSample {
public static void main(String[] args) throws Exception {
URLConnection con = new URL("http://www.google.com/search?q=java").openConnection();
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
Scanner sc = new Scanner(con.getInputStream());
System.out.println(sc.findWithinHorizon("<a>.*?</a>", 0).replaceAll("<a>(.*?)</a>", "$2=$1").replaceAll("", ""));
}
}
PHP (do Pilger, com ajuste do Fernando):
<?php
$html = implode('', file('http://www.google.com.br/search?q=php'));
preg_match_all("|class=l href=\"(.*?)\">(.*?)</a.*?j><font.*?>(.*?)<br><|", $html, $out, PREG_SET_ORDER);
echo $out[0][1]." ".$out[0][2]." ".$out[0][3];
?>
Python (do Salviato):
import urllib
import re
class AppURLopener(urllib.FancyURLopener):
version = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1"
urllib._urlopener = AppURLopener()
html = urllib.urlopen('http://www.google.com.br/search?q=python').read()
out = re.search('class=l href="(.*?)">(.*?)</a.*?j><font.*?>(.*?)<br><', html, re.DOTALL)
print re.sub('<.*?>', '', (out.group(1) + '\n' + out.group(2) + '\n' + out.group(3)))
TCl/TK (do Salviato):
package require http
http::config -useragent {Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1}
set html [http::data [http::geturl {http://www.google.com.br/search?q=tcl}]]
set out [regexp -inline {class=l href="(.*?)">(.*?)(.*?)< } $html]
puts [regsub -all {<.*?>} [concat "[lindex $out 1]\n[lindex $out 2]\n[lindex $out 3]"] "" ]
VB (do Giuliano):
Dim http As New WinHttp.WinHttpRequest
Dim re As New RegExp, matcher As Match
Dim texto As String
http.Open "GET", "http://www.google.com.br/search?q=java", False
http.SetRequestHeader "User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7"
http.Send
texto = http.ResponseText
re.Pattern = "class=l href=""(.*?)"">(.*?)</a.*?j><font.*?>(.*?)<br><"
re.Global = True
re.IgnoreCase = True
re.MultiLine = True
For Each matcher In re.Execute(texto)
Dim strSaida: strSaida = ""
strSaida = matcher.SubMatches(0) + vbCrLf + matcher.SubMatches(1) + vbCrLf + matcher.SubMatches(2)
MsgBox strSaida
Exit For
Next
Coldfusion (do Kenji)
<cfhttp url="http://www.google.com.br/search?q=coldfusion+mx">
<cfset tmp = REFindNoCase('class=l href="(.*?)">(.*?)</a.*?j><font.*?>(.*?)<br><', cfhttp.filecontent,1,true)>
<cfoutput>#mid(cfhttp.filecontent,tmp.pos[2],tmp.len[2])# #mid(cfhttp.filecontent,tmp.pos[3],tmp.len[3])# #mid(cfhttp.filecontent,tmp.pos[4],tmp.len[4])#</cfoutput>
Popularity: 53% [?]







