mongodb-MYSQL

#encoding:utf8

import pymongo
import MySQLdb
import random
def GetMongoData():
MyQuery = Mongo_Tab.find({'is_indb':"0"})
if MyQuery.count() > 0:
return MyQuery
else:
return []

def Update_MongoData(sha1_data):
MyQuery = {"sha1_title":sha1_data}
NewValue = {"$set":{"is_indb":"1"}}
Mongo_Tab.update(MyQuery,NewValue)


def InMySQL(set_data):
if len(set(set_data)) >0:
for i in set_data:
operation_subscribeCount = random.randint(10,20)
operation_bidcount = random.randint(10,20)
sql_in = 'insert into bid_invitation(title,province_id,province_name,city_id,city_name,county_id,county_name,publish_time,detail_html,source_link,iscatch,operation_subscribeCount,operation_bidcount,review_status)VALUE ("%s","%d","%s","%d","%s","%d","%s","%s","%s","%s","%d","%d","%d","%d")'%(i['title'],int(i['province_id']),i['province'],int(i['city_id']),i['city'],int(i['county_id']),i['county'],i['show_date'],i['content'].replace('"','\''),i['url'],1,operation_subscribeCount,operation_bidcount,2)
MySQL_Cur.execute(sql_in)
MySQL_Client.commit()
print '11111'
Update_MongoData(i['sha1_title'])
else:
print '没有新的数据'

if __name__ == '__main__':
Mongo_Client = pymongo.MongoClient()
Mongo_DB = Mongo_Client['Tender']
Mongo_Tab = Mongo_DB['HBGGZY']
MySQL_Client = MySQLdb.connect('101.201.70.139','root','Myjr678!@#','ant',charset='utf8')
MySQL_Cur = MySQL_Client.cursor()
InMySQL(GetMongoData())



posted @ 2018-08-19 19:44  破晓e  阅读(118)  评论(0编辑  收藏  举报