我有以下代码,但是当我尝试使用它时,我甚至用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'
答案 0 :(得分:1)
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