图片存储直接链接

import requests
from lxml import etree
import os
import random
import time
from fake_useragent import UserAgent
headers={
            'User-Agent':UserAgent().random
        }
url='https://chinamarketmx.com/wp-content/uploads/TECAUD63967-300x300.png'
img_data = requests.get(url=url,headers=headers).content

#创建一个文件夹
if not os.path.exists('./pictures'):
    os.mkdir('./pictures')

img_name="pict"+'.jpg'
img_name = img_name.encode('iso-8859-1').decode('gbk')
img_path = 'pictures/'+img_name
    
with open(img_path,'wb') as fp:
    fp.write(img_data)
    print(img_name,'下载成功!!!')




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MySQL本身并不适合直接存储图片,但可以在MySQL中存储图片的路径和文件名等信息,然后将图片文件存储在服务器上,以便在需要时进行检索和显示。 在Node.js中,可以使用以下步骤来实现将图片存储到服务器上并将其路径保存到MySQL中: 1.使用Multer或其他类似的中间件,在Node.js中设置表单以上传文件。 2.在服务器上创建一个用于存储图片的文件夹。 3.在上传文件的回调函数中,将文件保存到服务器上的文件夹中。 4.使用MySQL驱动程序连接到MySQL数据库。 5.在MySQL中创建一个表格,其中包含用于存储图片路径和文件名的列。 6.在上传文件的回调函数中,将文件路径和文件名插入到MySQL表格中。 7.在需要显示图片的地方,从MySQL表格中获取文件路径和文件名,然后使用HTTP服务器将图片发送回客户端。 下面是一个简单的示例代码,演示如何在Node.js中将图片上传到服务器并将其路径保存到MySQL中: ``` const express = require('express'); const mysql = require('mysql'); const multer = require('multer'); const path = require('path'); const app = express(); // 配置Multer中间件 const storage = multer.diskStorage({ destination: function(req, file, cb) { cb(null, 'uploads/'); }, filename: function(req, file, cb) { cb(null, Date.now() + path.extname(file.originalname)); } }); const upload = multer({ storage: storage }); // 连接到MySQL数据库 const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password', database: 'mydatabase' }); connection.connect(); // 创建一个包含图片路径和文件名的表格 connection.query('CREATE TABLE IF NOT EXISTS images (id INT AUTO_INCREMENT PRIMARY KEY, path VARCHAR(255) NOT NULL, filename VARCHAR(255) NOT NULL)'); // 设置路由来处理表单上传 app.post('/upload', upload.single('image'), function(req, res) { const imagePath = req.file.path; const imageName = req.file.filename; // 将图片路径和文件名插入到MySQL表格中 connection.query('INSERT INTO images (path, filename) VALUES (?, ?)', [imagePath, imageName], function(error, results, fields) { if (error) throw error; res.send('Image uploaded!'); }); }); // 设置路由来显示图片 app.get('/image/:id', function(req, res) { const id = req.params.id; // 从MySQL表格中获取图片路径和文件名 connection.query('SELECT path, filename FROM images WHERE id = ?', [id], function(error, results, fields) { if (error) throw error; const imagePath = results[0].path; const imageName = results[0].filename; // 发送图片到客户端 res.sendFile(imagePath + imageName); }); }); app.listen(3000, function() { console.log('Server started on port 3000'); }); ``` 请注意,此示例代码仅用于演示如何将图片上传到服务器并将其路径保存到MySQL中。在实际应用中,还需要添加更多的错误处理和安全性检查,以确保上传的文件是图片,并且防止任意文件上传等安全问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

聆听我的召唤,菜鸟进化

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值