我是python的新手(但不是php),并且做了一些测试来自己学习。
当我使用数组中的URL运行代码时,出现此错误:
print req[1].getValue()
AttributeError: 'cStringIO.StringO' object has no attribute 'getValue'
当我检查属性时,可以看到:
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']
当我检查
req.getValue()
我明白了:
print req.getValue()
AttributeError: 'tuple' object has no attribute 'getValue'
我做错了什么? 在这里找到代码:https://fragmentsofcode.wordpress.com/2011/01/22/pycurl-curlmulti-example/
#urls = [...] # list of urls
# reqs: List of individual requests.
# Each list element will be a 3-tuple of url (string), response string buffer
# (cStringIO.StringIO), and request handle (pycurl.Curl object).
reqs = []
# Build multi-request object.
m = pycurl.CurlMulti()
for url in urls:
response = StringIO()
handle = pycurl.Curl()
handle.setopt(pycurl.URL, url)
handle.setopt(pycurl.WRITEFUNCTION, response.write)
req = (url, response, handle)
# Note that the handle must be added to the multi object
# by reference to the req tuple (threading?).
m.add_handle(req[2])
reqs.append(req)
# Perform multi-request.
# This code copied from pycurl docs, modified to explicitly
# set num_handles before the outer while loop.
SELECT_TIMEOUT = 1.0
num_handles = len(reqs)
while num_handles:
ret = m.select(SELECT_TIMEOUT)
if ret == -1:
continue
while 1:
ret, num_handles = m.perform()
if ret != pycurl.E_CALL_MULTI_PERFORM:
break
# print response.getValue()
for req in reqs:
# req[1].getvalue() contains response content
# print dir(req)
print req[1].getValue()