request和response的认识
爬虫学习 request的几个常用的方法 requests.get() 获取HTML网页的主要方法,对应HTTP的GET requests.post() 向HTML网页提交POST请求的方法,对应HTTP的POST get方法需要接受一个参数URL,构造一个向目标服务器发送请求的request对象,返回一个包含对象服务器资源的response对象,常用为 response = requests.get("url") 在get方法中也可以加入其它参数params(Python字典类型数据),headers。这里还有很多参数,可以设置请求超时时间timeout等。 import requests url = "https://www.baidu.com" params = { 'wd':'百度贴吧' } headers = { 'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.3)' } response = requests.get(url