0

我正在尝试使用 Python 和 PyQt4 完成我的项目,但在通过我制作的函数传递 QLineEdit 变量时遇到问题。该字符串应该作为一个 url 工作,当我通过我的第一个参数传递它时,它试图读取 url 并获取它的内容,它会抛出这个错误:

Traceback (most recent call last):
  File "programa2.py", line 158, in on_link_clicked
    download_mango(self.a, self.path2)

  File "c:\Users\Poblet\ManGet\mango.py", line 19, in download_mango
    urlContent = urllib2.urlopen(url).read() # We read the URL

  File "c:\Python27\lib\urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)

  File "c:\Python27\lib\urllib2.py", line 386, in open
    protocol = req.get_type()

AttributeError: 'QString' object has no attribute 'get_type'

这是由以下操作触发的:

def on_link_clicked(self):
    self.a = self.linkEdit.displayText()
    download_mango(self.a, self.path2)

我完全迷路了。可能是 PyQt4 问题还是我的功能有问题?

我感谢您的帮助。

4

2 回答 2

1

您没有发布足够的代码来证明您的声明是正确的

该字符串应该作为一个 url 工作,当我通过我的第一个参数传递它时

看起来您正在将 QString 传递给 urlopen。把它包起来str(),你应该没问题。

>>> url = QString('http://stackoverflow.com/questions/11121475')
>>> urllib2.urlopen(url).read()
### this generates your error ending with
AttributeError: 'QString' object has no attribute 'get_type'

>>> urllib2.urlopen(str(url)).read()
### works
于 2012-06-20T16:57:49.400 回答
0

self.a 丢失

http:// ”或“ https://

尝试

download_mango("http://"+self.a,self.path2)

http://www.nullege.com/codes/search/urllib2.Request.get_type

于 2012-06-20T15:57:45.953 回答