【问题标题】:Caption of images on wikipedia pages维基百科页面上的图像标题
【发布时间】:2018-01-02 19:34:55
【问题描述】:

我正在查看维基百科文章的标题(每张图片下方的文字)。我希望解析这些字符串(主要使用正则表达式),然后如果匹配,我想保存该图像的链接。

我一直在直接导入 wikipedia 来解析文本,但是在环顾网络之后,我发现我需要一种不同类型的解析器。我尝试使用 mwparserfromhell 和 pywikibot,但我无法为我解决 pywikibot 错误,只是 mwparserfromhell 给了我空的结果。

在不使用 DBPpedia 的情况下执行上述操作有什么帮助吗?

【问题讨论】:

  • 你试过维基百科转储吗?我向您推荐 elasticsearch dumps.wikimedia.org/other/cirrussearch/current 的特定转储。您可以在那里索引所有维基百科文章并使用多种语言的 api 在 es 上进行操作。 es 的 python api 很棒,你可以在代码中集成每个 python 解析器
  • 我遇到了同样的问题。你有没有找到解决这个@someone1 的方法?

标签: python text nlp wikipedia


【解决方案1】:

这是我写的东西

#!/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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-15
    • 1970-01-01
    • 2010-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多