未定义 Python 全局名称 'urls' 问题

标签 python mysql

虽然 urls 已正确定义,但我确实不断收到 “全局名称‘urls’未定义” 并且 url 数据未插入 进入 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
from dateutil import parser

WORDS = ['security']

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

HOST = "192.168.150.94"
USER = "root"
PASSWD = "blah"
DATABASE = "tweets"


def store_data(tweet_url):
    db = MySQLdb.connect(host=HOST, user=USER, passwd=PASSWD, db=DATABASE,
                         charset="utf8")
    cursor = db.cursor()
    insert_query = "INSERT INTO  tweet_url (urls) VALUES (%s)"
    cursor.execute(insert_query, (urls))
    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)
            web_url = datajson['entities']['urls']
            print(web_url)
            for i in web_url:
                web_urls = i['expanded_url']
                urls = web_urls
            print(urls)
        store_data(urls)

    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)

最佳答案

你只需要将函数store_data中的参数urls重命名为tweet_url

def store_data(tweet_url):
    db = MySQLdb.connect(host=HOST, user=USER, passwd=PASSWD, db=DATABASE,
                         charset="utf8")
    cursor = db.cursor()
    insert_query = "INSERT INTO  tweet_url (urls) VALUES (%s)"
    cursor.execute(insert_query, (tweet_url))

您想要存储数据的方式仍然不清楚。如果你在循环后调用 store_data,它只存储最后一个值,你最好将每个值存储在一个列表中:

def on_data(self, data):
    try:
        datajson = json.loads(data)
        web_url = datajson['entities']['urls']
        print(web_url)
        urls = []
        for i in web_url:
            urls.append((i['expanded_url'],)) 
            # stores a tuple to make it easy in the database insertion
        print(urls)
        store_data(urls)
    except:
         [...]

这种方式需要在 store_data 中进行另一个小修复:

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

关于未定义 Python 全局名称 'urls' 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45784495/

相关文章:

Python - 将字典列表拆分为单独的字典

php - SQL 语法错误 :Mysqli prepare with NOW()

python - 使用python从mysql查询列表中提取ip地址

php - 每 5 分钟在服务器上执行一次 php 脚本

php - javascript代码中的数据插入到mysql数据库

python - 从 groupby 对象中过滤所有行

python - 通过组合索引#、字符串创建列

python - Microsoft Azure 数据仓库和 SqlAlchemy

python - 解码函数尝试对 Python 进行编码

php - 我无法从 MySQL 数据库中删除一行