python初试,学习get,post请求。

1.学习get,post请求,尝试使用requests或urllib用get方法向 https://www.baidu.com/ 发出一个请求,并将其返回结果输出。
过程如图:图一为requests方法;图二为urllib方法;
python初试,学习get,post请求。
python初试,学习get,post请求。
2.当网络断开时,结果如图
python初试,学习get,post请求。
一直报错,仔细看会提示time out
3.了解什么是请求头,如何添加请求头
请求头详细解释:https://blog.****.net/qq_42820268/article/details/82424353
添加请求头:构建字典,传入header参数即可
self.headers={
“Connection”: “keep-alive”,
“User-Agent”: "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 "
“(KHTML, like Gecko) Chrome/51.0.2704.63 Safari/537.36”,
“Accept”: “text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8”,
“Accept-Encoding”: “gzip, deflate, sdch”,
“Accept-Language”: “zh-CN,zh;q=0.8”
};
request = urllib.request.Request(url=url, headers=headers)
request = requests.get(url=url, headers=headers)
总结:request详细用法:https://www.cnblogs.com/mzc1997/p/7813801.html
urllib详细用法:https://www.cnblogs.com/zhangxinqi/p/9170312.html
http://www.cnblogs.com/strivepy/p/9231127.html