2017-07-02 71 views
-3

我在与我的Python应用程序的问题我的应用程序是在指定时间从网络上下载的视频。我的程序名称是tidopy.py ,但我得到这个错误:类型错误:参数1必须是字符串或缓冲区,而不是如

回溯(最近通话最后一个): 文件 “tidopy.py”,第29行,在 file.write(数据) 类型错误:参数1必须是字符串或缓冲区,而不是实例

我有问题,这部分:

while (coun > x): 
    file = open(namelist[x], 'wb') 
    file.write(urllib2.urlopen(addresslist[x])).read() 
    file.close() 
    x = x + 1 

X是用于视频的数字的变量。

名称列表是影片的名称列表。

AddressList中是针对网络视频

我怎样才能修复它的地址列表? 请帮忙。

+0

我想你只需要解决这个问题:'file.write(urllib2.urlopen(AddressList中[X])阅读()。)'。读取在urlopen上激活而不是写入。 –

+0

你明白这条线做:'file.write(urllib2.urlopen(AddressList中[X]))阅读()'? –

+2

这就是为什么你不应该在一条线上做四件不同的事情。 –

回答

0

下面是一个简单的代码从列表中进行下载。

import requests 
import shutil 

namelist = [...] 
addresslist = [...] 

for k, x in enumerate(namelist): 
    r = requests.get(x, stream=True) 
    if r.ok: 
     with open(addresslist[k], 'wb') as f: 
      r.raw.decode_content = True 
      shutil.copyfileobj(r.raw, f) 
+0

这并不是试图解决这个问题,而是与OP的代码完全不同。 –

0

这条线file.write(urllib2.urlopen(addresslist[x])).read()是一口(也是一个有错误)。

其分解成小的,可读的块这样的:

address = addresslist[x] 
request = urllib2.urlopen(address) # create a request object 
html = request.read() # make the request (call read on the request object, not as you were doing before) 
file.write(html) # write the response 

压实你的代码是你应该做的最后一个(最好是永远)的事,因为这使得它非常难以调试和妨碍可读性。

相关问题