【发布时间】:2021-05-21 17:15:10
【问题描述】:
这是我的代码:
from bs4 import BeautifulSoup
import requests
from requests import get
import os
def file_download():
domain = "ec.europa.eu"
page = requests.get("https://ec.europa.eu/eurostat/web/main/data/database")
html = page.text
soup = BeautifulSoup(html, "html.parser")
for link in soup.find_all('a'):
url = link.get('href')
print(url)
if ".gz" in url:
file_name = url.split("file=", 1)[1]
if os.path.exists(file_name):
print("File already exists.")
continue
else:
with open(file_name, 'wb') as file:
print('Downloading...')
response = get(url)
file.write(response.content)
continue
else:
continue
print('\nEvery file has been downloaded!')
在上面的代码中,我似乎无法从页面中找到所有可能的链接。 在 chrome 检查中,复制的元素为我提供了我写的评论。 这就是我想通过 beautifulsoup 以及其他类似链接找到的内容。
【问题讨论】:
-
ec.europa.eu/eurostat/estat-navtree-portlet-prod/…" title="用于下载TSV格式的完整表格">
-
这应该可以得到文件:stackoverflow.com/a/67649809/6106791
标签: python beautifulsoup python-requests-html