Python——记Requests的使用

Response对象的属性

在这里插入图片描述

理解requests库的异常

在这里插入图片描述

通用代码框架
#此代码用来保证网络链接的异常能够被有效处理
import requests

def getHTMLText(url):
    try:
        r = requests.get(url, timeout= 30)
        r.raise_for_status()# 如果状态不是200, 引发HTTPError异常
        r.encoding = r.apparent_encoding
        return r.text
    except:
        return "产生异常"
Requests库的方法

在这里插入图片描述

HTTP协议对资源的操作

在这里插入图片描述

request方法:

使用方法如下:

requests.request(method, url, **kwargs)
# method 包含七种方法:'get','head','post','put','PATCH','DELETE','OPTIONS'
# kwargs 有13个参数: params, 
#1params:字典或字节序列,作为参数添加到URL中
eg = {'key1': 'value1', 'key2': 'value2'}
r = requests.request('GET', 'https://www.baidu.com', params=eg)
print(r.url)

https://www.baidu.com/?key1=value1&key2=value2
#2 data:字典、字节序列或文件对象,作为request的内容,不放在URL链接里,而是作为数据来存储,用法同1。

#3 json:JSION格式的数据,用来作为Request数据的内容,用法同1

#4 headers:字典,HTTP定制头。可用来模拟浏览器。比如爬取亚马逊的时候
import requests

url = 'https://www.amazon.cn/gp/product/B07VSDG7S1/ref=s9_acsd_hps_bw_c_x_1_w?pf_r' \
      'd_m=A1U5RCOVU0NYF2&pf_rd_s=merchandised-search-2&pf_rd_r=0ZTSPRRCV9GZ3F429PGZ' \
      '&pf_rd_t=101&pf_rd_p=37178df6-bce5-4ca7-beb7-1a514eea5d6e&pf_rd_i=116169071'
try:
    eg = {'user-agent': 'Mozilla5.0/'}
    r = requests.get(url, headers=eg)
    r.raise_for_status()
    r.encoding = r.apparent_encoding
    print(r.text)
except:
    print("爬取失败")

#5 cookies: 字典或CookieJar,Request中的cookie
#6 auth: 元组,支持HTTP的认证功能
#7 files :字典类型,传输文件,用来向某一个链接提交文件
eg = {'file': open('data.xls','rb')}
r = requests.request('POST', 'https://www.baidu.com', files=eg)
#8 timeout:设置超时时间,秒为单位
#9 proxies:字典类型,设定访问代理服务器,增加登录认证,隐藏源的IP地址,防止爬虫的逆追踪。
eg = {'http': 'http://user:[email protected].:1478',
      'https:': 'https://10.10.10.1:1236'}
r = requests.request('GET', 'https://www.baidu.com', proxies=eg)
#10 allow_redirects: 重定向开关,默认TRUE。
#11 stream: 获取内容立即下载开关,默认TRUE
#12 verify: 认证SSL证书开关,默认TRUE
#13 cert :本地SSL证书路径

其余六个方法同request方法,但由于网络安全的限制,我们很难向一个URL发起post,put,patch,delete。在爬虫的学习中,我们更常用的是get,head(对于特别大的链接来获取其概要)。

发布了22 篇原创文章 · 获赞 17 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/BerryNard/article/details/100209743
今日推荐