尝试从tweets
获取'expanded_url'和'time_stamp'并将数据插入MYSQL
DB。我希望插入query
似乎是正确的。但是查询似乎有问题,并且我不断遇到“ not enough arguments for format string
”问题
不知道,我在哪里犯错。有什么建议吗?
#! /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 - Python MYSQL插入问题:格式字符串的参数不足,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45907162/