今天在使用requests 进行爬虫操作时,出现了没见过的问题
即:
raise InvalidHeader("Invalid return character or leading space in header: %s" % name)
requests.exceptions.InvalidHeader: Invalid return character or leading space in header: user-agent
百度无果,后来发现是粗心的问题:在从浏览器贴回来的请求头信息里面,不能有空格
错误演示:
headers = {
"user-agent":" Mozilla/5.0(Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36"
}
正确代码:
headers = {
"user-agent":"Mozilla/5.0(Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36"
}
一个空格这类的问题很难发现,共勉