Parse wikitext myself with python - KeyError: 'revisions'
Parse wikitext myself with python - KeyError: 'revisions'
我是 python 新手。我从 here 中获取了这段代码,但我得到了这个 KeyError:
我只想说它有效,所以我不知道发生了什么。
wikicode = page['revisions'][0]['*']
KeyError: 'revisions'
这是代码,感谢您的帮助!
def request_wiki_value(title=None, sentence=''):
if title is None:
title = input("No title entered.\nPlease enter a title: ")
import requests
import mwparserfromhell
response = requests.get(
'https://en.wikipedia.org/w/api.php',
params={
'action': 'query',
'format': 'json',
'titles': title,
'prop': 'revisions',
'rvprop': 'content',
}
).json()
page = next(iter(response['query']['pages'].values()))
wikicode = page['revisions'][0]['*']
parsed_wikicode = mwparserfromhell.parse(wikicode)
# open("article.txt", "a", encoding='utf-8').write(parsed_wikicode.strip_code())
for ch in parsed_wikicode.strip_code():
sentence = sentence + ch
if ch == '\n':
sentence = sentence.removesuffix(ch)
open("article.txt", "a", encoding='utf-8').write(sentence)
sentence = ''
试试这个:
import requests
import mwparserfromhell
def request_wiki_value(title=None, sentence=''):
if title is None:
title = input("No title entered.\nPlease enter a title: ")
response = requests.get(
'https://en.wikipedia.org/w/api.php',
params={
'action': 'query',
'format': 'json',
'titles': title,
'prop': 'revisions',
'rvprop': 'content',
}
).json()
page = next(iter(response['query']['pages'].values()))
wikicode = page['revisions'][0]['*']
parsed_wikicode = mwparserfromhell.parse(wikicode)
# open("article.txt", "a", encoding='utf-8').write(parsed_wikicode.strip_code())
for ch in parsed_wikicode.strip_code():
sentence = sentence + ch
if ch == '\n':
sentence = sentence.removesuffix(ch)
open("article.txt", "a", encoding='utf-8').write(sentence)
sentence = ''
问题是你没有为函数体添加缩进
我是 python 新手。我从 here 中获取了这段代码,但我得到了这个 KeyError: 我只想说它有效,所以我不知道发生了什么。
wikicode = page['revisions'][0]['*']
KeyError: 'revisions'
这是代码,感谢您的帮助!
def request_wiki_value(title=None, sentence=''):
if title is None:
title = input("No title entered.\nPlease enter a title: ")
import requests
import mwparserfromhell
response = requests.get(
'https://en.wikipedia.org/w/api.php',
params={
'action': 'query',
'format': 'json',
'titles': title,
'prop': 'revisions',
'rvprop': 'content',
}
).json()
page = next(iter(response['query']['pages'].values()))
wikicode = page['revisions'][0]['*']
parsed_wikicode = mwparserfromhell.parse(wikicode)
# open("article.txt", "a", encoding='utf-8').write(parsed_wikicode.strip_code())
for ch in parsed_wikicode.strip_code():
sentence = sentence + ch
if ch == '\n':
sentence = sentence.removesuffix(ch)
open("article.txt", "a", encoding='utf-8').write(sentence)
sentence = ''
试试这个:
import requests
import mwparserfromhell
def request_wiki_value(title=None, sentence=''):
if title is None:
title = input("No title entered.\nPlease enter a title: ")
response = requests.get(
'https://en.wikipedia.org/w/api.php',
params={
'action': 'query',
'format': 'json',
'titles': title,
'prop': 'revisions',
'rvprop': 'content',
}
).json()
page = next(iter(response['query']['pages'].values()))
wikicode = page['revisions'][0]['*']
parsed_wikicode = mwparserfromhell.parse(wikicode)
# open("article.txt", "a", encoding='utf-8').write(parsed_wikicode.strip_code())
for ch in parsed_wikicode.strip_code():
sentence = sentence + ch
if ch == '\n':
sentence = sentence.removesuffix(ch)
open("article.txt", "a", encoding='utf-8').write(sentence)
sentence = ''
问题是你没有为函数体添加缩进