Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python “NoneType”类型的对象没有len()错误_Python - Fatal编程技术网

Python “NoneType”类型的对象没有len()错误

Python “NoneType”类型的对象没有len()错误,python,Python,我想要的是计算goole首页结果站点上某个特定单词的出现次数,然后再次计算另一个单词的出现次数-如果该单词出现超过2次,那么我将第一个单词的出现次数更改为0。但我得到了这个错误: 文件D:\HQ\u Bot-master\answer\u Bot.py,第307行,在 现场获取积分 文件D:\HQ\u Bot-master\answer\u Bot.py,第293行,在get\u points\u live中 点,maxo=google\u wikisimq,选项,负数 文件D:\HQ\u Bo

我想要的是计算goole首页结果站点上某个特定单词的出现次数,然后再次计算另一个单词的出现次数-如果该单词出现超过2次,那么我将第一个单词的出现次数更改为0。但我得到了这个错误:

文件D:\HQ\u Bot-master\answer\u Bot.py,第307行,在 现场获取积分

文件D:\HQ\u Bot-master\answer\u Bot.py,第293行,在get\u points\u live中 点,maxo=google\u wikisimq,选项,负数

文件D:\HQ\u Bot-master\answer\u Bot.py,第242行,在google\u wiki中 count2=lenwords2

TypeError:类型为“NoneType”的对象没有len

这是我的密码:

import string
import requests 
import json
import urllib.request as urllib2
from bs4 import BeautifulSoup
from google import google
from PIL import Image
import pytesseract
import argparse
import cv2
import os
import pyscreenshot as Imagegrab
import sys
import wx
from halo import Halo

def google_wiki(sim_ques, options, neg):
        spinner = Halo(text='Googling and searching Wikipedia', spinner='dots2')
        spinner.start()
        num_pages = 1
        points = list()
        content = ""
        maxo=""
        maxp=-sys.maxsize
        i = 0
        temp = 0
        ques = ""
        translator = str.maketrans(string.punctuation, ' '*len(string.punctuation))
        sim_ques22 = sim_ques.translate(translator)
        while i < 3:
                o = options[i]
                if i <= 1:
                        x = options[i+1]
                else:

                        x = options[i-1]

                o = o.lower()
                x = x.lower()

                ques += sim_ques22 + ' ' + o + ' wiki'
                print(ques)
                page = requests.get("http://www.google.com/search?q="+ques)
                soup = BeautifulSoup(page.text,"lxml")
                words = soup.find(text=lambda text: text and o in text)
                if(type(words)is not None):
                    count = len(words)


                words2 = soup.find(text=lambda text: text and x in text)
                if(type(words)is not None):
                        count2 = len(words2)

                if count2 >= 2:
                        temp = 0
                else:
                        temp = count

                if neg:
                        temp*=-1
                points.append(temp)     
                if temp>maxp:
                        maxp=temp
                        maxo=o
                ques = ""        
                i += 1


        spinner.succeed()
        spinner.stop()
        return points,maxo


您可以使用一个简单的三元语句:

count=lenwords,如果单词数为0 哪个和这个一样

如果文字:这检查它是否是真实的,而没有一个是不真实的 计数=四个字 其他: 计数=0 如果需要,可以将条件替换为If words为None


编辑:我在后面使用变量时使用了三元表达式。否则,您将导致一个名称错误。

只要使用try和except,如果您想继续而不出现错误,或者捕获错误并在需要时打印即可

try:
        // your code where you got the error 
except:
       pass
       // or print the error caught if you want

因为这个问题是关于python的,所以我删除了标签javawords2,它是None。这就是错误所说的。我不使用BeautifulSoup,但它可能只可遍历一次?如果是,请找到一种方法将其倒带到开头,或者只在一个列表中获取两个查找结果,然后将其拆分为2。您可以发布完整的代码吗?当i小于或等于1时,只有x的值可用,并且在最后您递增i,因此循环永远不会退出,x永远不会获得值,希望这对您有所帮助