当前位置: 首页 >> Python >> 【排错】Requests有headers请求get网页,解析正常。无头不正常 >> 正文

【排错】Requests有headers请求get网页,解析正常。无头不正常

5年前 (2019-07-09)     作者:Jiaozn     分类:Python     阅读次数:1920     评论(0)    
# -*- coding:utf-8 -*-
import time
import requests
 
 
 
headers={
 
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36'
}
response=requests.get('http://maoyan.com/board/4?offset=10',headers=headers)
html=response.text
print(response.status_code)
print(html)


例如以上代码,在get方法中去掉headers,在response.text时候出现错误,加上则正常。估计是编码原因引起的。

除非注明,发表在“Jiaozn的博客”的文章『【排错】Requests有headers请求get网页,解析正常。无头不正常』版权归Jiaozn所有。 转载请注明出处为“本文转载于『Jiaozn的博客』原地址https://www.jiaozn.com/reed/492.html
0

评论

发表评论   

昵称*

E-mail*(建议输入,以便收到博主回复的提示邮件)

网站

分享:

支付宝

微信