简使用pycurl来获取网页信息头和内容
先下载pycurl并安装到python中,用import pycurl来测试是否安装成功。
import pycurl import StringIO print "xxx接口使用的https,其值:"; url='https://xxx.xxx.xxx.xxx/xweb/test.do?actionType=1' #url='http://www.lzccb.cn/' c=pycurl.Curl() c.setopt(c.URL, url) b = StringIO.StringIO() c.setopt(c.WRITEFUNCTION, b.write) c.setopt(c.SSL_VERIFYPEER, 0) /* 如果是https就要开启这两行 */ c.setopt(c.SSL_VERIFYHOST, 0) c.setopt(c.FOLLOWLOCATION, 1) c.setopt(c.HEADER, True) /* 要包头信息*/ #c.setopt(c.HEADER,False) /*不要包头信息*/ c.perform() html=b.getvalue() #print html /*打印网页内容*/ print url, " HTTP-code:", c.getinfo(c.HTTP_CODE) /*打印返回状态码*/ b.close() c.close()
下面来自
http://www.angryobjects.com/2011/10/15/http-with-python-pycurl-by-example/
import pycurl c = pycurl.Curl() c.setopt(c.URL, 'http://news.ycombinator.com') c.perform()
import pycurl import cStringIO buf = cStringIO.StringIO() c = pycurl.Curl() c.setopt(c.URL, 'http://news.ycombinator.com') c.setopt(c.WRITEFUNCTION, buf.write) c.perform() print buf.getvalue() buf.close()
import pycurl import cStringIO buf = cStringIO.StringIO() c = pycurl.Curl() c.setopt(c.URL, 'http://news.ycombinator.com') c.setopt(c.WRITEFUNCTION, buf.write) c.setopt(c.CONNECTTIMEOUT, 5) c.setopt(c.TIMEOUT, 8) c.setopt(c.PROXY, 'http://inthemiddle.com:8080') c.perform() print buf.getvalue() buf.close()
import pycurl c = pycurl.Curl() c.setopt(c.URL, 'http://myfavpizzaplace.com/order') c.setopt(c.POSTFIELDS, 'pizza=Quattro+Stagioni&extra=cheese') c.setopt(c.VERBOSE, True) c.perform()
import pycurl c = pycurl.Curl() c.setopt(c.URL, 'http://myappserver.com/ses1') c.setopt(c.CONNECTTIMEOUT, 5) c.setopt(c.TIMEOUT, 8) c.setopt(c.COOKIEFILE, '') c.setopt(c.FAILONERROR, True) c.setopt(c.HTTPHEADER, ['Accept: text/html', 'Accept-Charset: UTF-8']) try: c.perform() c.setopt(c.URL, 'http://myappserver.com/ses2') c.setopt(c.POSTFIELDS, 'foo=bar&bar=foo') c.perform() except pycurl.error, error: errno, errstr = error print 'An error occurred: ', errstr
import pycurl c = pycurl.Curl() c.setopt(c.URL, 'http://myappserver.com/ses1') c.setopt(c.COOKIEFILE, '') c.setopt(c.VERBOSE, True) c.perform() c.setopt(c.URL, 'http://myappserver.com/ses2') c.perform()