Cmag Cmag - 1 year ago 114
Python Question

Python lxml/beautiful soup to find all links on a web page

I am writing a script to read a web page, and build a database of links that matches a certain criteria. Right now I am stuck with lxml and understanding how to grab all the

<a href>
's from the html...

result = self._openurl(self.mainurl)
content =
html = lxml.html.fromstring(content)
print lxml.html.find_rel_links(html,'href')

Answer Source

Use XPath. Something like (can't test from here):

urls = html.xpath('//a/@href')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download