可以使用Python中的requests庫和BeautifulSoup庫來實現爬取網頁中的下載列表。以下是一個簡單的示例代碼:
import requests
from bs4 import BeautifulSoup
url = 'http://example.com/download-list'
response = requests.get(url)
if response.status_code == 200:
soup = BeautifulSoup(response.content, 'html.parser')
download_links = []
for link in soup.find_all('a'):
if link.get('href') and link.get('href').endswith('.zip'):
download_links.append(link.get('href'))
for download_link in download_links:
download_response = requests.get(download_link)
with open(download_link.split('/')[-1], 'wb') as f:
f.write(download_response.content)
print('下載完成!')
else:
print('無法訪問網頁')
在以上代碼中,首先使用requests庫獲取網頁內容,然后使用BeautifulSoup庫解析網頁,找出所有包含下載鏈接的標簽,并篩選出以’.zip’結尾的鏈接。最后,依次下載這些鏈接對應的文件,并保存到本地。