Using PyQuery and Gadget selector to extract URLs from a Website

78 views Asked by At

I have code that only works partially:

from pyquery import PyQuery as pq
import requests

url = SAMPLE_URL.com 
content = requests.get(url).content
doc = pq(content)

Latest_Report = doc(".head+ .post .heading")

Latest_Report.text()

I am able to get the Text element with this. But I want to use the URL available here.

print(Latest_Report)

What is the best way to get the href:

<a class="heading" href="URL_WANTED">ABC’s September Construction Backlog Indicator Dips, Yet Contractors Remain Confident</a> 
1

There are 1 answers

0
user459872 On BEST ANSWER

You can use Latest_Report.attr('href') to access the href attribute of the element.