Browser simulation - Python

前端 未结 4 2330
悲哀的现实
悲哀的现实 2021-02-20 11:41

I need to access a few HTML pages through a Python script, problem is that I need COOKIE functionality, therefore a simple urllib HTTP request won\'t work.

Any ideas?

4条回答
  •  一整个雨季
    2021-02-20 12:18

    The cookielib module provides cookie handling for HTTP clients.

    The cookielib module defines classes for automatic handling of HTTP cookies. It is useful for accessing web sites that require small pieces of data – cookies – to be set on the client machine by an HTTP response from a web server, and then returned to the server in later HTTP requests.

    The examples in the doc show how to process cookies in conjunction with urllib:

    import cookielib, urllib2
    cj = cookielib.CookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    r = opener.open("http://example.com/")
    

提交回复
热议问题