我正在通过网页上的超链接找到下载.pdf文件的方法。
从How can i grab pdf links from website with Python script那里学到的方法是:
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文件名正在更改呢?还是我只想限制在网页上的搜索,在“应用程序”的超链接上?谢谢。
发布于 2014-10-28 07:37:34
好吧,这不是最好的方法,但没有坏处:
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']https://stackoverflow.com/questions/26600696
复制相似问题