Last active
June 16, 2020 06:08
-
-
Save ting11222001/38c948b0453a7528f3a9b0ad094e641b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# !pip install PyMySQL | |
import pymysql | |
#連結自己架在Virtual Box (Centos 7) 的mariadb | |
conn = pymysql.connect(host = '192.168.56.123', | |
user = 'tiffany', | |
passwd = "admin123", | |
db = 'demo_2020_pyscrap') | |
#創一個cursor物件,是在資料庫操作的游標 | |
cur = conn.cursor() | |
#把剛剛爬下來的句子用原生SQL語言插入指定表格,確保插入每個項目都是string | |
#例句當中有一些雙引號和反斜線,為了避免程式碼出錯,先把這些符號拿掉再寫入mariadb | |
for i in results: | |
symOut1 = i.replace('"','') | |
symOut2 = symOut1.replace('\\','') | |
sql = "insert into JPA_ENG (Sentence) values (%s)" % ('"'+symOut2+'"') | |
cur.execute(sql) | |
#把之前的改變確實反應到資料庫中 | |
conn.commit() | |
#關閉資料庫連線 | |
conn.close() | |
#把之前的改變確實反應到資料庫中 | |
conn.commit() | |
#關閉資料庫連線 | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment