request的get方法
r=request.get(url)构造一个向服务器请求资源的Request对象,css
返回一个包含服务器资源的Response对象。html
Request对象由Request库自动生成的。
Response对象包含从服务器返回的全部相关资源
同时包含咱们向服务器请求得到页面的request信息
request.get(url,params=None,**kwargs)
url:拟获取页面的url连接
params:url中的额外参数,字典或字节格式,可选
**kwargs:12个控制访问的参数
get方法源代码用request方法进行封装
request库提供了七个经常使用方法,除了第一个request方法是基础方法外
其余方法都是经过调用request方法来实现的
也能够这样认为:request库只由一个方法就是request方法
为了编写程序方便,提供了其余6个方法来调用request方法
request库的2个重要对象
r=requests.get(url)
使用request对象,返回response对象
response对象包含爬虫返回的所有内容
网络上的资源,他有他的编码
若是没有编码,咱们将没办法用有效的解析方式使得人类可读这样的内容
r.encoding的编码方式是从Http header中charset字段得到的
若是Http header中有这样一个字段,说明咱们访问的服务器对它资源的编码是有要求的
而这样的编码会得到回来存在r.encoding中
但不是全部的服务器对他的相关资源编码都是有这样的要求
若是header中不存在charset字段,则认为编码为ISO-8859-1
可是这样的编码并不能解析中文
因此Request库提供一个备选编码叫apparent_encoding
apparent_encoding作的事情是根据Http的内容部分(而不是头部分)
分析内容中出现文本的可能的编码形式
原则上来讲,apparent_encoding比encoding更为准确
由于encoding并无分析内容,他只是从header的相关字段中提取编码数
而apparent_encoding在分析内容且找到其中可能的编码服务器
运用实例:网络
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> import requests >>> r = requests.get("http://www.baidu.com") >>> print(r.status_code) #在这里,若是返回的是200表示访问成功。若是不是200则出现了错误 200 >>> type(r) <class 'requests.models.Response'> >>> r.headers {'Server': 'bfe/1.0.8.18', 'Date': 'Thu, 03 May 2018 23:52:26 GMT', 'Content-Type': 'text/html', 'Last-Modified': 'Mon, 23 Jan 2017 13:27:32 GMT', 'Transfer-Encoding': 'chunked', 'Connection': 'Keep-Alive', 'Cache-Control': 'private, no-cache, no-store, proxy-revalidate, no-transform', 'Pragma': 'no-cache', 'Set-Cookie': 'BDORZ=27315; max-age=86400; domain=.baidu.com; path=/', 'Content-Encoding': 'gzip'} >>> r.text '<!DOCTYPE html>\r\n<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>ç\x99¾åº¦ä后面无论是啥了,反正出现了乱码' #因为出现乱码,查看一下编码 >>> r.encoding 'ISO-8859-1' >>> r.apparent_encoding 'utf-8' #改一下r.encoding编码 >>> r.encoding='utf-8' >>> r.text '<!DOCTYPE html>\r\n<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head>'乱码已修改好 >>>