2017-04-03 77 views
1

下破壞我有代碼如下:環路執行功能

def scrapeFacebookPageFeedStatus(access_token): 

    query = "SELECT page_id FROM falken" 

    result_list = c.execute(query) 

    for single_row in result_list: 

     str_single_row = str(single_row) 

     str_norm_single_row = str_normalize(str_single_row) 

     print(str_norm_single_row) 

當我執行上面的代碼,它從result_list顯示每SINGLE_ROW值。

但是當我通過SINGLE_ROW到功能象下面這樣:

def scrapeFacebookPageFeedStatus(access_token): 

    query = "SELECT page_id FROM falken" 

    result_list = c.execute(query) 

    for single_row in result_list: 

     str_single_row = str(single_row) 

     str_norm_single_row = str_normalize(str_single_row) 

     print(str_norm_single_row) 

     statuses = getFacebookPageFeedData(str_norm_single_row, access_token, 100) 

     for status in statuses['data']: 

      query = "INSERT INTO falken_posts VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" 

      c.execute(query,(processFacebookPageFeedStatus(status, access_token))) 

      conn.commit() 

它僅通過的SINGLE_ROW給函數的第一值和所述循環停止。

getFacebookPageFeedData功能

def getFacebookPageFeedData(page_id, access_token, num_statuses): 

base = "https://graph.facebook.com/v2.6" 
node = "/%s/posts" % page_id 
fields = "/?fields=message,link,created_time,type,name,id," + \ 
     "comments.limit(0).summary(true),shares,reactions" + \ 
     ".limit(0).summary(true)" 
parameters = "&limit=%s&access_token=%s" % (num_statuses, access_token) 
url = base + node + fields + parameters 

# retrieve data 
data = json.loads(request_until_succeed(url)) 

return data 

它從頁的帖子Facebook的圖形API檢索數據。

processFacebookPageFeedStatus功能

def processFacebookPageFeedStatus(status, access_token): 

    status_id = status['id'] 
    status_message = '' if 'message' not in status.keys() else \ 
     unicode_normalize(status['message']) 
    link_name = '' if 'name' not in status.keys() else \ 
     unicode_normalize(status['name']) 
    status_type = status['type'] 
    status_link = '' if 'link' not in status.keys() else \ 
     unicode_normalize(status['link']) 

    status_published = datetime.datetime.strptime(
     status['created_time'],'%Y-%m-%dT%H:%M:%S+0000') 
    status_published = status_published + \ 
     datetime.timedelta(hours=-5) # EST 
    status_published = status_published.strftime(
     '%Y-%m-%d %H:%M:%S') 

    num_reactions = 0 if 'reactions' not in status else \ 
     status['reactions']['summary']['total_count'] 
    num_comments = 0 if 'comments' not in status else \ 
     status['comments']['summary']['total_count'] 
    num_shares = 0 if 'shares' not in status else status['shares']['count'] 

    reactions = getReactionsForStatus(status_id, access_token) if \ 
     status_published > '2016-02-24 00:00:00' else {} 

    num_likes = 0 if 'like' not in reactions else \ 
     reactions['like']['summary']['total_count'] 

    num_likes = num_reactions if status_published < '2016-02-24 00:00:00' \ 
     else num_likes 

IT賣場需要從狀態字典,並將其存儲到變量數據插入到數據庫中。

+0

什麼是「c」? –

+0

請修復您的代碼縮進。 – languitar

+0

@brunodesthuilliers c是一個sqlite遊標 – jeremybcenteno

回答

1

sqlite的cursor.execute()返回光標本身。所以這行之後:

result_list = c.execute(query) 

result_list實際上是c的別名。

現在你開始遍歷c

for single_row in result_list: 
    # code here 

,然後再次調用c.execute()

query = "INSERT INTO falken_posts VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" 
    c.execute(query,(processFacebookPageFeedStatus(status, access_token))) 

其丟棄c的前面的結果與這個新的查詢結果集。由於此查詢不會選擇任何內容,因此c將變爲空的迭代器,並且您的循環停在那裏。

固化是簡單明瞭:使用另一個光標插入查詢,這樣你就不會覆蓋c的結果集:

# create a second cursor for insert statements 
writer = conn.cursor() 
# no need to recreate this same string anew for each iteration, 
# we can as well define it here once for all 
insert_query = "INSERT INTO falken_posts VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" 

# no need for result_list - just iterate over `c` 
c.execute(query) 
for single_row in c: 
    # code here 
    writer.execute(insert_query,(processFacebookPageFeedStatus(status, access_token))) 

作爲一個側面說明,如果性能是一個問題,你可能還希望在整個循環之後提交一次,而不是在每個插入語句之後提交。

+0

這工作。非常感謝你。 – jeremybcenteno

+0

@JeremyCenteno很高興我能幫上忙 - 請不要忘記接受答案吧) –