Skip to content

Instantly share code, notes, and snippets.

@gaspardzul
Created August 8, 2015 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaspardzul/ff2ffcc33243549899c3 to your computer and use it in GitHub Desktop.
Save gaspardzul/ff2ffcc33243549899c3 to your computer and use it in GitHub Desktop.
# coding=utf-8
__author__ = 'gaspar dzul'
from pyquery import PyQuery as pq
# ejemplo consulta valor del peso mexicano
print("---- ejemplo de consulta de divisa ----")
jquery = pq(url="http://www.cambiodolar.mx/")
valor_dolar_pesos_mx = jquery('div.valor').text()
fecha_consulta = jquery('p.day').text()
print(valor_dolar_pesos_mx)
print (fecha_consulta)
#consulta de imagenes de un sitio en internet
print("---- ejemplo de consulta de imagenes de pythonizame ----")
print("consultando imagenes de pythonizame")
jquery = pq(url="https://instagram.com/pythonizame")
elementos = [elemento.attr('src') for elemento in jquery.items('img')]
for img in elementos:
print(img)
#removiendo y agregando clases a elementos html
print("---- removiendo y agregando clases a elementos html----")
jquery = pq("<div id='mi_div' class='myclass'>mi elemento</div>")
print jquery
id_elemento = jquery.attr('id');
jquery.remove_attr('id')
print("Removiendo el atributo id de %s" % id_elemento)
print ("resultado: %s" % jquery)
print("Removiendo el atributo class de %s" % id_elemento)
jquery.remove_attr('class')
print ("resultado sin id ni class: %s" % jquery)
print("--agregando nuevos valores---")
jquery.add_class('mynuevaclase')
jquery.attr('id','mnuevoid')
print ("elementos con nuevos valores:%s " % jquery)
@gaspardzul
Copy link
Author

Este ejemplo muestra como utilizar pyquery para web scraping

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment