PyQuery

Adam Gall

  • gall.adam@gmail.com

Why PyQuery?

  • Familiar: leverage existing knowledge of jQuery
  • Flexible: interact with the HTML DOM natively
  • Not Regex

In [ ]:
from pyquery import PyQuery as pq

In [ ]:
import requests

source = requests.get('https://news.ycombinator.com/').content

In [ ]:
# D is for Dollar $ign

d = pq(source)
type(d)

In [ ]:
print(d.text())

In [ ]:
# Lets find the news
d('a')

In [ ]:
d('a').filter('.storylink')  # can even define custom functions here

In [ ]:
storyLinks = d('a.storylink')

type(storyLinks[0])

In [ ]:
# Every. Single. Time.
storyLinks = [pq(x) for x in storyLinks]

type(storyLinks[0])

In [ ]:
for storyLink in storyLinks:
    print(storyLink.text())

In [ ]:
for storyLink in storyLinks:
    print(storyLink.attr('href'))

Faves


In [ ]:
- PyQuery.hasClass(name)

In [ ]:
- PyQuery.html(value=<NoDefault>, **kwargs)

In [ ]:
- PyQuery.make_links_absolute(base_url=None)[source]