使用python和executemany在列表中插入数据库facebook帖子

时间:2017-02-19 00:13:28

标签: python mysql facebook web-scraping executemany

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

const fn = (obj, ...restArgs) => {
  if(restArgs.length > 0) {
    // got more arguments than expected
    throw new Error('Must only pass in single argument');
  }
};

准备好的声明:

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 

0 个答案:

没有答案