我正在查看Wikipedia文章上的标题(每个图像下方的文字)。我希望解析这些字符串(主要使用正则表达式),然后如果匹配,则要保存该图像的链接。
我一直在直接导入Wikipedia来解析文本,但是在网上浏览后,我发现我需要另一种解析器。我尝试使用mwparserfromhell和pywikibot,但是我无法为我解决pywikibot错误,只是mwparserfromhell给了我空的结果。
在不使用DBPpedia的情况下完成上述操作的任何帮助吗?
这是我写的要做的这件事
#!/usr/bin/python3
"""
parse.py
MediaWiki API Demos
Demo of `Parse` module: Parse content of a page
MIT License
"""
import requests
from pprint import pprint
S = requests.Session()
URL = "https://en.wikipedia.org/w/api.php"
page_title= "Photosynthesis"
PARAMS = {
"action": "parse",
"page": page_title,
"format": "json"
}
R = S.get(url=URL, params=PARAMS)
DATA = R.json()
page = (DATA["parse"]["text"]["*"])
from bs4 import BeautifulSoup
soup = BeautifulSoup(page, 'html.parser')
thumb_divs = soup.findAll("div", {"class": "thumbinner"})
images = []
for div in thumb_divs:
image = div.findAll("img")[0]['src']
caption = div.findAll("div")[0].text
image_and_caption = {
'image_url' : image,
'image_caption' : caption
}
images.append(image_and_caption)
return_value = {'term' : page_title, 'images' : images }
pprint(return_value)