gpt4 book ai didi

Python MYSQL 插入问题 : not enough arguments for format string

转载 作者:行者123 更新时间:2023-11-29 10:38:53 26 4
gpt4 key购买 nike

尝试从推文获取“expanded_url”和“time_stamp”并将数据插入MYSQL数据库。我希望插入查询似乎是正确的。但查询似乎存在问题,并且我一直遇到“格式字符串参数不足”的问题

不确定,我在哪里犯了错误。请问有什么建议吗?

#! /usr/bin/python


#Description : This script can collect the URLs from Tweets and Records them into research MYSQL DB.

from __future__ import print_function
import tweepy
import json
import MySQLdb
import time
from dateutil import parser

WORDS = ['security']

#CREDENTAILS
CONSUMER_KEY = ""
CONSUMER_SECRET = ""
ACCESS_TOKEN = ""
ACCESS_TOKEN_SECRET = ""

HOST = "192.168.150.94"
USER = "root"
PASSWD = "password"
DATABASE = "twitter"

def store_data(values, insert_time):
db=MySQLdb.connect(host=HOST, user=USER, passwd=PASSWD, db=DATABASE, charset="utf8")
cursor = db.cursor()
cursor.executemany("""INSERT INTO tweet_url VALUES (%s,%s)""",(values,insert_time))
db.commit()
cursor.close()
db.close()
return

class StreamListener(tweepy.StreamListener):


def on_connect(self):
print("We are now connected to the streaming API.")

def on_error(self, status_code):
print('An Error has occured: ' + repr(status_code))
return False

def on_data(self, data):
try:
datajson = json.loads(data)
#text = datajson['text']
#screen_name = datajson['user']['screen_name']
#expanded_url= datajson['entities']['urls'][0]['expanded_url']
web_url= datajson['entities']['urls']
print(web_url)
urls=[]
for i in web_url:
urls.append((i['expanded_url']))
values = [list([item]) for item in urls]
insert_time=time.strftime('%Y-%m-%d %H:%M:%S')
store_data(values, insert_time)


except Exception as e:
print(e)

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
listener = StreamListener(api=tweepy.API(wait_on_rate_limit=True))
streamer = tweepy.Stream(auth=auth, listener=listener)
print("Tracking: " + str(WORDS))
streamer.filter(track=WORDS)

数据库详细信息:

Database Name : twitter
Table Name : tweet_urls
Column in 'tweet_urls' : urls & time_stamp(TYPE:DATETIME)

最佳答案

替换:

 cursor.executemany("""INSERT INTO tweet_url VALUES (%s,%s)""",(values,insert_time))

与:

 data = []
for value in values:
data.append((value, insert_time))

# execute many expects tuple (value, insert_time) for every row

cursor.executemany("""INSERT INTO tweet_url VALUES (%s,%s)""",data)

关于Python MYSQL 插入问题 : not enough arguments for format string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45907162/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com