1. Packages that need guidance
Import Pymysql
2.# MySQL connection information (in dictionary form)
Db_config ={
' Host ': ' 127.0.0.1 ', #连接的主机id (107.0.0.1 is a native ID)
' Port ': 3306,
' User ': ' * * * ',
' Password ': ' * * * ',
' DB ': ' Test ', # (database name)
' CharSet ': ' UTF8 '
}
3.# Getting a database connection
Connection = Pymysql.connect (**db_config)
Connection () Details of the basic knowledge of the connection
53994112
4. Specific connection (take Pinterest as an example)
1 Try:2 #Get Database Cursors (cursors provide a flexible means of manipulating data retrieved from a table, essentially, a cursor is essentially a mechanism for extracting one record at a time from a result set that includes multiple data records.) Cursors are always associated with an SQL selection statement because the cursor consists of a result set (which can be 0, one, or multiple records retrieved by a related selection statement) and a cursor position in the result set that points to a particular record. )3 With Connection.cursor () as cursor:4sql ='insert into Simplebook (title, URL) values (%s,%s)'5 forUinchURLs:6 #Execute SQL statement7Cursor.execute (SQL, (u.string, R'http://www.jianshu.com'+u.attrs['href']))8 #Transaction Commit9 Connection.commit ()Ten finally: One #To close a database connection AConnection.close ()
5. Connect the database successfully and get the data
Database connectivity issues for Python crawlers