0

自从尝试学习 python 以来,我正在使用 BeautifulSoup 作为我的第一个项目制作足球评论刮板。

基本上对于每个目标陈述,它可能是一个目标、一个乌龙球或一个点球,我需要将这些单独记录在我的数据库中。

出现的问题是有时目标语句包含“罚球区”或“罚球区”,因此我需要将这些情况记录为进球而不是罚球。

我用大量嵌套语句模拟了一些东西,但它很脏。

所以最初我有一些类似的东西

if goal in statement:
   if 'own goal' in statement:
      {record own goal in db}
   elif 'penalty' in statement:
      if not 'penalty area' in statement:
          if not 'penalty box' in statement:
              {record penalty in db}
   else:
      {record goal in db}

我只是一起嘲笑它,因为我没有代码。这是实现这一目标的唯一方法吗?

4

2 回答 2

0

To clean it up, you can maybe combine some logical statements, but apart from that I don't see much improvement.

if 'goal' in statement:
    if 'own goal' in statement:
        {record own goal}
    elif 'penalty' in statement and not('penalty area' in statement) and not('penalty box' in statement):
        {record penalty}
    else:
        {record goal}
于 2013-02-19T22:16:36.493 回答
0

也许这样更好:

   if 'goal' in statement:
       if 'own goal' in statement:
           {record own goal in db}
       elif not('penalty area' in statement or 'penalty box' in statement):
           {record goal in db}
       elif 'penalty' in statement:
           {record penalty in db}
       else:
           {record goal in db}
于 2013-02-19T22:12:58.443 回答