python 连接mysql

 
#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&currentPageNum=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
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值