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错误
今天爬虫的时候遇到了从来没有遇见过的错误
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
原因是在从浏览器贴回来的请求头信息里面,不能有空格
# 发送请求时设置请求头User-Agent:把自己伪装成一个浏览器去访问
headers = {
"User-Agent":" Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36"
}
把空格去掉就可以了
改正后
# 发送请求时设置请求头User-Agent:把自己伪装成一个浏览器去访问
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36"
}