Python脚本赢得';无法运行,因为TypeError:类型为';非类型';没有len()

Python脚本赢得';无法运行,因为TypeError:类型为';非类型';没有len(),python,beautifulsoup,Python,Beautifulsoup,我遇到并尝试试运行,以便稍后调整 在意识到它是用Python2编写的之后,我手动将其转换为Python3。我修复了“print”和“urlparse”语句并删除了它们 reload(sys) sys.setdefaultencoding("utf-8")" (谷歌说它在Python3中没有必要。真的吗?) 当我现在运行代码时,我得到了以下输出,我不知道该怎么做才能解决这个问题,因为我对Python非常陌生。我研究了类似的问题,但解决方案无法修复此代码 [*] Re

我遇到并尝试试运行,以便稍后调整

在意识到它是用Python2编写的之后,我手动将其转换为Python3。我修复了“print”和“urlparse”语句并删除了它们

reload(sys)
sys.setdefaultencoding("utf-8")"
(谷歌说它在Python3中没有必要。真的吗?)

当我现在运行代码时,我得到了以下输出,我不知道该怎么做才能解决这个问题,因为我对Python非常陌生。我研究了类似的问题,但解决方案无法修复此代码

[*] Retrieved 89 possible stored URLs
[*] Retrieving https://web.archive.org/web/20110823161411/http://www.oct282011.com/ (1 of 89)
[*] Retrieving https://web.archive.org/web/20110830211214/http://www.oct282011.com/ (2 of 89)
[+] Adding new image: https://web.archive.org/web/20110830211214im_/http://www.oct282011.com/st.jpg
[+] Adding new image: https://web.archive.org/web/20110830211214im_/http://www.oct282011.com/itswhatyouknow_itsnotamystery_mostofyouhavefoundit.png
[+] Adding new image: https://web.archive.org/web/20110830211214im_/http://www.oct282011.com/sngl.jpg
[+] Adding new image: https://web.archive.org/web/20110830211214im_/http://www.oct282011.com/qst.png
[*] Retrieving https://web.archive.org/web/20110831172158/http://www.oct282011.com/ (3 of 89)
[+] Adding new image: https://web.archive.org/web/20110831172158im_/http://www.oct282011.com/0light.png
[+] Adding new image: https://web.archive.org/web/20110831172158im_/http://www.oct282011.com/itswhatyouknow_itsnotamystery_mostofyouhavefoundit.png
[+] Adding new image: https://web.archive.org/web/20110831172158im_/http://www.oct282011.com/sngl.jpg
[+] Adding new image: https://web.archive.org/web/20110831172158im_/http://www.oct282011.com/0.png
[*] Retrieving https://web.archive.org/web/20110903151702/http://www.oct282011.com/ (4 of 89)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-7152635ed042> in <module>
    123 print("[*] Retrieved %d possible stored URLs" % len(results.assets))
    124 
--> 125 image_paths = get_image_paths(results)
    126 
    127 print("[*] Retrieved %d image paths." % len(image_paths))

<ipython-input-3-7152635ed042> in get_image_paths(packed_results)
     54 
     55         # parse out all image tags
---> 56         soup = bs4.BeautifulSoup(result)
     57         image_list = soup.findAll("img")
     58 

~\Anaconda3\lib\site-packages\bs4\__init__.py in __init__(self, markup, features, builder, parse_only, from_encoding, exclude_encodings, **kwargs)
    265         if hasattr(markup, 'read'):        # It's a file-type object.
    266             markup = markup.read()
--> 267         elif len(markup) <= 256 and (
    268                 (isinstance(markup, bytes) and not b'<' in markup)
    269                 or (isinstance(markup, str) and not '<' in markup)

TypeError: object of type 'NoneType' has no len()
[*]检索到89个可能存储的URL
[*]检索https://web.archive.org/web/20110823161411/http://www.oct282011.com/ (第1页,共89页)
[*]检索https://web.archive.org/web/20110830211214/http://www.oct282011.com/ (第2页,共89页)
[+]添加新图像:https://web.archive.org/web/20110830211214im_/http://www.oct282011.com/st.jpg
[+]添加新图像:https://web.archive.org/web/20110830211214im_/http://www.oct282011.com/itswhatyouknow_itsnotamystery_mostofyouhavefoundit.png
[+]添加新图像:https://web.archive.org/web/20110830211214im_/http://www.oct282011.com/sngl.jpg
[+]添加新图像:https://web.archive.org/web/20110830211214im_/http://www.oct282011.com/qst.png
[*]检索https://web.archive.org/web/20110831172158/http://www.oct282011.com/ (第3页,共89页)
[+]添加新图像:https://web.archive.org/web/20110831172158im_/http://www.oct282011.com/0light.png
[+]添加新图像:https://web.archive.org/web/20110831172158im_/http://www.oct282011.com/itswhatyouknow_itsnotamystery_mostofyouhavefoundit.png
[+]添加新图像:https://web.archive.org/web/20110831172158im_/http://www.oct282011.com/sngl.jpg
[+]添加新图像:https://web.archive.org/web/20110831172158im_/http://www.oct282011.com/0.png
[*]检索https://web.archive.org/web/20110903151702/http://www.oct282011.com/ (第4页,共89页)
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在里面
123打印(“[*]检索到%d个可能存储的URL”%len(results.assets))
124
-->125图像路径=获取图像路径(结果)
126
127打印(“[*]检索到%d个图像路径。“%len(图像路径))
在获取图像路径中(压缩结果)
54
55#解析出所有图像标签
--->56汤=bs4.BeautifulSoup(结果)
57 image_list=soup.findAll(“img”)
58
~\Anaconda3\lib\site packages\bs4\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\.py in\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
265如果hasattr(标记,'read'):#它是一个文件类型对象。
266 markup=markup.read()
-->267 elif len(markup)它看起来像是一个和,它把事情分解了

您可以添加如下内容

if not result:
    print('Result was None:', asset)
    continue
在该行之后,跳过无法提取的资产。

1)

因此,实际上这三行是一条识别真正错误的路径:第一行是调用失败的函数,但由于第一个函数中的另一个函数而引发错误,这是第二行/箭头。第二行/函数由于第二个函数(即第三行也是最后一行)中的错误而引发错误。例如


def f1(a):
b=f2(a)
def f2(a):
c=1/a
如果你调用
f1(0)
你会得到错误


f1(0)
---------------------------------------------------------------------------
ZeroDivisionError回溯(最近一次呼叫上次)
中的c:\Users\test.py
---->1 f1(0)
f1中的c:\Users\test.py(a)
1 def f1(a):
---->2b=f2(a)
f2(a)中的c:\Users\test.py
1 def f2(a):
---->2 c=1/a
ZeroDivision错误:被零除
这表示“调用
f1(0)
(第一行/箭头)中有错误”,但这是由于调用
b=f2(a)
(第二行/箭头)在
c=1/a
(第三行/箭头)行失败所致

这样你就可以追溯你的错误。如果它只是说“f1(0)
中有一个错误”,那么很难追踪真正的错误是什么(
c=1/0

2)
这可能是由于
结果
为空,即不存在

只是添加到其他答案中:

我相信你问题的第一部分已经得到了回答,但关于第二部分,你可以在第267行中具体说明:

elif markup and len(markup) >= 256 and ((isinstance(markup, bytes) and not b'<' in markup) or (isinstance(markup, str) and not '<' in markup))

elif markup and len(markup)>=256 and((isinstance(markup,bytes)and not b'请避免一次问多个问题。1.按奇怪顺序排列的代码行是标题所说的“回溯”——这个词应该允许搜索解释。至于2.问题,请参阅和如何帮助我们帮助您的页面。