以下是使用BeautifulSoup抓取CSV格式的URL列表的代码示例:
import requests
from bs4 import BeautifulSoup
# 指定目标网页的URL
url = 'http://example.com/csv_links.html'
# 发送GET请求获取网页内容
response = requests.get(url)
# 使用BeautifulSoup解析网页内容
soup = BeautifulSoup(response.content, 'html.parser')
# 找到所有CSV链接
csv_links = soup.find_all('a', href=lambda href: href and href.endswith('.csv'))
# 打印CSV链接列表
for link in csv_links:
print(link['href'])
在这个示例中,我们首先导入requests
库和BeautifulSoup
库。然后,我们指定目标网页的URL,并发送GET请求获取网页内容。接下来,我们使用BeautifulSoup解析网页内容,并使用find_all
方法找到所有以.csv
结尾的链接。最后,我们遍历这些链接,并打印它们的href
属性(即链接地址)。