本文收集整理关于python异常处理的主动抛出的相关议题,使用内容导航快速到达。
内容导航:
Q1:python3 异常处理里面的 异常参数
参数前面加as.....
Q2:python怎么实现数据的异常值的处理
# coding=utf-8、
import re
import requests
from lxml import etree
import pymongo
import sys
reload(sys)
sys.setdefaultencoding(utf-8)
def getpages(url, total):
nowpage = int(re.search((\d+), url, re.S).group(1))
urls = []
for i in range(nowpage, total + 1):
link = re.sub((\d+), %s % i, url, re.S)
urls.append(link)
return urls
def spider(url):
html = requests.get(url)
selector = etree.HTML(html.text)
book_name = selector.xpath(//*[@id="container"]/ul/li//div/div[2]/a/text())
book_author = selector.xpath(//*[@id="container"]/ul/li//div/div[2]/div/a/text())
saveinfo(book_name, book_author)
def saveinfo(book_name, book_author):
connection = pymongo.MongoClient()
BookDB = connection.BookDB
BookTable = BookDB.books
length = len(book_name)
for i in range(0, length):
books = {}
books[name] = str(book_name[i]).replace(\n,)
books[author] = str(book_author[i]).replace(\n,)
BookTable.insert_one(books)
if __name__ == __main__:
url = http://readfree.me/shuffle/?page=1
urls = getpages(url,3)
for each in urls:
spider(each)
Q3:关于python教程中处理异常的问题
Python3.1 + win 里面,下面这个程序是可以响应EOF错误的:#!/usr/bin/python
# Filename: try_except.pyimport systry:
s = input(Enter something --> )
except EOFError:
print(\nWhy did you do an EOF on me?)
sys.exit() # exit the program
except:
print(e)
print(\nSome error/exception occurred.)
print(Done)
\n是换行啊,你写个简单的例子就知道了:print("abc")
print("def")执行以后得到的结果是:abcdef如果改成print("abc\n")
print("def")那么得到的结果是:abc# 一个空行defwwW.YIJiTa※O.COM
Q4:python中抛出异常处理
try先输出1:print(1),然后抛出ValueError异常,直接跳到except ValueError:print(3),输出3,finally作为无论如何必须要执行的语句,输出6:print(6).当抛出Exception异常时会输出4.(应该是这样的)