我想爬取这个网页https://data.binance.vision/?prefix=data/spot/daily/aggTrades/ETHUSDT/中的所有ZIP文件,并保存到本地,python如何实现
时间: 2023-07-07 08:43:04 浏览: 45
你可以使用Python的requests和BeautifulSoup库来实现这个任务。
首先,我们需要发送HTTP请求来获取网页内容。然后,使用BeautifulSoup库解析HTML内容,找到所有的ZIP文件链接。最后,使用requests库下载这些ZIP文件并保存到本地。
以下是实现的代码:
```python
import requests
from bs4 import BeautifulSoup
import zipfile
# 发送HTTP请求并获取网页内容
url = "https://data.binance.vision/?prefix=data/spot/daily/aggTrades/ETHUSDT/"
response = requests.get(url)
# 解析HTML内容并找到所有的ZIP文件链接
soup = BeautifulSoup(response.content, "html.parser")
links = soup.find_all("a")
zip_links = [link.get("href") for link in links if link.get("href").endswith(".zip")]
# 下载并保存ZIP文件
for zip_link in zip_links:
filename = zip_link.split("/")[-1]
response = requests.get(zip_link)
with open(filename, "wb") as f:
f.write(response.content)
print(f"Saved {filename} to local directory")
```
这个代码会将所有的ZIP文件下载并保存到当前的工作目录中。如果你想要保存到其他目录,可以修改代码中的`open()`函数的参数。