Python,将搜索限制在网页上的特定超链接上

Python,将搜索限制在网页上的特定超链接上,python,html,hyperlink,Python,Html,Hyperlink,我正在寻找通过网页上的超链接下载.pdf文件的方法 从中学习,方法是: import lxml.html, urllib2, urlparse base_url = 'http://www.renderx.com/demos/examples.html' res = urllib2.urlopen(base_url) tree = lxml.html.fromstring(res.read()) ns = {'re': 'http://exslt.org/regular-expression

我正在寻找通过网页上的超链接下载.pdf文件的方法

从中学习,方法是:

import lxml.html, urllib2, urlparse

base_url = 'http://www.renderx.com/demos/examples.html'
res = urllib2.urlopen(base_url)
tree = lxml.html.fromstring(res.read())

ns = {'re': 'http://exslt.org/regular-expressions'}

for node in tree.xpath('//a[re:test(@href, "\.pdf$", "i")]', namespaces=ns):
    print urlparse.urljoin(base_url, node.attrib['href'])
问题是,我怎样才能在特定的超链接下找到.pdf,而不是在网页上列出所有的.pdf

一种方法是,我可以限制打印时,它包含某些字,如:

If ‘CA-Personal.pdf’ in node:

但是如果.pdf文件名正在更改怎么办?或者我只是想限制在网页上的搜索,在“应用程序”的超链接?谢谢。

好吧,这不是最好的方法,但没有坏处:

from bs4 import BeautifulSoup
import urllib2

domain = 'http://www.renderx.com'    
url = 'http://www.renderx.com/demos/examples.html'

page = urllib2.urlopen(url)
soup = BeautifulSoup(page.read())
app = soup.find_all('a', text = "Applications")

for aa in app:
    print domain + aa['href']