在python中使用Wikipedia模块

我在python代码中使用Wikipedia模块。我希望用户输入要从Wikipedia中搜索并从其摘要中获得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

现在该怎么办?

iCMS 回答:在python中使用Wikipedia模块

这可能是由于互联网连接不畅/错误所致,

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

,

上述错误是由于Internet的连接问题所致。但是下面的代码有效

value=input("Enter the words: ")
try:
    m=wikipedia.search(value,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进行Web抓取并还原仅几行顶行,而不是总结。维基百科上的内容也可以每2小时更改一次

,

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

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 可以得到一个标题不明确的页面,这是该代码所依赖的系统。

本文链接:https://www.f2er.com/1645585.html

大家都在问