在爬图片时,不想在本地保存了直接传FTP服务器,可以用以下方法。

import requests, io
import ftplib

img = "111.png"
header = {
    "User-Agent" :"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"
}
res = requests.get(img, headers=header)

ftp = ftplib.FTP()
ftp.connect(host="host",port=21,timeout=10)
ftp.login("test1", "test1")
print(ftp.getwelcome()) # 显示信息页面
with io.BytesIO(res.content) as f:
    # 如果目录不存在,会自动创建
    ftp.cwd('/')
    for dir in "/files/test/mm".split('/'):
        if dir:
            try:
                ftp.cwd(dir)
            except ftplib.error_perm:
                ftp.mkd(dir)
                ftp.cwd(dir)

    ftp.storbinary("STOR test.png", f)
ftp.quit()
分类: 网络爬虫 标签: 上传FTP

评论

暂无评论数据

暂无评论数据

目录