requests模块
requests 模块:python中原生的一款基于网络请求的模块,功能非常强大,简单便捷,效率极高。
作用: 模拟浏览器发送请求
如何使用:(request模块的编码流程)
- 指定url
- 发起请求
- 获取响应数据
- 持久化存储
环境安装:
- pip install requests 使用指令安装
实战编码:
import requests
if __name__ == '__main__':
# step 1 :指定url
url = 'https://www.sogou.com/'
#step 2: 发起请求
# get方法会返回一个响应对象
response = requests.get(url=url)
# step 3 :获取响应数据 text返回的是字符串形式的响应数据
page_text= response.text
# stpe 4:持久化存储
with open('./wangye.html','w',encoding='utf-8')as fp:
fp.write(page_text)
print('爬取结束!!!')
UA伪装
# UA伪装
# UA:user-agent(请求载体的身份标识)