我在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
现在该怎么办?