开发者

Basic Python StringIO -- Why is GetValue() Returning Nothing?

I'm开发者_如何学JAVA having basic python issues.. In the following example no errors are returned but displaying the contents of all variables using pprint shows that contents is = '' -- why would this possibly be the case?

import sys, os, re, StringIO, pprint, time
from BeautifulSoup import BeautifulSoup, BeautifulStoneSoup
import pycurl

url = "http://google.com/";

strio = StringIO.StringIO()

curlobj = pycurl.Curl()
curlobj.setopt(pycurl.URL, url)
curlobj.perform()
curlobj.close()

contents = strio.getvalue()
strio.close()

Any ideas? Thanks


Look at the lines that involve StringIO.

strio = StringIO.StringIO()
contents = strio.getvalue()
strio.close()

None of these statements draw content from curlobj. So strio is empty.


Edit (thanks to @Alexander Cameron and @agf):

Perhaps you meant

curlobj.setopt(pycurl.WRITEFUNCTION, strio.write)    


You never do anything with your strio variable. You have to pass it in to some function in order for anything to get written to it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜