Python爬虫模块:Requests详细教程

Requests库入门

Requests安装

用管理员身份打开命令提示符:

pip install requests

   
   
  • 1

测试:打开IDLE:

>>> import requests
>>> r = requests.get("http://www.baidu.com")
>>> r.status_code
200
>>> r.encoding = 'utf-8' #修改默认编码
>>> r.text		#打印网页内容

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

HTTP协议

超文本传输协议,Hypertext Transfer Protocol.

HTTP是一个基于“请求与响应”模式的、无状态的应用层协议。

HTTP协议采用URL作为定位网络资源的标识。

URL格式

http://host[:port][path]

host:合法的Internet主机域名或IP地址

port:端口号,缺省端口为80

path:请求资源的路径

操作

在这里插入图片描述
Requests主要方法
在这里插入图片描述
主要方法为request方法,其他方法都是在此方法基础上封装而来以便使用。

request()方法

requests.request(method,url,**kwargs)
#method:请求方式,对应get/put/post等7种
#url:拟获取页面的url链接
#**kwargs:控制访问的参数,共13个

   
   
  • 1
  • 2
  • 3
  • 4

**kwargs:控制访问的参数,均为可选项

get()方法

'''
这是小编准备的python爬虫学习资料,加群:1136201545 即可免费获取!
'''
r  = requests.get(url)
完整方法:
requests.get(url,params=None,**kwargs)
	url:拟获取页面的url链接
	params:url中的额外参数,字典或字节流格式,可选
	**kwargs:12个控制访问的参数,可选

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

get()方法:

构造一个向服务器请求资源的Request对象

返回一个包含服务器资源的Response对象

Response对象
在这里插入图片描述
head()方法

r = requests.head('http://httpbin.org/get')
r.headers

   
   
  • 1
  • 2

获取网络资源的概要信息

post()方法

向服务器提交新增数据

payload = {'key1':'value1','key2':'value2'} #新建一个字典
#向URL POST一个字典,自动编码为form(表单)
r = requests.post('http://httpbin.org/post',data = payload)
#向URL POST一个字符串,自动编码为data
r = requests.post('http://httpbin.org/post',data = 'ABC') 
print(r.text)

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

put()方法

同post,只不过会把原来的内容覆盖掉。

patch()方法

delete()方法

Requests库的异常

在这里插入图片描述
在这里插入图片描述
爬取网页的通用代码框架

import requests
'''
这是小编准备的python爬虫学习资料,加群:1136201545 即可免费获取!
'''
def getHTMLText(url):
    try:
        r = requests.get(url,timeout=30)
        r.raise_for_status() #如果不是200,引发HTTPError异常
        r.recoding = r.apparent_encoding
        return r.text
    except:
        return "产生异常"
if __name__ == "__main__":
    url = "http://www.baidu.com"
    print(getHTMLText(url))

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

实例

向百度提交关键词

import requests
'''
这是小编准备的python爬虫学习资料,加群:1136201545 即可免费获取!
'''
# 向搜索引擎进行关键词提交
url = "http://www.baidu.com"
try:
    kv = {'wd':'python'}
    r = requests.get(url,params =kv)
    print(r.request.url)
    r.raise_for_status()
    print(len(r.text))
except:
    print("产生异常")

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

获取网络图片及存储

import requests
import os
'''
这是小编准备的python爬虫学习资料,加群:1136201545 即可免费获取!
'''
url = "http://image.ngchina.com.cn/2019/0423/20190423024928618.jpg"
root = "D://2345//Temp//"
path = root + url.split('/')[-1]
try:
    if not os.path.exists(root):
        os.mkdir(root)
    if not os.path.exists(path):
        r = requests.get(url)
        with open(path,'wb') as f:
            f.write(r.content)  #r.content返回二进制内容
            f.close()
            print("文件保存成功")
    else:
        print("文件已存在")
except:
    print("爬取失败")

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值