# 1. openner 的 addheaders方法:
>>> from urllib import request
>>> url = "https://www.whatismyip.com"
>>> proxy_support = request.ProxyHandler({'http': '125.108.106.64:9000'})
>>> opener = request.build_opener(proxy_support)
>>> dir(opener)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_call_chain', '_open', 'add_handler', 'addheaders', 'close', 'error', 'handle_error', 'handle_open', 'handlers', 'open', 'process_request', 'process_response'] # 注意,没有add_headers方法
>>> type(opener) # openner对象
<class 'urllib.request.OpenerDirector'>
>>> opener.addheaders = [('User-agent', 'Firefox')]
>>>
# 2. Request对象的add_header方法
>>> from urllib import request
>>> req = request.Request(url='http://www.baidu.com')
>>> dir(req)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_data', '_full_url', '_parse', '_tunnel_host', 'add_header', 'add_unredirected_header', 'data', 'fragment', 'full_url', 'get_full_url', 'get_header', 'get_method', 'has_header', 'has_proxy', 'header_items', 'headers', 'host', 'origin_req_host', 'remove_header', 'selector', 'set_proxy', 'type', 'unredirected_hdrs', 'unverifiable'] # 注意没有addheaders方法
>>> type(req) # request对象
<class 'urllib.request.Request'>
>>> req.add_header('User-Agent', 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)')