Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将这些头添加到python urllib opener中?_Python_Http_Url_Urllib2 - Fatal编程技术网

如何将这些头添加到python urllib opener中?

如何将这些头添加到python urllib opener中?,python,http,url,urllib2,Python,Http,Url,Urllib2,opener.addHeaders(标题) 你的开场白应该有一个属性addheaders,这是一个元组列表。默认情况下,它包含用户代理 headers = { 'Accept': 'application/json, text/javascript, */*; q=0.01', 'X-Requested-With': 'XMLHttpRequest', 'Referer': 'http://www.namestation.com/domain-search?autosea

opener.addHeaders(标题)


你的开场白应该有一个属性
addheaders
,这是一个元组列表。默认情况下,它包含用户代理

headers = {
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'X-Requested-With': 'XMLHttpRequest',
    'Referer': 'http://www.namestation.com/domain-search?autosearch=1',
    'Origin': 'http://www.namestation.com',
    'Host': 'www.namestation.com',
    'Content-Type': 'application/json; charset=UTF-8',
    'Connection': 'keep-alive'
}
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

类似的方法可能会奏效:

opener.addheaders.append(('Host', 'www.namestation.com'))

你应该先把dict转换成tuple

opener.addHeaders=tuple(headers.items()中项目的项目)

def opener():
    cj=cookielib.CookieJar()
    #Process Hadlers
    opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    opener.addheaders=[
                    ('Accept', 'application/json, text/javascript, */*; q=0.01'),
                    ('X-Requested-With', 'XMLHttpRequest'),
                    ('Referer', 'http://www.namestation.com/domain-search?autosearch=1'),
                    ('Host', 'www.namestation.com'),
                    ('Content-Type', 'application/json; charset=UTF-8'),
                    ('Connection', 'keep-alive'),
                ]
    return opener