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

时间:2019-07-12 12:26:53

标签: python mysql api

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

玩弄了代码,但没有使它起作用。 各种“预期缩进的块”错误

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

data = requests.get('https://newsapi.org/v2/top-headlines?country=us&apiKey=xxxxxxxxxxxxxxxxxxxx')

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(self, 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)

我希望将输出存储到mysql表中...但是尝试运行脚本时出现错误。 我还需要使它无限运行直到病死了进程/会话。...

3 个答案:

答案 0 :(得分:0)

您的缩进全部搞砸了,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)

已更新,以反映评论中建议的更改

答案 1 :(得分:0)

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)

答案 2 :(得分:0)

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)

最终使其成功!