Python的爬虫模块:Requests介绍
Requests是Python中一个非常简洁的HTTP客户端库,它用于发送HTTP请求。它的设计理念是提供美观,简单且易于使用的HTTP接口。
安装方法:
pip install requests
简单的GET请求:
import requests
response = requests.get('https://www.example.com')
print(response.text)
简单的POST请求:
import requests
payload = {'key1': 'value1', 'key2': 'value2'}
response = requests.post('https://www.example.com', data=payload)
print(response.text)
添加headers和cookies:
import requests
headers = {'User-Agent': 'my-app/0.0.1'}
cookies = {'cookie_key': 'cookie_value'}
response = requests.get('https://www.example.com', headers=headers, cookies=cookies)
print(response.text)
处理响应:
import requests
response = requests.get('https://www.example.com')
print(response.status_code) # 状态码
print(response.headers) # 响应头
print(response.cookies) # 响应cookies
print(response.text) # 响应文本
以上代码展示了如何使用Requests库发送不同类型的HTTP请求,并处理响应。
评论已关闭