您当前的位置: 首页 >  爬虫

IT之一小佬

暂无认证

  • 3浏览

    0关注

    1192博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

爬虫之requests模块介绍

IT之一小佬 发布时间:2021-01-22 23:08:21 ,浏览量:3

爬虫之requests模块介绍

requests文档http://docs.python-requests.org/zh_CN/latest/index.html      【文档中包括的快速上手要精读,高级用法也要了解】

1.1 requests模块的作用:
  • 发送http请求,获取响应数据        【1.导入2.调用get方法对目标url发送请求】
1.2 requests模块是一个第三方模块,需要在你的python(虚拟)环境中额外安装
  • pip/pip3 install requests
1.3 requests模块发送get请求
  1. 需求:通过requests向百度首页发送请求,获取该页面的源码

  2. 运行下面的代码,观察打印输出的结果

import requests

#  目标url
url = 'http://www.baidu.com'

#  向目标发送get请求
response = requests.get(url)

#  手动设置编码格式
response.encoding = 'utf8'

#  打印响应结果
print(response.text)

print(response.content)
print(response.content.decode())

运行效果:

关注
打赏
1665675218
查看更多评论
立即登录/注册

微信扫码登录

0.0414s