【问题标题】:Using wikipedia module in python在 python 中使用维基百科模块
【发布时间】:2020-09-01 06:05:30
【问题描述】:

我在我的 python 代码中使用维基百科模块。我希望用户输入从维基百科中搜索并从其摘要中获取 2 行。由于可能有很多同名的主题,所以我是这样使用的。

import wikipedia
value=input("Enter what u want to search")
m=wikipedia.search(value,3)
print(wikipedia.summary(m[0],sentences=2))

在执行此操作时,它显示了大约 3 页的异常。这有什么问题? 编辑: 正如@Ruperto 所建议的那样,我改变了这样的代码。

import wikipedia
import random
value=input("Enter the words: ")
try:
    p=wikipedia.page(value)
    print(p)
except wikipedia.exceptions.DisambiguationError as e:
    s=random.choice(e.options)
    p=wikipedia.summary(s,sentences=2)
    print(p)

现在我得到的错误是,

Traceback (most recent call last):   File "C:\Users\vdhan\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\connection.py", line 160, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw   File "C:\Users\vdhan\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\util\connection.py", line 84, in create_connection
    raise err   File "C:\Users\vdhan\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\util\connection.py", line 74, in create_connection
    sock.connect(sa) TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

During handling of the above exception, another exception occurred:

Traceback (most recent call last):   File "C:\Users\vdhan\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\connectionpool.py", line 677, in urlopen
    chunked=chunked, urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x03AEEAF0>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

现在该怎么办?

【问题讨论】:

  • 您输入了什么搜索词组?它显示了哪些异常?如果你pip install wikipedia 并运行上面的代码,它就可以正常工作,除了提示符中有拼写错误。
  • 如果我在维基百科下搜索标题中只有一页的名称,则不会出错。例如,如果我搜索“印度”,它会给出所需的输出。如果我输入“programming”,它会产生很多异常,因为有很多类型的页面都有这个标题。
  • 也许这会对stackoverflow.com/questions/25946692/… wikipedia.exceptions.DisambiguationError 有所帮助
  • 好像无法上网。您是否使用任何防火墙、代理或 VPN?
  • 不,我不使用这些

标签: python wikipedia-api


【解决方案1】:

正如您的错误所说,这可能是由于没有/网络连接不佳,

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

您可以更改/检查您的互联网连接,然后重试。两者都不是,这是您的 python 环境的问题。 我的实现是,

import warnings
warnings.filterwarnings("ignore")

import wikipedia
import random


value=input("Enter the words: ")
try:
    m=wikipedia.search(value,3)
    print(wikipedia.summary(m[0],sentences=2))
    # print(p)
except wikipedia.exceptions.DisambiguationError as e:
    s=random.choice(e.options)
    p=wikipedia.summary(s,sentences=2)
    print(p)

输出:

Enter the words: programming
Program management or programme management is the process of managing several related projects, often with the intention of improving an organization's performance. In practice and in its aims, program management is often closely related to systems engineering, industrial engineering, change management, and business transformation.

它在google colab中运行良好,我的实现colab文件你可以找到here

【讨论】:

    【解决方案2】:

    上述错误是由于互联网的连接问题。但是下面的代码有效

    value=input("Enter the words: ")
    try:
        m=wikipedia.search(value,3)
        print(wikipedia.summary(m[0],sentences=2))
    except wikipedia.exceptions.DisambiguationError as e:
        s=random.choice(e.options)
        p=wikipedia.summary(s,sentences=2)
        print(p)
    

    但是这里需要注意的是,由于这是更大代码块的一部分,因此最好使用任何 NLP 库进行抽象或提取摘要,因为 wikipdia 包仅使用 beautifulsoup 和 soupsieve 进行网络抓取并恢复只是几条顶线,在某种程度上不是总结。维基百科上的内容也可以每 2 小时更改一次

    【讨论】:

    • 问题是我的代码是一个猜谜游戏,所以可以给出任何主题的任何单词(例如电影、食物)所以我认为维基百科模块会更好。
    【解决方案3】:

    我遇到了类似的问题,经过大量的挠头和谷歌搜索,找到了这个解决方案:

    import wikipediaapi as api
    import wikipedia as wk
    
    # Wikipediaapi 'initialization'
    wiki_wiki = api.Wikipedia('en')
    
    
    # Getting fixed number of sentences from summary
    def summary(pg, sentences=5):
        summ = pg.summary.split('. ')
        summ = '. '.join(summ[:sentences])
        summ += '.'
        return summ
    
    
    s_term = 'apple'# Any term, ambiguous or not
    wk_res = wk.search(s_term)
    page = wiki_wiki.page(wk_res[0])
    print("Page summary", summary(page))
    

    基本上,据我所见,仅使用 wikipedia 模块并没有得到很好的解决方案。 例如,如果我要搜索“印度”,我将永远无法找到印度这个国家的页面,而这正是我想要的。 发生这种情况是因为印度(国家)的维基百科页面的标题只是标题为“印度”。但是,由于它可以引用的事物的数量,该标题是无效的。这种情况也适用于很多其他事情。

    然而,wiki_wiki_.page 可以得到一个标题不明确的页面,这是这段代码所依赖的系统。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多