0

我对python很陌生......

 import sys, os, time, py4chan, urllib from urllib2 import urlopen, URLError, HTTPError

 def refreshthread(boardin,no):
     global thread
     global topic
     board = py4chan.Board(boardin)
     thread = board.getThread(int(no))
     topic = thread.topic
     time.sleep(2.5)

 def dlfile(url, folder):

     try:
         f = urlopen(url)

         with open(folder + "/" + os.path.basename(url), "wb") as local_file:
             local_file.write(f.read())
             print "Downloaded to " + str(folder + "/" + os.path.basename(url)) 

     except HTTPError, e:
         print "HTTP Error:", e.code, url
     except URLError, e:
         print "URL Error:", e.reason, url

 def getsize(uri):
     file = urllib.urlopen(uri)
     size = file.headers.get("content-length")
     file.close()
     return str(int(size) / 1024)

 def main():
     boardtag = str(raw_input("Board: "))
     threadno = int(raw_input("Thread id: "))
     folder = str(raw_input("Save to folder: "))
     print "Getting thread information..."
     refreshthread(boardtag,threadno)
     print "Subject: " + topic.Subject
     while(True):
         if not os.path.exists(folder): os.makedirs(folder)
         refreshthread(boardtag,threadno)
         for imgurl in thread.Files():
             if imgurl is not None and not os.path.exists(folder + "/" + os.path.basename(imgurl)):
                 print "A wild image appears! " + "(" + getsize(imgurl) + "kb)" 
                 dlfile(imgurl,folder)
             else:
                 pass

 if __name__ == '__main__':
     main()

我在linux上编写了这个代码,它运行得很好,但是如果我在windows上运行它,我会收到这个错误:

TypeError: __init__() takes exactly 4 arguments (2 given)

这很奇怪,因为我没有定义init。这可能是来自另一个模块的初始化吗?

如果我编写其他脚本,py4chan 模块似乎工作正常。两台机器也有相同的python版本。

编辑(完全错误):

Getting thread information...
Traceback (most recent call last):
  File "4chan.py", line 59, in <module>
    main()
  File "4chan.py", line 46, in main
    refreshthread(boardtag,threadno)
  File "4chan.py", line 15, in refreshthread
    board = py4chan.Board(boardin)
TypeError: __init__() takes exactly 4 arguments (2 given)

*编辑: *好的,我有两个不同的同名模块。现在都在工作。我不应该被允许在这个网站上。

4

3 回答 3

0

乍一看,您似乎/在两个地方硬编码为目录分隔符。在 Windows 中,目录分隔符是\.

于 2012-11-13T21:46:15.027 回答
0

我刚刚从http://py4chan.sourceforge.net/获取的 py4chan 模块具有以下定义Board.__init__

class Board:
    def __init__(self, base_url, post_url, filesize):

这需要 4 个参数(包括隐式self),因此您缺少post_urlandfilesize参数(无论是什么)。

于 2012-11-13T21:47:02.560 回答
0

该类py4chan.Board需要用 3 个参数构造,而不是 1 个。refreshthread函数中的这一行:

board = py4chan.Board(boardin)

应该看起来像这样:

board = py4chan.Board(base_url, post_url, filesize)
于 2012-11-13T21:47:09.477 回答