Browser simulation - Python

前端 未结 4 2354
悲哀的现实
悲哀的现实 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:14

    check out Mechanize. "Stateful programmatic web browsing in Python".
    It handles cookies automagically.

    import mechanize
    
    br = mechanize.Browser()
    resp = br.open("http://www.mysitewithcookies.com/")
    print resp.info()  # headers
    print resp.read()  # content
    

    mechanize also exposes the urllib2 API, with cookie handling enabled by default.

提交回复
热议问题