Python 爬虫,requests模块,代理

本文介绍了如何使用Python的requests模块进行网络请求,包括安装requests库、设置代理IP、获取响应中的Cookie信息并转换为字典、处理URL编码解码问题、进行SSL验证以及设置超时时间等关键操作。
摘要由CSDN通过智能技术生成

安装requests模块:pip3 install requests

 

demo.py(requests模块):

import requests   # 导入。 需要pip安装

response = requests.get("http://www.baidu.com")  # 发送get请求。 返回response对象
# response = requests.post("http://www.baidu.com")
# response = requests.put("http://www.baidu.com")
# response = requests.delete("http://www.baidu.com")

print(response)  # <Response [200]>

# print(response.text)   # <html>... (可能会乱码)
print(response.encoding)   # 查看编码方式。 ISO-8859-1
response.encoding = "utf-8"   # 设置编码方式。
# print(response.text)   # 通过指定的编码方式解码响应数据

print(response.content)   # 获取响应内容。 b'<html>...'  bytes类型
print(response.content.decode('utf-8'))   # decode() 通过指定编码方式将bytes类型转换成字符串类型。 (默认utf-8)

# 查看url
print(response.url)   # 响应的url。 http://www.baidu.com/
print(response.request.url)  # 请求的url。 http://w
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值