How do I set headers using python's urllib?

前端 未结 4 1800
逝去的感伤
逝去的感伤 2020-11-28 22:06

I am pretty new to python\'s urllib. What I need to do is set a custom header for the request being sent to the server. Specifically, I need to set the Content-type and Au

4条回答
  •  -上瘾入骨i
    2020-11-28 22:53

    Use urllib2 and create a Request object which you then hand to urlopen. http://docs.python.org/library/urllib2.html

    I dont really use the "old" urllib anymore.

    req = urllib2.Request("http://google.com", None, {'User-agent' : 'Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5'})
    response = urllib2.urlopen(req).read()
    

    untested....

提交回复
热议问题