Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正在尝试收集API数据并发送到本地mysql_Python_Mysql_Api - Fatal编程技术网

Python 正在尝试收集API数据并发送到本地mysql

Python 正在尝试收集API数据并发送到本地mysql,python,mysql,api,Python,Mysql,Api,我无法使我创建的脚本正常工作。 它需要收集API数据(返回JSON) 我想将特定数据保存到MYSQL 玩弄代码却没能让它工作。。。 各种“预期缩进块”错误 我希望输出存储到mysql表中。。。但我在尝试运行脚本时出错。 另外,我需要让它无休止地运行,直到我终止进程/会话……你的缩进都搞乱了,Python依赖缩进。没有查看代码本身,因此可能仍然存在错误,但修复了缩进: from __future__ import print_function import requests import re i

我无法使我创建的脚本正常工作。 它需要收集API数据(返回JSON) 我想将特定数据保存到MYSQL

玩弄代码却没能让它工作。。。 各种“预期缩进块”错误

我希望输出存储到mysql表中。。。但我在尝试运行脚本时出错。
另外,我需要让它无休止地运行,直到我终止进程/会话……

你的缩进都搞乱了,Python依赖缩进。没有查看代码本身,因此可能仍然存在错误,但修复了缩进:

from __future__ import print_function
import requests
import re
import MySQLdb
import json

HOST = "localhost"
USER = "root"
PASSWD = "user"
DATABASE = "something"

def store_data(articles, source, auther, title, description, url, timestamp, content):
    db = MySQLdb.connect(host = HOST, user = USER, passwd = PASSWD, db = DATABASE, charset = "utf8")
    cursor = db.cursor()
    insert_query = MySQLdb.escape_string("INSERT INTO table (articles, source, auther, title, description, url, timestamp, content) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)")
    cursor.execute(insert_query, (articles, source, auther, title, description, url, timestamp, content))
    db.commit()
    cursor.close()
    db.close()
    return

        # db = MySQLdb.connect(host = HOST, user = USER, passwd = PASSWD, db = DATABASE, charset = "utf8")# cursor = db.cursor()

def on_data(data): #This is the meat of the script...it connects to your mongoDB and stores the tweet
    try:
        datajson = json.loads(data) #  grab the wanted data from the Tweet
        articles = datajson['articles']
        source = datajson['articles']['source']['name']
        auther = datajson['articles']['auther']
        title = datajson['articles']['title']
        description = datajson['articles']['description']
        url = datajson['articles']['url']
        timestamp = parser.parse(datajson['articles']['publishedAt'])
        content = datajson['articles']['content']

        # insert the data into the MySQL database
        store_data(articles, source, auther, title, description, url, timestamp, content)
    except Exception as e:
        print(e)

if __name__ == '__main__':
    data = requests.get('https://newsapi.org/v2/top-headlines?country=us&apiKey=xxxxxxxxxxxxxxxxxxxx')
    on_data(data)
更新以反映评论中建议的更改

import requests
import MySQLdb
from dateutil import parser

HOST = "localhost"
USER = "root"
PASSWD = "xxxxx"
DATABASE = "xxxxx"



# api-endpoint
URL = "https://newsapi.org/v2/sources?apiKey=xxxxxxxxxxxxxxxxxxx"


# API given here
country = "us"


# defining a params dict for the parameters to be sent to the API
PARAMS = {'country':country}

# sending get request and saving the response as response object
r = requests.get(url = URL, params= PARAMS)

# extracting data in json format
data = r.json()

# extracting latitude, longitude and formatted address
# of the first matching location
articles = data['sources'][0]['id']

# printing the output
print("article name:%s"
      %(articles))


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


#insert the data into the MySQL database
    store_data(articles)

终于成功了

这种安排很有帮助,但如果有人能签出代码,我们将不胜感激。。。如果您可以提供您正在获取的任何错误,那么一些不正确的内容会很有帮助。该内容正在运行,但我没有任何错误-但是数据不会进入数据库这是您的整个脚本吗?如果是这样,您就永远不会调用
on_data()
函数,这就是为什么什么都不会发生的原因。在文件底部,添加以下内容:
on_data(data)
,不带任何缩进。另外,删除
on_data()
定义中的
self
参数,它不应该在那里。我对答案进行了一些编辑,以反映我建议的更改,并在主检查中移动了数据收集和函数调用。现在唯一的事情是,我只从API中获得第一个结果,而不是所有结果:(要获得所有结果,您可以对范围内的i循环
(len(数据['sources')):print(数据['sources'][i]['id'))
import requests
import MySQLdb
from dateutil import parser

HOST = "localhost"
USER = "root"
PASSWD = "xxxxx"
DATABASE = "xxxxx"



# api-endpoint
URL = "https://newsapi.org/v2/sources?apiKey=xxxxxxxxxxxxxxxxxxx"


# API given here
country = "us"


# defining a params dict for the parameters to be sent to the API
PARAMS = {'country':country}

# sending get request and saving the response as response object
r = requests.get(url = URL, params= PARAMS)

# extracting data in json format
data = r.json()

# extracting latitude, longitude and formatted address
# of the first matching location
articles = data['sources'][0]['id']

# printing the output
print("article name:%s"
      %(articles))


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


#insert the data into the MySQL database
    store_data(articles)
from __future__ import print_function
import requests
import MySQLdb
from dateutil import parser

HOST = "localhost"
USER = "root"
PASSWD = "ssss!"
DATABASE = "sss"

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


# api-endpoint
URL = "https://newsapi.org/v2/sources?apiKey=ssssssssss"


# API given here
country = "us"


# defining a params dict for the parameters to be sent to the API
PARAMS = {'country':country}

# sending get request and saving the response as response object
r = requests.get(url = URL, params= PARAMS)

# extracting data in json format
data = r.json()

# extracting latitude, longitude and formatted address
# of the first matching location
articles = data['sources'][0]['id']

# printing the output
print("article name:%s"
      %(articles))

#insert the data into the MySQL database
store_data(articles)