1

我正在使用 python、oursql 和 facebook SDK 从 Facebook 抓取数据并将其放入数据库。我编写了一个函数并设法获取列表中的数据,但 executemany 仅将第一行放入数据库。我不知道错误在哪里。我在这里先向您的帮助表示感谢。这是我的功能:

def get_posts(page_id, follow_account_id):
graph = facebook.GraphAPI(get_facebook_access_token()) 
posts = graph.get_connections(page_id, 'posts')
facebook_posts = list()
should_finish = False
while True:  # keep paginating 
    try: 
        with get_db_connection() as cursor: 
            for post in posts['data']: 
                #print post
                if datetime_from_iso(post["created_time"]) < until_time:
                    should_finish = True
                    break
                try:
                    user_id, post_id = post["id"].split("_")  # "133174387208_10154860725907209
                    link = "https://www.facebook.com/{0}/posts/{1}".format(user_id, post_id)
                except:
                    link = None
                facebook_posts.append((3, post_id, None, None,  0, post.get("from").get("name"), post.get("from").get("id"), post.get("from").get("name"), filter_using_re(post.get("message", None)), link, follow_account_id)) 
                print facebook_posts


            cursor.executemany(post_insert_query, facebook_posts)
# get next page 
            posts = requests.get(posts['paging']['next']).json() 
    except KeyError:  
        break 

准备好的声明:

post_insert_query = u'''
INSERT INTO post(
    social_media_id, external_id, post_time, post_date, related_post_id, poster_name, poster_id, title, content, post_url,
    follow_account_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'''
4

0 回答 0