Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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 从WikipediaAPI的json输出中提取文本_Python_Json_Wikipedia Api - Fatal编程技术网

Python 从WikipediaAPI的json输出中提取文本

Python 从WikipediaAPI的json输出中提取文本,python,json,wikipedia-api,Python,Json,Wikipedia Api,我在用Python解析WikipediaAPI的输出时遇到问题。像很多人一样,我想分离出一些文本。但是,我不想使用python wikipedia扩展来解析Wiki,因为我希望能够使用其他API并解析它们的数据 到目前为止,我有: # Import requests package import requests import pprint # Assign URL to variable: url url = 'https://en.wikipedia.org/w/api.php?actio

我在用Python解析WikipediaAPI的输出时遇到问题。像很多人一样,我想分离出一些文本。但是,我不想使用python wikipedia扩展来解析Wiki,因为我希望能够使用其他API并解析它们的数据

到目前为止,我有:

# Import requests package
import requests
import pprint

# Assign URL to variable: url
url = 'https://en.wikipedia.org/w/api.php?action=parse&prop=wikitext&format=json&page=norway&section=7'

# Package the request, send the request and catch the response: r
r = requests.get(url)

# Decode the JSON data into a dictionary: json_data
json_data = r.json()
pprint.pprint(json_data)

# Print the Wikipedia page text
norway_text = json_data['parse']['wikitext']
pprint.pprint(norway_text)
输出:

{'parse': {'pageid': 21241,
           'title': 'Norway',
           'wikitext': {'*': '=== Viking Age ===\n'
                             '[[File:Le bateau viking dOseberg '
                             '(4835828216).jpg|thumb|upright|The [[Oseberg '
                             'ship]] at the [[Viking Ship Museum (Oslo)|Viking '
                             'Ship Museum]] in [[Oslo]], Norway]]\n'
                             '{{Main article|Viking Age}}\n'
                             '{{See also|Unification of Norway|Hereditary '
                             'Kingdom of Norway}}\n'
                             '[[File:Gjermundbu helmet - '
                             'cropped.jpg|thumb|left|upright=0.7|The '
                             '[[Gjermundbu helmet]] found in [[Buskerud]] is '
                             'the only known reconstructable [[Viking Age]] '
                             'helmet]]\n'
                             '\n'
                             'From the 8th to the 10th century, the wider '
                             'Scandinavian region was the source of '
                             '[[Viking]]s. The looting of the monastery at '
                             '[[Lindisfarne]] in Northeast England in 793 by '
                             '[[Norsemen|Norse people]] has long been regarded '
                             'as the event which marked the beginning of the '
                             '[[Viking Age]].<ref>{{cite web '
                             '|url=http://loststory.net/node/91 |title=Age of '
                             'the vikings |publisher=loststory|accessdate=17 '
                             'February 2015}}</ref> This age was characterised '
                             'by expansion and emigration by Viking '
                             '[[seafarer]]s. They [[colonise]]d, raided, and '
                             'traded in all parts of Europe. Norwegian Viking '
                             'explorers first discovered [[Iceland]] by '
                             'accident in the 9th century when heading for the '
                             '[[Faroe Islands]], and eventually came across '
                             '[[Vinland]], known today as [[Newfoundland '
                             '(island)|Newfoundland]], in [[Canada]]. The '
                             'Vikings from Norway were most active in the '
                             'northern and western [[British Isles]] and '
                             'eastern [[North America]] isles.<ref>{{cite '
                             'web|url=https://naturalhistory.si.edu/vikings/voyage/subset/vinland/archeo.html|title=Vinland '
                             'Archeology|website=naturalhistory.si.edu|access-date=11 '
                             'April 2017}}</ref>\n'
                             '\n'
                             'According to tradition, [[Harald Fairhair]] '
                             'unified them into one in 872 after the [[Battle '
                             'of Hafrsfjord]] in [[Stavanger]], thus becoming '
                             'the first king of a united '
                             'Norway.<ref>[[#Larsen|Larsen]], p. 83.</ref> '
                             "Harald's realm was mainly a South Norwegian "
                             'coastal state. Fairhair ruled with a strong hand '
                             'and according to the sagas, many Norwegians left '
                             'the country to live in Iceland, the [[Faroe '
                             'Islands]], [[Greenland]], and parts of [[Great '
                             'Britain|Britain]] and [[Ireland]]. The '
                             'modern-day Irish cities of [[Dublin]], '
                             '[[Limerick]] and [[Waterford]] were founded by '
                             'Norwegian settlers.<ref>[[R. F. Foster '
                             "(historian)|Foster, R. F.]] (2001) ''The Oxford "
                             "History of Ireland''. Oxford University Press. "
                             '{{ISBN|0-19-280202-X}}</ref>\n'
                             '\n'
                             '[[File:Züge, Landnahmen und Siedlungsgebiete der '
                             'Nordmänner - '
                             '800-1050.png|thumb|upright=1.2|Norwegian, Danish '
                             'and Swedish expansion during the Viking age '
                             'between 800–1050]]\n'
                             '\n'
                             '[[Norse mythology|Norse traditions]] were slowly '
                             'replaced by [[Christian mythology|Christian '
                             'ones]] in the late 10th and early 11th '
                             'centuries. One of the most important sources for '
                             'the history of the 11th century Vikings is the '
                             'treaty between the Icelanders and Olaf '
                             'Haraldsson, king of Norway circa 1015 to '
                             '1028.<ref>Jones, Gwyn, A history of the Vikings '
                             '(Oxford 2001).</ref> This is largely attributed '
                             'to the missionary kings [[Olaf I of Norway|Olav '
                             'Tryggvasson]] and [[Olaf II of Norway|St. '
                             'Olav]]. [[Haakon I of Norway|Haakon the Good]] '
                             "was Norway's first Christian king, in the "
                             'mid-10th century, though his attempt to '
                             'introduce the religion was rejected. Born '
                             'sometime in between 963–969, Olav Tryggvasson '
                             'set off raiding in [[England]] with 390 ships. '
                             'He attacked [[London]] during this raiding. '
                             'Arriving back in Norway in 995, Olav landed in '
                             '[[Moster (island)|Moster]]. There he built a '
                             'church which became the first [[Old Moster '
                             'Church|Christian church]] ever built in Norway. '
                             'From Moster, Olav sailed north to [[Trondheim]] '
                             'where he was proclaimed King of Norway by the '
                             'Eyrathing in 995.<ref>[[#Larsen|Larsen]], p. '
                             '95.</ref>\n'
                             '\n'
                             '[[Feudalism]] never really developed in Norway '
                             'or Sweden, as it did in the rest of Europe. '
                             'However, the administration of government took '
                             'on a very conservative feudal character. The '
                             '[[Hanseatic League]] forced the royalty to cede '
                             'to them greater and greater concessions over '
                             'foreign trade and the economy. The League had '
                             'this hold over the royalty because of the loans '
                             'the Hansa had made to the royalty and the large '
                             "debt the kings were carrying. The League's "
                             'monopolistic control over the economy of Norway '
                             'put pressure on all classes, especially the '
                             'peasantry, to the degree that no real burgher '
                             'class existed in Norway.<ref>[[#Larsen|Larsen]], '
                             'p. 201.</ref>'}}}
{'*': '=== Viking Age ===\n'
      '[[File:Le bateau viking dOseberg (4835828216).jpg|thumb|upright|The '
      '[[Oseberg ship]] at the [[Viking Ship Museum (Oslo)|Viking Ship '
      'Museum]] in [[Oslo]], Norway]]\n'
      '{{Main article|Viking Age}}\n'
      '{{See also|Unification of Norway|Hereditary Kingdom of Norway}}\n'
      '[[File:Gjermundbu helmet - cropped.jpg|thumb|left|upright=0.7|The '
      '[[Gjermundbu helmet]] found in [[Buskerud]] is the only known '
      'reconstructable [[Viking Age]] helmet]]\n'
      '\n'
      'From the 8th to the 10th century, the wider Scandinavian region was the '
      'source of [[Viking]]s. The looting of the monastery at [[Lindisfarne]] '
      'in Northeast England in 793 by [[Norsemen|Norse people]] has long been '
      'regarded as the event which marked the beginning of the [[Viking '
      'Age]].<ref>{{cite web |url=http://loststory.net/node/91 |title=Age of '
      'the vikings |publisher=loststory|accessdate=17 February 2015}}</ref> '
      'This age was characterised by expansion and emigration by Viking '
      '[[seafarer]]s. They [[colonise]]d, raided, and traded in all parts of '
      'Europe. Norwegian Viking explorers first discovered [[Iceland]] by '
      'accident in the 9th century when heading for the [[Faroe Islands]], and '
      'eventually came across [[Vinland]], known today as [[Newfoundland '
      '(island)|Newfoundland]], in [[Canada]]. The Vikings from Norway were '
      'most active in the northern and western [[British Isles]] and eastern '
      '[[North America]] isles.<ref>{{cite '
      'web|url=https://naturalhistory.si.edu/vikings/voyage/subset/vinland/archeo.html|title=Vinland '
      'Archeology|website=naturalhistory.si.edu|access-date=11 April '
      '2017}}</ref>\n'
      '\n'
      'According to tradition, [[Harald Fairhair]] unified them into one in '
      '872 after the [[Battle of Hafrsfjord]] in [[Stavanger]], thus becoming '
      'the first king of a united Norway.<ref>[[#Larsen|Larsen]], p. 83.</ref> '
      "Harald's realm was mainly a South Norwegian coastal state. Fairhair "
      'ruled with a strong hand and according to the sagas, many Norwegians '
      'left the country to live in Iceland, the [[Faroe Islands]], '
      '[[Greenland]], and parts of [[Great Britain|Britain]] and [[Ireland]]. '
      'The modern-day Irish cities of [[Dublin]], [[Limerick]] and '
      '[[Waterford]] were founded by Norwegian settlers.<ref>[[R. F. Foster '
      "(historian)|Foster, R. F.]] (2001) ''The Oxford History of Ireland''. "
      'Oxford University Press. {{ISBN|0-19-280202-X}}</ref>\n'
      '\n'
      '[[File:Züge, Landnahmen und Siedlungsgebiete der Nordmänner - '
      '800-1050.png|thumb|upright=1.2|Norwegian, Danish and Swedish expansion '
      'during the Viking age between 800–1050]]\n'
      '\n'
      '[[Norse mythology|Norse traditions]] were slowly replaced by '
      '[[Christian mythology|Christian ones]] in the late 10th and early 11th '
      'centuries. One of the most important sources for the history of the '
      '11th century Vikings is the treaty between the Icelanders and Olaf '
      'Haraldsson, king of Norway circa 1015 to 1028.<ref>Jones, Gwyn, A '
      'history of the Vikings (Oxford 2001).</ref> This is largely attributed '
      'to the missionary kings [[Olaf I of Norway|Olav Tryggvasson]] and '
      '[[Olaf II of Norway|St. Olav]]. [[Haakon I of Norway|Haakon the Good]] '
      "was Norway's first Christian king, in the mid-10th century, though his "
      'attempt to introduce the religion was rejected. Born sometime in '
      'between 963–969, Olav Tryggvasson set off raiding in [[England]] with '
      '390 ships. He attacked [[London]] during this raiding. Arriving back in '
      'Norway in 995, Olav landed in [[Moster (island)|Moster]]. There he '
      'built a church which became the first [[Old Moster Church|Christian '
      'church]] ever built in Norway. From Moster, Olav sailed north to '
      '[[Trondheim]] where he was proclaimed King of Norway by the Eyrathing '
      'in 995.<ref>[[#Larsen|Larsen]], p. 95.</ref>\n'
      '\n'
      '[[Feudalism]] never really developed in Norway or Sweden, as it did in '
      'the rest of Europe. However, the administration of government took on a '
      'very conservative feudal character. The [[Hanseatic League]] forced the '
      'royalty to cede to them greater and greater concessions over foreign '
      'trade and the economy. The League had this hold over the royalty '
      'because of the loans the Hansa had made to the royalty and the large '
      "debt the kings were carrying. The League's monopolistic control over "
      'the economy of Norway put pressure on all classes, especially the '
      'peasantry, to the degree that no real burgher class existed in '
      'Norway.<ref>[[#Larsen|Larsen]], p. 201.</ref>'}
{'parse':{'pageid':21241,
‘标题’:‘挪威’,
'wikitext':{'*':'==海盗时代====\n'
“[[文件:Le bateau viking dOseberg'
“(48358216).jpg |拇指|直立| The[[Oseberg'
[维京人船只博物馆(奥斯陆)|维京人'
“挪威[[奥斯陆]]的船舶博物馆]]\n”
“{{主要文章|维京时代}}\n”
“{{另见|挪威统一|世袭”
'挪威王国}}}\n'
“[[文件:Gjermundbu头盔-”
'crapped.jpg | thumb | left | right=0.7 | The'
“[[Buskerud]]发现的[[Gjermundbu头盔]]是”
“唯一已知的可重建[[维京时代]]”
'头盔]]\n'
“\n”
“从8世纪到10世纪,范围更广”
“斯堪的纳维亚地区是
“[[Viking]]s.对圣母院的抢劫”
“[[Lindisfarne]]于793年在英格兰东北部被”
“[[挪威人]长期以来一直被视为”
'作为标志着新世纪开始的事件'
“[[Viking Age].{{引用web'
|网址=http://loststory.net/node/91 |title=年龄'
“维京人|出版商=失败者|访问日期=17”
“2015年2月}这一年龄的特点”
“维京人的扩张和移民”
“[[海员]]s.他们[[殖民]]d,袭击,并
“在欧洲各地交易。挪威维京人”
“探险家第一次发现[[冰岛]]是在”
“9世纪驶向欧洲时发生的意外”
“[[Faroe Islands]],并最终遇到”
“[[Vinland]],今天被称为[[Newfoundland]
“(岛)|纽芬兰]],位于[[加拿大]。”
“来自挪威的维京人在这场战争中最为活跃”
“北部和西部[[不列颠群岛]]和”
“东[[北美]]群岛{{引述}”
“web | url=https://naturalhistory.si.edu/vikings/voyage/subset/vinland/archeo.html|标题=文兰'
“考古学|网站=naturalhistory.si.edu |访问日期=11”
'2017年4月}}}\n'
“\n”
“根据传统,[[Harald Fairhair]]
“在[[战斗]之后,他们在872年合并为一个”
在[[Stavanger]]中的'of Hafrsfjord]],因此成为'
“联合王国的第一位国王”
“挪威。[[#Larsen | Larsen]],第83页。”
“哈拉尔德的领地主要是南挪威”
“沿海州。金发碧眼的人用有力的手段统治”
“根据传说,许多挪威人离开了”
“居住在冰岛的国家,[[Faroe]
“岛屿”]、[[格陵兰岛]]和[[大岛]的部分地区
“不列颠|不列颠]]和[[爱尔兰]]
“都柏林的现代爱尔兰城市”
“[[Limerick]]和[[Waterford]]是由”
“挪威定居者。[[R.F.福斯特”
“(历史学家)|福斯特,R.F.]](2001)“牛津”
《爱尔兰历史》。牛津大学出版社
“{ISBN | 0-19-280202-X}}\n”
“\n”
“[[档案:苏格、兰德那曼和西德隆斯盖比特”
“诺德姆纳——”
“800-1050.png |拇指|直立=1.2 |挪威语、丹麦语”
“维京时代瑞典的扩张”
'介于800–1050之间]]\n'
“\n”
“[[挪威神话|挪威传统]]正在慢慢地”
'替换为[[基督教神话|基督教'
在10月底和11月初
“几个世纪。这是最重要的信息来源之一”
“11世纪维京人的历史是最重要的”
“冰岛人和奥拉夫之间的条约”
“哈拉尔松,挪威国王,约公元1015年至公元1015年”
1028年。琼斯,格温,维京人的历史
(牛津2001)。这主要归因于
“致传教士国王[[Olaf I of Norway | Olav]
"Tryggvasson"和"挪威奥拉夫二世|圣""
“奥拉夫].[Haakon I of Norway | Haakon the Good]]
是挪威第一位基督教国王
“10世纪中期,尽管他试图”
介绍宗教被拒绝
txt = json_data['parse']['wikitext']['*']
urls = []

while "url=" in txt:
    start = txt.find("url=") + 4
    end = txt.find(' ', start)

    url = txt[start:end]
    urls.append(url)

    txt = txt[end:]