Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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 执行函数时循环中断_Python_Python 3.x - Fatal编程技术网

Python 执行函数时循环中断

Python 执行函数时循环中断,python,python-3.x,Python,Python 3.x,我的代码如下: 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_nor

我的代码如下:

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)
当我执行上面的代码时,它会显示结果列表中的每一行值

但当我将单行传递给如下函数时:

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()
它只将单行的第一个值传递给函数,循环停止

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
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
它从Facebook Graph API检索页面帖子的数据

processFacebookPageFeedStatus功能

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
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
def processFacebookPageFeedStatus(状态,访问令牌):
status_id=status['id']
状态\消息=“”如果“消息”不处于状态。keys()否则\
unicode_规范化(状态['message'])
如果“name”不处于状态,则link_name=''。keys()否则\
unicode_规范化(状态['name'])
状态\类型=状态['type']
如果'link'未处于状态,则状态为\'link=''。keys()否则\
unicode_规范化(状态['link'])
状态_published=datetime.datetime.strtime(
状态['created_time'],“%Y-%m-%dT%H:%m:%S+0000”)
状态_已发布=状态_已发布+\
datetime.timedelta(小时=-5)#EST
status_published=status_published.strftime(
“%Y-%m-%d%H:%m:%S”)
如果“反应”不处于其他状态,则num_reactions=0\
状态['reactions']['summary']['total_count']
如果“注释”不处于其他状态,则num_comments=0\
状态['comments']['summary']['total_count']
num_shares=0,如果“shares”未处于其他状态['shares']['count']
反应=getReactionsForStatus(状态\u id,访问\u令牌)如果\
状态发布>'2016-02-24 00:00:00'其他{}
num_likes=0,如果“like”不在其他选项中\
反应['like']['summary']['total_count']
num_likes=发布状态时的num_反应<'2016-02-24 00:00:00'\
还有你喜欢的吗

它存储来自status字典的所需数据,并将其存储到变量中以插入数据库。

sqlite的
游标。execute()
返回游标本身。因此,在这一行之后:

result_list = c.execute(query)
result\u 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)))

作为补充说明,如果性能是一个问题,您也可能只希望在整个循环之后提交一次,而不是在每个insert语句之后提交一次。

什么是“c”??请修复代码缩进。@Brunodesshuilliers c是sqlite游标什么是
getFacebookPageFeedData
processFacebookPageFeedStatus
?请编辑您的帖子,给出一个答案。现在还不清楚这里到底出了什么问题,代码中有很多未知项,还有不正确的缩进。@Langitar缩进很好,当我执行代码时,我不知道IndentationError@JeremyCenteno很高兴我能帮上忙——请别忘了接受回复;)