使用 Python 和 BeautifulSoup 列出来自 URL 的 Excel 个文件的名称

Listing names of Excel files from URL using Python and BeautifulSoup

我正在尝试使用 Python 和 BeautifulSoup 进行网络抓取,所以通过教程,但我在成功 requests.get(url) 后卡住了。

一旦我根据标签及其 class 定义要提取的元素(网站上出现的 Excel 文件名的名称),其中包含字符串“file- id-...”(...表示文件的 ID)所有我得到一个空列表。

我的目标是从这个 url 地址列出所有 Excel 文件名,基本上以后使用 for 循环打开它们。所有这些,从国家劳动局提取具体的月度数据,全年结构相同。

labour_office_web_text = requests.get("url").text
soup = BeautifulSoup(labour_office_web_text, "lxml")
file_names = soup.find_all('a[class*="file-id-"]')
file_names

有什么建议吗?谢谢!

要从该页面获取所有 .xls 链接,您可以使用下一个示例:

import requests
from bs4 import BeautifulSoup

url = "https://www.upsvr.gov.sk/statistiky/nezamestnanost-mesacne-statistiky/2020.html?page_id=971502"

soup = BeautifulSoup(requests.get(url).content, "html.parser")

for link in soup.select('a[href*=".xls"]'):
    print(link["class"], link["href"])

打印:

['file-id-1059252'] https://www.upsvr.gov.sk/buxus/docs/statistic/mesacne/2020/MS_2012.xlsx
['file-id-1050892'] https://www.upsvr.gov.sk/buxus/docs/statistic/mesacne/2020/MS_2011.xlsx
['file-id-1042979'] https://www.upsvr.gov.sk/buxus/docs/statistic/mesacne/2020/MS_2010.xlsx
['file-id-1034316'] https://www.upsvr.gov.sk/buxus/docs/statistic/mesacne/2020/MS_2009_okresy.xlsx
['file-id-1027296'] https://www.upsvr.gov.sk/buxus/docs/statistic/mesacne/2020/MS_2008_okresy.xlsx
['file-id-1021527'] https://www.upsvr.gov.sk/buxus/docs/statistic/mesacne/2020/MS_2007_okresy.xlsx
['file-id-1015636'] https://www.upsvr.gov.sk/buxus/docs/statistic/mesacne/2020/MS_2006_okresy.xlsx
['file-id-1009682'] https://www.upsvr.gov.sk/buxus/docs/statistic/mesacne/2020/MS_maj2020_okresy.xlsx
['file-id-1002749'] https://www.upsvr.gov.sk/buxus/docs/statistic/mesacne/2020/MS_apr2020_okresy.xlsx
['file-id-995793'] https://www.upsvr.gov.sk/buxus/docs/statistic/mesacne/2020/MS_mar_2020_okresy.xlsx
['file-id-983937'] https://www.upsvr.gov.sk/buxus/docs/statistic/mesacne/2020/MS_2002_okresy.xlsx
['file-id-971509'] https://www.upsvr.gov.sk/buxus/docs/statistic/mesacne/2020/MS_2001.xlsx