使用 pycurl 獲取 網頁 信息 內容 欄目 HTML 简体版
原文   原文鏈接

簡使用pycurl來獲取網頁信息頭和內容 html

先下載pycurl並安裝到python中,用import pycurl來測試是否安裝成功。 python

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/ web

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()
相關文章
相關標籤/搜索
每日一句
    每一个你不满意的现在,都有一个你没有努力的曾经。
本站公眾號
   歡迎關注本站公眾號,獲取更多信息