How do I set headers using python's urllib?

前端 未结 4 1801
逝去的感伤
逝去的感伤 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条回答
  •  执笔经年
    2020-11-28 22:46

    adding HTTP headers using urllib2:

    from the docs:

    import urllib2
    req = urllib2.Request('http://www.example.com/')
    req.add_header('Referer', 'http://www.python.org/')
    resp = urllib2.urlopen(req)
    content = resp.read()
    

提交回复
热议问题