22

I'm using PyQuery and want to print a list of links, but can't figure out how to get the href attribute from each link in the PyQuery syntax.

This is my code:

  e = pq(url=results_url)
  links = e('li.moredetails a')
  print len(links)
  for link in links:
    print link.attr('href')

This prints 10, then gives the following error:

AttributeError: 'HtmlElement' object has no attribute 'attr'

What am I doing wrong?

Richard
  • 62,943
  • 126
  • 334
  • 542

2 Answers2

32

PyQuery wraps lxml, so you use the ElementTree API to access attributes:

e = pq(url=results_url)
for link in e('li.moredetails a'):
    print link.attrib['href']

Alternatively, to use the PyQuery API on any found element, wrap the element in a pq() call, echoing the way you need to use jQuery $() or jQuery() to wrap DOM elements:

    print pq(link).attr('href')

or

    print pq(link).attr['href']

for a more pythonic way to accessess the attributes.

You could also loop over the .items() method instead, which returns PyQuery elements instead:

e = pq(url=results_url)
for link in e('li.moredetails a').items():
    print link.attr['href']
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Is the `pq(...).attr()` method documented? I don't see it in the API doc: https://pythonhosted.org/pyquery/api.html – Joe Coder May 06 '15 at 06:03
  • @JoeCoder: see the [quickstart](https://pythonhosted.org/pyquery/index.html), where the `PyQuery` is imported and bound to the name `pq`. – Martijn Pieters May 06 '15 at 07:02
3

As in jQuery, wrap that link up:

e = pq(url=results_url)
links = e('li.moredetails a')
print len(links)
for link in links:
    print pq(link).attr('href')
Pavel Anossov
  • 60,842
  • 14
  • 151
  • 124