python-多处理池中的示例



我有以下代码,但是当我尝试时,我甚至会使用 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:UsersAnonymousDesktopProjectgoogle spreadsheetproject.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

相关内容

  • 没有找到相关文章

最新更新