【发布时间】:2021-07-15 01:02:38
【问题描述】:
我正在尝试创建一个自动 Python 脚本,该脚本可以转到 this 之类的网页,在正文底部找到链接(锚文本“此处”),然后下载单击所述下载后加载的 PDF关联。我能够从原件中检索 HTML 并找到下载链接,但我不知道如何从那里获取 link to the PDF。任何帮助将非常感激。到目前为止,这是我所拥有的:
import urllib3
from urllib.request import urlopen
from bs4 import BeautifulSoup
# Open page and locate href for bill text
url = 'https://www.murphy.senate.gov/newsroom/press-releases/murphy-blumenthal-introduce-legislation-to-create-a-national-green-bank-thousands-of-clean-energy-jobs'
html = urlopen(url)
soup = BeautifulSoup(html, 'html.parser')
links = []
for link in soup.findAll('a', href=True, text=['HERE', 'here', 'Here']):
links.append(link.get('href'))
links2 = [x for x in links if x is not None]
# Open download link to get PDF
html = urlopen(links2[0])
soup = BeautifulSoup(html, 'html.parser')
links = []
for link in soup.findAll('a'):
links.append(link.get('href'))
links2 = [x for x in links if x is not None]
此时,我获得的链接列表不包括我正在寻找的 PDF。有什么方法可以在不硬编码代码中指向 PDF 的链接的情况下获取它(这与我在这里尝试做的事情有悖常理)?谢谢!
【问题讨论】:
-
您可以
requests.get(url, allow_redirects=True)并使用r.history查看重定向历史记录。从那里,检索您的 pdf 文件应该很简单。 -
看起来像在“这里”链接中添加
download=1参数可以让它为您下载 PDF。https://www.murphy.senate.gov/download/green-bank-act-2021?download=1。您可能必须允许重定向才能正常工作。 -
使用脚本检索的链接是页面通过 href 链接到的链接。所以这是关于缺少重定向的问题(请参阅其他 cmets)。
标签: python beautifulsoup urllib3 urlopen