本文介绍了维基百科模块python:跳转"wikipedia.exceptions.PageError";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在尝试将Wikipedia摘要和主图像与csv文件中列出的每个物种名称相关联.我写这段代码:

I'm trying to associate to each species name listed in a csv file the wikipedia summary and main image.I write this code:

import csv
import wikipedia

wikipedia.set_lang('it')

with open('D:\\GIS\\Dati\\Vinca\\specie_vinca.csv', 'rt', encoding="utf8") as f:
    reader = csv.reader(f)
    for row in reader:
        wikipage = wikipedia.page(row)
        print (wikipage.title)
        print (wikipage.summary)
        print ("Page URL: %s" % wikipage.url)
        print ("Nr. of images on page: %d" % len(wikipage.images))
        print (" - Main Image: %s" % wikipage.images[0])
        print ("")

但是每次没有遇到种类名称时,脚本都会通过该消息"wikipedia.exceptions.PageError"停止运行.如何跳过这些记录以使脚本结束?

but each time it doesn't encounter a species name the script stops wiht this message "wikipedia.exceptions.PageError".How can I skip these records leaving the script finish?

推荐答案

如果页面不存在,则函数wikipedia.page将引发wikipedia.exceptions.PageError.这就是您要捕获的错误.

The function wikipedia.page will raise a wikipedia.exceptions.PageError if the page doesn't exist. That's the error you want to catch.

我会给你一个例子://也许对您有用

I will give you an example://maybe its useful to you

import wikipedia
links = ["CPython","no page"]
test=[]
for link in links:
    try:
        #try to load the wikipedia page
        page=wikipedia.page(link, auto_suggest=False)
        test.append(page)
    except wikipedia.exceptions.PageError:
        #if a "PageError" was raised, ignore it and continue to next link
        continue

这篇关于维基百科模块python:跳转"wikipedia.exceptions.PageError";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 13:12