维基媒体页面到 Python 中的文本

发布于 2024-09-15 20:27:03 字数 49 浏览 5 评论 0原文

我想将使用 API 提取的维基百科内容转换为纯文本。

有什么提示吗?

I would like to convert a wikipedia content extracted with API to plain text.

Any tip??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荭秂 2024-09-22 20:27:03

据说有一些 python mediawiki 标记解析器/渲染器,并且您几乎可以将 HTML 转换为您需要的样式的纯文本。但不知道这实际上效果如何。

There are supposedly some python mediawiki markup parsers/renderers, and you can pretty much convert from HTML into plaintext of the style you need. Don't know how well that would actually work, though.

水溶 2024-09-22 20:27:03

我几天前做这个是为了克隆一个维基媒体网站

import re
from mediawikitools import *
import os
from sys import argv

def list_all_pages(site):
    query_results = api.APIRequest(site, {'action':'query', 'list':'allpages', 'aplimit':'500'}).query()
    results = query_results['query']['allpages']
    return results

def clone(site):
    if not os.path.exists(site.siteinfo['sitename'][:20]):
        print 'Make Dir', site.siteinfo['sitename'][:20]
        os.makedirs(site.siteinfo['sitename'][:20])
    index = open(site.siteinfo['sitename'][:20] + '/' + 'Index','w')

    pages = list_all_pages(site)
    for test_page in pages:
        if test_page['title'].rfind('/') != -1 and not os.path.exists(site.siteinfo['sitename'][:20] + '/' + test_page['title'][:test_page['title'].rfind('/')+1]):
            #print test_page['title'][:test_page['title'].rfind('/')+1]
            os.makedirs(site.siteinfo['sitename'][:20] + '/' + test_page['title'][:test_page['title'].rfind('/')+1])
        page_file = open(site.siteinfo['sitename'][:20] + '/' + test_page['title']+'.wiki', 'w')
        try:
            index.write(site.siteinfo['sitename'][:20] + '/' + test_page['title']+'.wiki')
            wiki_file = page.Page(site, test_page['title'])
            print site.siteinfo['sitename'][:20] + '/' + test_page['title']+'.wiki'
            page_file.write(wiki_file.getWikiText())
        except KeyError, e:
            print e
        except UnicodeEncodeError, e:
            print e

if __name__ == '__main__':
    site = wiki.Wiki("http://localhost/wiki/api.php")
    site.setUserAgent('Mozilla/6.0 (Windows NT 6.2; WOW64; rv:16.0.1) Gecko/20121011 Firefox/16.0.1')
    print site.siteinfo['sitename']
    clone(site)


    #site.login(username, password, force=true) if you need a username and password to acess it

I made this a few days ago to clone a Wikimedia site

import re
from mediawikitools import *
import os
from sys import argv

def list_all_pages(site):
    query_results = api.APIRequest(site, {'action':'query', 'list':'allpages', 'aplimit':'500'}).query()
    results = query_results['query']['allpages']
    return results

def clone(site):
    if not os.path.exists(site.siteinfo['sitename'][:20]):
        print 'Make Dir', site.siteinfo['sitename'][:20]
        os.makedirs(site.siteinfo['sitename'][:20])
    index = open(site.siteinfo['sitename'][:20] + '/' + 'Index','w')

    pages = list_all_pages(site)
    for test_page in pages:
        if test_page['title'].rfind('/') != -1 and not os.path.exists(site.siteinfo['sitename'][:20] + '/' + test_page['title'][:test_page['title'].rfind('/')+1]):
            #print test_page['title'][:test_page['title'].rfind('/')+1]
            os.makedirs(site.siteinfo['sitename'][:20] + '/' + test_page['title'][:test_page['title'].rfind('/')+1])
        page_file = open(site.siteinfo['sitename'][:20] + '/' + test_page['title']+'.wiki', 'w')
        try:
            index.write(site.siteinfo['sitename'][:20] + '/' + test_page['title']+'.wiki')
            wiki_file = page.Page(site, test_page['title'])
            print site.siteinfo['sitename'][:20] + '/' + test_page['title']+'.wiki'
            page_file.write(wiki_file.getWikiText())
        except KeyError, e:
            print e
        except UnicodeEncodeError, e:
            print e

if __name__ == '__main__':
    site = wiki.Wiki("http://localhost/wiki/api.php")
    site.setUserAgent('Mozilla/6.0 (Windows NT 6.2; WOW64; rv:16.0.1) Gecko/20121011 Firefox/16.0.1')
    print site.siteinfo['sitename']
    clone(site)


    #site.login(username, password, force=true) if you need a username and password to acess it
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文