2024-10-01 13:38:23 发布
网友
我正在查看维基百科文章的标题(每张图片下面的文字)。我希望解析这些字符串(主要使用regex),然后如果匹配,我希望保存该图像的链接。你知道吗
我一直在直接导入维基百科来解析文本,但是环顾网络之后,我发现我需要一种不同的解析器。我尝试使用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)
这是我写的一些东西
相关问题 更多 >
编程相关推荐