[英]Python + BeautifulSoup - Limiting text extraction on a specific table (multiple tables on a webpage)
[英]Python, Limiting search at a specific hyperlink on webpage
我正在尋找一種通過網頁上的超鏈接下載.pdf文件的方法。
從如何使用Python腳本從網站獲取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']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.