0

我有一个项目正在进行。在项目文件夹中,我有另一个文件夹(lib),在该文件夹中我有 2 个文件。每个内容如下。问题是 req.py 可以完美地自行运行,但是当我运行 main.py 时,出现以下错误。

Traceback (most recent call last):
  File "C:\Users\redacted\main.py", line 5, in <module>
    req.req()
  File "C:\Users\redacted\lib\req.py", line 13, in req
    return r.content
  File "C:\Python27\lib\site-packages\requests\models.py", line 595, in content
    content = decode_gzip(self._content)
  File "C:\Python27\lib\site-packages\requests\utils.py", line 354, in decode_gzip
    return zlib.decompress(content, 16 + zlib.MAX_WBITS)
TypeError: must be string or read-only buffer, not None

设置如下:
__init__.py:import req

主文件

import lib as n
if __name__ == "__main__":
    req = n.req.req()
    req.req()

请求文件

import requests

class req():
    def __init__(self):
        ua     = 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2'
        self.w = requests.session( headers = { 'User-Agent': ua } )

    def req(self):
        r = self.w.get('http://www.google.com')
        return r.content

if __name__ == "__main__":
    r = req()
    print r.req()

我完全没有想法。有什么解释是什么原因造成的吗?

4

1 回答 1

0

在项目文件夹中,我有另一个文件夹(lib),在该文件夹中我有 2 个文件。

应该是3。

下面的设置: __init__.py:import req

这是第三个文件还是?

于 2011-12-11T10:36:44.193 回答