python中报错requests.exceptions.InvalidHeader: Invalid return character or leading space in header: Acc

这是因为你在写头部的时候加入了空格,例:

headers={
    'Host' : ' 123.206.31.85:49167',
    'User-Agent' : ' Mozilla/5.0 (Windows NT 10.0; WOW64; rv:65.0) Gecko/20100101 Firefox/65.0',
    'Accept' : ' text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Accept-Language' : ' zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
    'Referer' : ' http://123.206.31.85:49167/',
    'Content-Type' : ' application/x-www-form-urlencoded',
    'Content-Length': ' 87',
    'Cookie' : ' PHPSESSID=qij0np73li9nmm1rog8vo5dm76',
    'Connection' : ' keep-alive',
    'Upgrade-Insecure-Requests' : ' 1'
}

然而正确的格式是,在 'Host' 后面的 值里前面不应带有空格,如:

headers={
    'Host' : '123.206.31.85:49167',
    'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:65.0) Gecko/20100101 Firefox/65.0',
    'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Accept-Language' : 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
    'Referer' : 'http://123.206.31.85:49167/',
    'Content-Type' : 'application/x-www-form-urlencoded',
    'Content-Length': '87',
    'Cookie' : 'PHPSESSID=qij0np73li9nmm1rog8vo5dm76',
    'Connection' : 'keep-alive',
    'Upgrade-Insecure-Requests' : '1'
}

 

你可能感兴趣的:(python爬虫)