Getting HTML with Pycurl

后端 未结 2 1279
梦毁少年i
梦毁少年i 2021-02-06 03:52

I\'ve been trying to retrieve a page of HTML using pycurl, so I can then parse it for relevant information using str.split and some for loops. I know Pycurl retrieves the HTML,

2条回答
  •  半阙折子戏
    2021-02-06 04:35

    this will send a request and store/print the response body:

    from StringIO import StringIO    
    import pycurl
    
    url = 'http://www.google.com/'
    
    storage = StringIO()
    c = pycurl.Curl()
    c.setopt(c.URL, url)
    c.setopt(c.WRITEFUNCTION, storage.write)
    c.perform()
    c.close()
    content = storage.getvalue()
    print content
    

    if you want to store the response headers, use:

    c.setopt(c.HEADERFUNCTION, storage.write)
    

提交回复
热议问题