gpt4 book ai didi

python-2.7 - 从 URL 下载所有 csv 文件

转载 作者:行者123 更新时间:2023-12-05 05:23:00 25 4
gpt4 key购买 nike

我想下载所有 csv 文件,知道我该怎么做吗?

from bs4 import BeautifulSoup
import requests
url = requests.get('http://www.football-data.co.uk/englandm.php').text
soup = BeautifulSoup(url)
for link in soup.findAll("a"):
print link.get("href")

最佳答案

像这样的东西应该可以工作:

from bs4 import BeautifulSoup
from time import sleep
import requests


if __name__ == '__main__':
url = requests.get('http://www.football-data.co.uk/englandm.php').text
soup = BeautifulSoup(url)
for link in soup.findAll("a"):
current_link = link.get("href")
if current_link.endswith('csv'):
print('Found CSV: ' + current_link)
print('Downloading %s' % current_link)
sleep(10)
response = requests.get('http://www.football-data.co.uk/%s' % current_link, stream=True)
fn = current_link.split('/')[0] + '_' + current_link.split('/')[1] + '_' + current_link.split('/')[2]
with open(fn, "wb") as handle:
for data in response.iter_content():
handle.write(data)

关于python-2.7 - 从 URL 下载所有 csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39033674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com