TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

运行Python报错:

Traceback (most recent call last):

  File "", line 1, in 
    runfile('C:/Users/Administrator/untitled2.py', wdir='C:/Users/Administrator')

  File "D:\Program Files\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)

  File "D:\Program Files\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/Administrator/untitled2.py", line 27, in 
    with open(filename+'/'+str(x)+'.html','wb')as f:

TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

代码:

# -*- coding: utf-8 -*-
import urllib
import os
import ssl
   
context = ssl._create_unverified_context()
url='https://tieba.baidu.com/f?'

kw=input('请输入吧名:')
pfn=input('请输入开始页:')
pln=input('请输入结束页:')
pfn=int(pfn)
pln=int(pln)
dir='E:/'
filename=dir+kw
if not os.path.exists(filename):
    filename=os.makedirs(dir+kw)
for x in range((pln-pfn+1)):
    print('第'+str(x+1)+'页开始下载。。。。。')
    data0={
      'kw':kw,
      'pn':50*x
      }
    data1=urllib.parse.urlencode(data0) 
    total_url=url+data1
    response=urllib.request.urlopen(total_url,context=context)
    with open(filename+'/'+str(x)+'.html','wb')as f:
        f.wirte(response.read())
    print('第'+x+'页下载结束。。。。。')

看来问题是在于filename

我打印一下它:

结果它是None

TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'_第1张图片

 突然意识到,这仅仅是一个操作,我怎么把它赋值了。。。

改:

TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'_第2张图片

你可能感兴趣的:(Python)