python - Python - 处理多处理 Pool.map 中的异常
问题描述
我有以下代码,但是当我尝试它时会引发我什至处理过的那些错误try except
from multiprocessing.dummy import Pool as ThreadPool
def getPrice(product='',listing=False):
try:
avail = soup.find('div',id='availability').get_text().strip()
except:
avail = soup.find('span',id='availability').get_text().strip()
pool.map(getPrice, list_of_hashes)
它给了我以下错误
Traceback (most recent call last):
File "C:\Users\Anonymous\Desktop\Project\google spreadsheet\project.py", line 4, in getPrice
avail = soup.find('div',id='availability').get_text().strip()
AttributeError: 'NoneType' object has no attribute 'get_text'
解决方案
avail = soup.find('span',id='availability').get_text().strip()
在except
语句内部,因此不在您的函数内部处理
更好地循环属性并在未找到时返回默认值:
def getPrice(product='',listing=False):
for p in ['div','span']:
try:
# maybe just checking for not None would be enough
avail = soup.find(p,id='availability').get_text().strip()
# if no exception, break
break
except Exception:
pass
else:
# for loop ended without break: no value worked
avail = ""
# don't forget to return your value...
return avail
推荐阅读
- arrays - 使用并发在旋转的排序数组中查找最小值
- git - 使用 docker 将持久的 postgres 数据推送到 git
- angular - Angular 5 - 如何对 ng5-fileupload 组件中的删除文件进行验证
- javascript - 当给出相同的结构时,为什么要求的行为与导入不同
- java - Log4j2 记录到文件和控制台
- arrays - 使用 Array() 构造函数声明数组的正确方法?
- c++ - 为什么需要知道一个类是平凡的还是具有平凡的构造函数?
- python - 如何在 PIL.ImageFont 中使用我自己的位图字体?
- php - 如何从 json 数组中获取所有定价值
- php - 如何在WordPress中按ID提取和显示帖子?