EL CICLO PRÁCTICO DE UN MOTOR DE CUATRO TIEMPOS.pptx
Screen scraping
1. Universidad de Oviedo Programa de extensión universitaria
CLOUD COMPUTING.
DESARROLLO DE APLICACIONES Y
MINERÍA WEB
Miguel Fernández Fernández
miguelff@innova.uniovi.es
7. Porqué screen scraping
La Web es fundamentalmente para
humanos (HTML)
Pero no está diseñada para ser procesada
por máquinas (XML, JSON, CSV...)
8. Porqué screen scraping
La Web es fundamentalmente para
humanos (HTML)
Pero no está diseñada para ser procesada
por máquinas (XML, JSON, CSV...)
<horario>
<viaje>
<salida format="hh:mm">06:39</salida>
<llegada format="hh:mm">07:15</llegada>
<duracion format="minutes">36</duracion>
<linea>C1</linea>
</viaje>
</horario>
10. Porqué screen scraping
No siempre disponemos de una API
Necesitamos simular el comportamiento humano
11. Porqué screen scraping
No siempre disponemos de una API
Necesitamos simular el comportamiento humano
Interpretar
HTML
12. Porqué screen scraping
No siempre disponemos de una API
Necesitamos simular el comportamiento humano
Realizar
Interpretar
interacciones
HTML
(Navegar)
13. Porqué screen scraping
No siempre disponemos de una API
Necesitamos simular el comportamiento humano
Realizar
Interpretar
interacciones Ser un Ninja
HTML
(Navegar)
Evitar DoS
14. Selección de las herramientas
¿Con qué lenguaje vamos a trabajar?
Java .NET Ruby Python
net/http
URL System.Net. urllib
java.net.URL open-uri
fetching HTTPWebRequest urllib2
rest-open-uri
DOM javax.swing.text.html HTree / ReXML
parsing TagSoup
/
HTMLAgilityPack HPricot BeautifulSoup
NekoHTML RubyfulSoup
transversing
System.Text.
Regexp java.util.regexp RegularExpressions
Regexp re
--- Librerías de terceras partes. No forman parte de la API del lenguaje.
15. Selección de las herramientas
¿Con qué lenguaje vamos a trabajar?
Duck typing + Reflexión = Syntactic Sugar
16. Selección de las herramientas
¿Con qué lenguaje vamos a trabajar?
Lenguajes dinámicos facilitan la codificación
Duck typing + Reflexión = Syntactic Sugar
17. Java
import javax.swing.text.html.*;
import javax.swing.text.Element;
import javax.swing.text.ElementIterator;
import java.net.URL;
import java.io.InputStreamReader;
import java.io.Reader;
public class HTMLParser
{
public static void main( String[] argv ) throws Exception
{
URL url = new URL( "http://java.sun.com" );
HTMLEditorKit kit = new HTMLEditorKit();
HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
Reader HTMLReader = new InputStreamReader(url.openConnection().getInputStream());
kit.read(HTMLReader, doc, 0);
ElementIterator it = new ElementIterator(doc);
Element elem;
while( elem = it.next() != null )
{
if( elem.getName().equals( "img") )
{
String s = (String) elem.getAttributes().getAttribute(HTML.Attribute.SRC);
if( s != null )
System.out.println (s );
}
}
System.exit(0);
}
}
19. Selección de las herramientas Ruby
rest-open-uri
Nos permitirá hacer peticiones a
HTree + REXML URLs y extraer su contenido
Hpricot
extiende open-uri para soportar
RubyfulSoup más verbos
WWW:Mechanize
20. Selección de las herramientas Ruby
HTree crea un árbol de
rest-open-uri objetos a partir de código
HTML
HTree + REXML
Hpricot
HTree#to_rexml
Convierte el árbol a un árbol
RubyfulSoup REXML
WWW:Mechanize REXML puede navegarse con
XPath 2.0
22. Selección de las herramientas Ruby
Scanner implementado en C
rest-open-uri
(Muy rápido)
HTree + REXML
Genera un DOM con su
Hpricot propio sistema de navegación
RubyfulSoup como Jquery(selectores CSS y XPath*)
WWW:Mechanize Funcionalidad equivalente a
Htree + REXML
http://hpricot.com/
24. Selección de las herramientas Ruby
Scanner implementado en C
rest-open-uri
(Muy rápido)
HTree + REXML
Genera un DOM con su
Hpricot propio sistema de navegación
RubyfulSoup co mo Jquery
(selectores CSS y XPath*)
WWW:Mechanize Funcionalidad equivalente a
Htree + REXML
http://hpricot.com/
25. Selección de las herramientas Ruby
Scanner implementado en C
rest-open-uri
(Muy rápido)
Hpricot
Genera un DOM con su
RubyfulSoup propio sistema de navegación
WWW:Mechanize co mo Jquery
(selectores CSS y XPath*)
Funcionalidad equivalente a
Htree + REXML
http://hpricot.com/
26. Selección de las herramientas Ruby
rest-open-uri
Ofrece la misma funcionalidad que
Hpricot HTree + REXML
RubyfulSoup
WWW:Mechanize
28. Selección de las herramientas Ruby
rest-open-uri
Ofrece la misma funcionalidad que
Hpricot HTree + REXML
RubyfulSoup
WWW:Mechanize
29. Selección de las herramientas Ruby
rest-open-uri
Ofrece la misma funcionalidad que
Hpricot HTree + REXML
RubyfulSoup Menor rendimiento que Hpricot
WWW:Mechanize
30. Selección de las herramientas Ruby
rest-open-uri
Ofrece la misma funcionalidad que
Hpricot HTree + REXML
RubyfulSoup Menor rendimiento que Hpricot
WWW:Mechanize No se admiten selectores CSS
31. Selección de las herramientas Ruby
Ofrece la misma funcionalidad que
rest-open-uri HTree + REXML
Hpricot Menor rendimiento que Hpricot
WWW:Mechanize No se admiten selectores CSS
32. Selección de las herramientas Ruby
Permite realizar interacciones
rest-open-uri
Rellenar y enviar formularios
Hpricot Seguir enlaces
WWW:Mechanize Consigue alcanzar documentos en
La Web Profunda
41. require 'rubygems'
require 'mechanize'
class TuentiAPI
def initialize(login,password)
@login=login
@password=password
end
def inicio()
agent = Mechanize.new
agent.set_proxy("localhost",8080)
#decimos que somos firefox modificando la cabecera user agent
agent.user_agent_alias='Mac FireFox'
login_page = agent.get('http://m.tuenti.com/?m=login')
#cogemos el formulario de login
login_form = login_page.forms.first
#y rellenamos los campos usuario y contraseña
login_form.fields.select{|f| f.name=="tuentiemail"}.first.value=@login
login_form.fields.select{|f| f.name=="password"}.first.value=@password
pagina_de_inicio=agent.submit(login_form)
end
end
pagina_de_inicio=TuentiAPI.new("miguelfernandezfernandez@gmail.com","xxxxxx").inicio()
45. Paso 2: Obtener las fotos
class TuentiAPI
...
def fotos_nuevas()
tree=Hpricot(inicio().content)
fotos = tree / "//a//img[@alt=Foto]"
fotos.map!{|foto| foto.attributes["src"]}
Set.new(fotos).to_a
end
private
def inicio()
...
end
end
48. Paso 3: Establecer el estado
class TuentiAPI
...
def actualizar_estado(msg)
form_actualizacion=inicio.forms.first
form_actualizacion.fields.select{|f| f.name=="status"}.first.value=msg
@agent.submit(form_actualizacion)
end
end
50. Tor: navegando de forma
anónima
Red de encadenamiento de proxies
N peticiones salen de M servidores
Garantiza el anonimato a nivel de IP
https://www.torproject.org/vidalia/
52. Universidad de Oviedo Programa de extensión universitaria
CLOUD COMPUTING.
DESARROLLO DE APLICACIONES Y
MINERÍA WEB
Miguel Fernández Fernández
miguelff@innova.uniovi.es