資源簡介
使用Python的urllib庫爬取小說網站信息,并吧小說目錄和小說內容存儲到數據庫
代碼片段和文件信息
#!/usr/bin/python
#?-*-?coding:?UTF-8?-*-
import?urllib3
import?re
‘‘‘
#配置數據庫
import?MySQLdb
class?Sql(object):
????conn?=?MySQLdb.connect(host=‘192.168.19.213‘port=3306user=‘root‘passwd=‘Admin123‘db=‘novel‘charset=‘utf8‘)
????def?addnovels(selfsortnovelname):
????????cur?=?self.conn.cursor()
????????cur.execute(“insert?into?novel(sortnovelname)?values(%s??‘%s‘)“?%(sortnovelname))
????????lastrowid?=?cur.lastrowid
????????cur.close()
????????self.conn.commit()
????????return?lastrowid
????def?addchapters(selfnovelidchapternamecontent):
????????cur?=?self.conn.cursor()
????????cur.execute(“insert?into?chapter(novelidchapternamecontent)?values(%s??‘%s‘?‘%s‘)“?%(novelidchapternamecontent))
????????cur.close()
????????self.conn.commit()
‘‘‘
doma
評論
共有 條評論