BeautifulSoup 使用动态 href

BeautifulSoup with dynamic href

正在尝试 python 3.4 beautifulsoup 从网页上抓取一个 zip 文件,以便我可以将其解压缩并下载到一个文件夹中。我可以让 beautifulsoup 打印()页面上的所有 href,但我想要一个特定的 href ending,“=Hospital_Revised_Flatfiles.zip”。那可能吗?这是我目前所拥有的,只有 url.

中的 href 列表

文件的完整 href 是,https://data.medicare.gov/views/bg9k-emty/files/Dlx5-ywq01dGnGrU09o_Cole23nv5qWeoYaL-OzSLSU?content_type=application%2Fzip%3B%20charset%3Dbinary&filename=Hospital_Revised_Flatfiles.zip ,但是当他们更新文件时,中间疯狂的东西会发生变化,而且无法知道它会变成什么。

如果我遗漏了可能有帮助的问题,请告诉我。我正在使用 Python 3.4 和 BeautifulSoup4 (bs4)

from bs4 import BeautifulSoup 
import requests
import re

url = "https://data.medicare.gov/data/hospital-compare"

r = requests.get(url)

data = r.text

soup = BeautifulSoup(data)

for link in soup.find_all('a'):
    print(link.get('href'))
from BeautifulSoup import BeautifulSoup 
import requests
import re

url = "https://data.medicare.gov/data/hospital-compare"

r = requests.get(url)

data = r.text

soup = BeautifulSoup(data)

for link in soup.findAll('a'):
   if link.has_key('href'):
      if(link['href'].endswith("=Hospital_Revised_Flatfiles.zip")):
         print(link['href'])