#coding:utf8 import requests import pand as pd import re import MySQLdb db = MySQLdb.connect("localhost","root","root",charset='utf8') cursor = db.cursor() def data(url): response = requests.get(url=url) data = re.findall(r'"content":".*?"',response.text) data = pd.Series(data) data = data.str.replace('"content":','') return data '''储存数据''' def storage(datas): cursor.execute("use taobao") for i in datas: cursor.execute('insert into dd(pingfeng) VALUES (\'%s\')'%(i)) db.commit() # cursor.execute("select * from taobao") # bb = cursor.fetchall() # print pd.Series(bb) def read(): '''执行sql语句''' cursor.execute('use taobao') cursor.execute('select * from dd') '''返回所有数据''' data = cursor.fetchall() file = open('data.csv','w+') for i in data: # file.write(str(i).decode(encoding='utf8')) c = str(i).decode('unicode_escape').replace("u'",'').strip('(""),\'') file.write(c.encode('utf8')+'\n') file.close() # print data url = "https://rate.taobao.com/feedRateList.htm?auctionNumId=563070438131&userNumId=661274088¤tPageNum=2&pageSize=20&rateType=&orderType=sort_weight&attribute=&sku=&hasSku=false&folded=0&ua=098%23E1hvnpvWvRhvUpCkvvvvvjiPPLcwgjYnPssZtjnEPmPU6jrPRs5vljD8RFSOtjEvn2yCvvpvvvvv3QhvCvvhvvm5vpvhvvmv99GCvvpvvvvvvphvC9vhvvCvpvyCvhQUAKgvCzox9WLyjLyDZacEKOmAdc9DYUxrD46XjZmQD7zUdit3CVz9afm65kx%2FAjcG6C9ww65hAnsO5CO0747B9Wma%2BoHoDO2UQC6tKphv8vvvv8RMpR%2FHvvmC2hCv2vQvvUEpphvWh9vv9DCvpv11vvmmL6Cv2VeEvpvVmvvC9jXmuphvmvvv9bm4uP%2BNRphvCvvvvvmrvpvEvvFXvieXvhm69phv2nMzMXzO7rMNmZHLzv%3D%3D&_ksTS=1519786732826_1594&callback=jsonp_tbcrate_reviews_list" datas = data(url) # storages = storage(datas) reads = read() 参考网站:http://www.runoob.com/python/python-mysql.html
python 连接mysql
最新推荐文章于 2024-08-29 17:58:04 发布