响应式网页网站设计,wordpress 缩略图清理,好的网站设计制作,seo网络推广技术Requests模块#xff1a;
requests是一个 HTTP客户端库#xff0c;编写爬虫和测试服务器响应数据时经常会用到。
它可以提取 url 中的信息。
requests库的七个主要方法#xff1a;
方法解释requests.request()构造一个请求#xff0c;支持以下各种方法requests.get()获…Requests模块
requests是一个 HTTP客户端库编写爬虫和测试服务器响应数据时经常会用到。
它可以提取 url 中的信息。
requests库的七个主要方法
方法解释requests.request()构造一个请求支持以下各种方法requests.get()获取html的主要方法requests.head()获取html头部信息的主要方法requests.post()向html网页提交post请求的方法requests.put()向html网页提交put请求的方法requests.patch()向html提交局部修改的请求requests.delete()向html提交删除请求 1. requests.get()
这个我们最常见也是用的最多的
基本使用方法 r request.get(url,params,kwargs)
# 这句代码是构造一个服务器请求request返回一个包含服务器资源的response对象。其中
url: 需要爬取的网站地址。 params: 翻译过来就是参数 url中的额外参数字典或者字节流格式可选。 kwargs : 12个控制访问的参数 kwargs 有以下参数
params字典或字节序列 作为参数增加到url中
使用这个参数可以把一些键值对以?key1value1key2value2的模式增加到url中
例如 import requests # 导入模块url http://www.hello.com/index.php # 设置 urlpayload {key1: values, key2: values}r requests.get(url, paramspayload) print(r.url)# 输出 http://www.hello.com/index.php?key2valueskey1valuesprint(r.status_code) # 输出 200 状态码这里 r 是自定义的变量名用来接收返回值 response
注 www.hello.com 是使用phpstudy搭建的虚拟网站 data字典字节序或文件对象重点作为向服务器提交资源作为request的内容
与params不同的是data提交的数据并不放在url链接里
而是放在url链接对应位置的地方作为数据来存储。它也可以接受一个字符串对象。 json json格式的数据 json合适在相关的htmlhttp相关的web开发中非常常见也是http最经常使用的数据格式
他是作为内容部分可以向服务器提交。
例如 url http://www.hello.com/index.php
payload {key1: value1}
r requests.post(url, jsonpayload)
print(r.json)# 输出 bound method Response.json of Response [200]headers 字典是http的相关语对应了向某个url访问时所发起的http的头字段
可以用这个字段来定义http的访问头可以模拟任何浏览器来对url发起访问。
例如 url http://www.hello.com/index.php
header {user-agent: Chrome/10}
r requests.get(url, headersheader)
print(r.headers)cookies字典或CookieJar指的是从http中解析cookie auth元组用来支持http认证功能 files字典 是用来向服务器传输文件时使用的字段。
例如 url http://www.hello.com/index.php
file {files: open(data.txt, rb)}
r requests.post(url, filesfile)
print(r.text)timeout: 用于设定超时时间 单位为秒当发起一个get请求时可以设置一个timeout时间
如果在timeout时间内请求内容没有返回 将产生一个timeout的异常。 proxies字典 用来设置访问代理服务器。 allow_redirects: 开关 表示是否允许对url进行重定向 默认为True。 stream: 开关 指是否对获取内容进行立即下载 默认为True。 verify开关 用于认证SSL证书 默认为True。 cert 用于设置保存本地SSL证书路径 response对象属性
属性说明r.status_codehttp请求的返回状态若为200则表示请求成功。r.texthttp响应内容的字符串形式即返回的页面内容r.encoding从http header 中猜测的相应内容编码方式r.apparent_encoding从内容中分析出的响应内容编码方式备选编码方式r.contenthttp响应内容的二进制形式 2. requests.head()
head 获取html头部信息的方法 。
例如 import requestsurl https://www.baidu.comr requests.get(url)print(r.headers)3. requests.post()
发送post请求
POST 方法被用于请求源服务器接受请求中的实体作为请求资源的一个新的从属物
例如 payload {key1:value1,key2:value2}r requests.post(http://www.hello.com/index.php,datapayload)print(r.text)
{args: {}, data: , files: {}, form: {key1: value1, key2: value2}, headers: {Accept: */*, Accept-Encoding: gzip, deflate, Connection: close, Content-Length: 23, Content-Type: application/x-www-form-urlencoded, Host: httpbin.org, User-Agent: python-requests/2.18.4}, json: null, origin: 192.168.1.2, url: http://www.hello.com/index.php
}向 url post 一个字符串自动编码为 data rrequests.post(http://www.hello.com/index.php,datahello world)
print(r.text)
{args: {}, data: hello world, files: {}, form: {}, headers: {Accept: */*, Accept-Encoding: gzip, deflate, Connection: close, Content-Length: 10, Host: httpbin.org, User-Agent: python-requests/2.18.4}, json: null, origin: 192.168.1.2, url: http://www.hello.com/index.php
}4. requests.put()
PUT 方法请求服务器去把请求里的实体存储在请求URIRequest-URI标识下。
例如 payload{key1:value1,key2:value2}rrequests.put(http://www.hello.com/index.php,datapayload)print(r.text)
{args: {}, data: , files: {}, form: {key1: value1, key2: value2}, headers: {Accept: */*, Accept-Encoding: gzip, deflate, Connection: close, Content-Length: 23, Content-Type: application/x-www-form-urlencoded, Host: httpbin.org, User-Agent: python-requests/2.18.4}, json: null, origin: 192.168.1.2, url: http://www.hello.com/index.phpRequests模块例子
例
判断状态码是不是 200 import requeststry:url https://www.baidu.comr requests.get(url)if r.status_code 200:print(hello)
except:print(error)BeautifulSoup
BeautifulSoup 是一个可以从 HTML 或 XML 文件中提取数据的 Python 库
它通常跟一些第三方解析器一起使用如 lxmlXMLhtml5lib 等
例 import requests
from bs4 import BeautifulSoupurl https://www.baidu.comr requests.get(url)r.encoding utf-8 # 设置编码soup BeautifulSoup(r.text,lxml)print(soup.prettify())r.text 是获取文本内容的意思lxml 是解析器的意思注意必须使用引号。
prettify() 是对内容进行格式化处理看起来没哪里乱。
BeautifulSoup 通常配合 requests 一起使用。 其他一些方法
find_all() 是一种方法选择器顾名思义就是查询所有符合条件的元素
例如
print(soup.find_all(nameul)) # 查询所有 ui 节点还有一些其他啊
例如
print(soup.head) # 获取head标签print(soup.p.b) # 获取p节点下的b节点soup.p[class] # 获取p节点class属性等等一些方法功能很强大。 urllib
urllib 是一种 http请求的 Python 库
基本语法
urllib.request.urlopen(url, dataNone, timeout)参数
url: 需要打开的网址data: Post提交的数据timeout: 设置网站的访问超时时间单位为秒 常用模块 urllib.request 请求模块 urllib.error 异常处理模块 urllib.parse url解析模块 urllib.robotparser robots.txt解析模块 常用方法
print(response.read().decode(‘utf-8’)) # 返回网页内容print(response.version) # 返回版本信息print(response.status) # 返回状态码200404代表网页未找到print(response.closed) # 返回对象是否关闭布尔值print(response.geturl()) # 返回检索的URLprint(response.info()) # 返回网页的头信息print(response.getcode()) # 返回响应的HTTP状态码 例 判断一个网站的状态码 import urllib.requesturl https://www.baidu.comr urllib.request.urlopen(url)print(r.status)好了这个模块就先说这么多了。 http.client
http.client 也是一种发送 http请求的 Python库
他跟 request 很像。
例如 import http.clientconn http.client.HTTPConnection(www.baidu.com) # 请求地址conn.request(GET,/index.php) # 发送 GET请求路径是 /index.phpres conn.getresponse() # 接收返回值print(res.read().decode(utf-8)) # 打印返回值带中文参数的 GET请求 import http.client
import urllib.parseconn http.client.HTTPConnection(www.baidu.com)url urllib.parse.quote(/index.php?name张三age18,safe:/?)conn.request(GET,url)res conn.getresponse()print(res.read().decode(utf-8))这里需要使用到 urllib模块
quote 是 urllib 的一个子模块
他的作用是对 url进行编码。
safe 是一个 安全过滤器默认会将斜杠转换成 %2F 语法 safe这里放不需要处理的字符
这里如果使用 urllib 模块来打开 url 如果存在中文会报错
为了使他原原本本的输出需要将一些符号排除在过滤之外。 POST 请求 import http.client
import urllib.parseconn http.client.HTTPConnection(www.baidu.com)data urllib.parse.urlencode({name:张三,age:18}).encode(utf-8) # 对url进行编码以及utf-8编码conn.request(POST,index.php,data)res conn.getresponse()print(res.read().decode(utf-8))