- 1、怎麼把爬取的數據放到mysql數據庫里
- 2、如何將圖片儲存在MySQL數據庫里?
- 3、python爬蟲爬下來的數據怎麼導入到MySQL
根據爬取到的數據的字段分類,設計mysql表進行存錯,文本太長建議設置成text類型,就是普通的jdbc操作。。
解決方法一般有兩種:
1、將圖片保存的路徑存儲到數據庫;
2、將圖片以二進制數據流的形式直接寫入數據庫字段中。
以下為具體方法:
一、保存圖片的上傳路徑到數據庫:
string
uppath=””;//用於保存圖片上傳路徑
//獲取上傳圖片的文件名
string fileFullname =
this.FileUpload1.FileName;
//獲取圖片上傳的時間,以時間作為圖片的名字可以防止圖片重名
string
dataName =
DateTime.Now.ToString(“yyyyMMddhhmmss”);
//獲取圖片的文件名(不含擴展名)
string
fileName = fileFullname.Substring(fileFullname.LastIndexOf(“\\”) +
1);
//獲取圖片擴展名
string type =
fileFullname.Substring(fileFullname.LastIndexOf(“.”) +
1);
//判斷是否為要求的格式
if (type == “bmp” || type == “jpg” || type == “jpeg”
|| type == “gif” || type == “JPG” || type == “JPEG” || type == “BMP” || type ==
“GIF”)
{
//將圖片上傳到指定路徑的文件夾
this.FileUpload1.SaveAs(Server.MapPath(“~/upload”)
+ “\\” + dataName + “.” +
type);
//將路徑保存到變量,將該變量的值保存到數據庫相應字段即可
uppath
= “~/upload/” + dataName + “.” +
type;
}
二、將圖片以二進制數據流直接保存到數據庫:
引用如下命名空間:
using
System.Drawing;
using System.IO;
using
System.Data.SqlClient;
設計數據庫時,表中相應的字段類型為iamge
保存:
//圖片路徑
string
strPath = this.FileUpload1.PostedFile.FileName.ToString
();
//讀取圖片
FileStream fs = new System.IO.FileStream(strPath,
FileMode.Open, FileAccess.Read);
BinaryReader br = new
BinaryReader(fs);
byte[] photo =
br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
//存入
SqlConnection
myConn = new SqlConnection(“Data Source=.;Initial Catalog=stumanage;User
ID=sa;Password=123″);
string strComm = ” INSERT INTO
stuInfo(stuid,stuimage) VALUES(107,@photoBinary
)”;//操作數據庫語句根據需要修改
SqlCommand myComm = new SqlCommand(strComm,
myConn);
myComm.Parameters.Add(“@photoBinary”, SqlDbType.Binary,
photo.Length);
myComm.Parameters[“@photoBinary”].Value =
photo;
myConn.Open();
if (myComm.ExecuteNonQuery()
0)
{
this.Label1.Text =
“ok”;
}
myConn.Close();
讀取:
…連接數據庫字符串省略
mycon.Open();
SqlCommand
command = new
SqlCommand(“select stuimage from stuInfo where stuid=107”,
mycon);//查詢語句根據需要修改
byte[] image = (byte[])command.ExecuteScalar
();
//指定從數據庫讀取出來的圖片的保存路徑及名字
string strPath =
“~/Upload/zhangsan.JPG”;
string strPhotoPath =
Server.MapPath(strPath);
//按上面的路徑與名字保存圖片文件
BinaryWriter bw = new
BinaryWriter(File.Open(strPhotoPath,FileMode.OpenOrCreate));
bw.Write(image);
bw.Close();
//顯示圖片
this.Image1.ImageUrl
= strPath;
採用這兩種方式可以根據實際需求靈活選擇。
下載mysql.connector庫
然後把爬蟲爬到的數據通過mysql裏面的insert語句查到數據庫,當然也可以建表,一般我沒用python建表 是先建好再寫數據的
import mysql.connector
conn = mysql.connector.connect(
user=’root’,
password=’root’,
host=’127.0.0.1′,
port=’3306′,
database=’test_demo’
)
cursor = conn.cursor()
cursor.execute(“INSERT INTO test_user(`uuid`,`user_name`,`user_level`) VALUES (%s,%s,%s)”,[id, user_name, user_level])
cursor.execute(“INSERT INTO tieba_user_detail(`user_name`,`user_exp`,`user_sex`,`tieba_age`,`tieba_note`,`user_favorites`,`user_fans`) VALUES (%s,%s,%s,%s,%s,%s,%s)”,[user_name,user_exp,user_sex, tieba_age,tieba_note, user_favorites, user_fans])
print(‘************** %s %s 數據保存成功 **************’%(user_rank,user_name))
conn.commit()
cursor.close()
插進入就這樣的
原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/126552.html