Python 抓取數據存儲到Mysql中

# -*- coding: utf-8 -*-
import os,sys
import requests
import bs4
import pymysql
#import MySQLdb
#鏈接MYSQL數據庫 db = pymysql.connect(host='127.0.0.1',user='root',password='mysql',db='test',port=3306,charset='utf8') #db = MySQLdb.connect('127.0.0.1','root','mysql','test',coon.set_character_set('utf8')) print('鏈接數據庫成功!') conn = db.cursor() #獲取指針以操做數據庫 conn.execute('set names utf8') html = 'https://www.dongmanmanhua.cn/dailySchedule?weekday=MONDAY' result = requests.get(html) texts = result.text data = bs4.BeautifulSoup(texts,'html.parser'); lidata = data.select('div#dailyList ul.daily_card li') #print(lidata) arr = {} for x in lidata: did = x.get('data-title-no') print(did) name = x.select('p.subj') name1 = name[0].get_text() url = x.a.get('href') #print(url) story = x.a.p story1 = story.string user = x.select('p.author') user1 = user[0].get_text() like = x.select('em.grade_num') like1 = like[0].get_text() #寫入MYSQL數據庫 t = [did,name1,url,story1,user1,like1] sql = u"INSERT INTO dongman(did,name,url,story,user,likes) VALUES(%s,%s,%s,%s,%s,%s)" conn.execute(sql,t) # t1 = (did,name1,url,story1,user1,like1) # sql1 = u'''insert into dongman(did,name,url,story,user,likes) values (%d,'%s','%s','%s','%s','%s')''' % t1 # conn.execute(sql1) db.commit() #提交操做 print('插入數據成功!') #關閉MySQL鏈接 conn.close() db.close()
相關文章
相關標籤/搜索