如何将这些头添加到python urllib opener中?
opener.addHeaders(标题)如何将这些头添加到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
你的开场白应该有一个属性
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