【发布时间】:2020-09-23 10:50:45
【问题描述】:
这是我在 StackOverflow 上的第一个问题,所以如果我做错了什么,请告诉我...
我一直在做一些网页抓取,特别是在寻找某些音乐组在维基百科中所属的所有“类别”。我还是个新手,所以我可能会忽略一些非常基本的东西......
为此,我正在使用维基百科 API。它适用于某些乐队。例如,如果我寻找 Maroon 5
r = http.get("https://en.wikipedia.org/w/api.php?action=query&format=json&prop=categories&titles=Maroon_5")
r.json()
它返回这个 JSON 文件
{'continue': {'clcontinue': '512449|Articles_with_permanently_dead_external_links',
'continue': '||'},
'query': {'normalized': [{'from': 'Maroon_5', 'to': 'Maroon 5'}],
'pages': {'512449': {'pageid': 512449,
'ns': 0,
'title': 'Maroon 5',
'categories': [{'ns': 14,
'title': 'Category:1994 establishments in California'},
{'ns': 14, 'title': 'Category:222 Records artists'},
{'ns': 14, 'title': 'Category:A&M Records artists'},
{'ns': 14, 'title': 'Category:Adam Levine'},
{'ns': 14,
'title': 'Category:All Wikipedia articles needing words, phrases or quotes attributed'},
{'ns': 14, 'title': 'Category:All articles with dead external links'},
{'ns': 14, 'title': 'Category:American pop rock music groups'},
{'ns': 14, 'title': 'Category:American soul musical groups'},
{'ns': 14,
'title': 'Category:Articles with dead external links from February 2020'},
{'ns': 14, 'title': 'Category:Articles with hCards'}]}}}}
但是对于很多其他波段,例如“Active Child”,会返回这个文件:
r = requests.get("https://en.wikipedia.org/w/api.php?action=query&format=json&prop=categories&titles=Acid_Casuals")
r.json()
{'batchcomplete': '',
'query': {'pages': {'-1': {'ns': 0, 'title': 'Acid Casuals', 'missing': ''}}}}
我尝试用“_”替换“%20”,但没有任何变化。谁能告诉我发生了什么以及如何解决它?
在旁注中,我还注意到,即使对于 Maroon 5,json 中也缺少很多类别。我猜这可能是一个相关的问题。
如果一切都失败了,我会尝试深入研究 Beautifulsoup,但我仍然希望让这种方法发挥作用。
非常感谢您的帮助
【问题讨论】:
标签: python api web-scraping wikipedia-api