问题描述
我正在尝试构建一个循环,为我提供一个类别的所有 pageid。因此,我尝试按预期使用“continue”参数。
import requests
URL = "https://fr.wikipedia.org/w/api.PHP"
ParaMS_FRANCE = {
"action": "query","cmtitle": 'Catégorie:Portail:France/Articles liés',"list": "categorymembers","cmlimit": 500,"format": "json"
}
R = S.get(url=URL,params=ParaMS_FRANCE)
DATA = R.json()
PAGES_FRANCE = DATA['query']['categorymembers']
idx_continue = DATA['continue']
但是,当它进入循环时:
while('continue' in DATA):
ParaMS_FRANCE = {
"action": "query","continue": idx_continue,"format": "json"
}
R = S.get(url=URL,params=ParaMS_FRANCE)
DATA = R.json()
PAGES_FRANCE = DATA['query']['categorymembers']
idx_continue = DATA['continue']
当我打印 DATA
时它返回以下错误:
{
"error":{
"code":"badcontinue","info":"Invalid continue param. You should pass the original value returned by the prevIoUs query.","*":"See https://fr.wikipedia.org/w/api.PHP for API usage. Subscribe to the mediawiki-api-announce mailing list at <https://lists.wikimedia.org/mailman/listinfo/mediawiki-api-announce&gt; for notice of API deprecations and breaking changes."
},"servedby":"mw1390"
}
除了我提供了 continue
的所有内容之外,还有什么问题?
谢谢
解决方法
使用延续参数的正确方法是将它们与原始参数合并(即PARAMS_FRANCE.update(DATA['continue'])
)。