Python爬虫教程

Python Requests

Python Requests详细教程
Requests继承了urllib的特性。Requests支持HTTP连接保持和连接池,支持使用cookie保持会话,支持文件上传,支持自动确定响应内容的编码,支持国际化的 URL 和 POST 数据自动编码。

pip安装Requests

利用 pip 安装 或者利用 easy_install 都可以完成安装:
$ pip install requests
$ easy_install requests

GET请求(headers参数 和 parmas参数)

1. 最基本的GET请求可以直接用get方法
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-20
import requests
response = requests.get("https://www.lidihuo.com/python/spider-test.html")
# 也可以这么写
# response = requests.request("get", "https://www.lidihuo.com/python/spider-test.html")
添加 headers 和 查询参数,如果想添加 headers,可以传入headers参数来增加请求头中的headers信息。如果要将参数放在url中传递,可以利用 params 参数。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-20
import requests
kw = {'wd':'爬虫-立地货'}
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}
 
# params 接收一个字典或者字符串的查询参数,字典类型自动转换为url编码,不需要urlencode()
response = requests.get("http://www.baidu.com/s?", params = kw, headers = headers)
 
# 查看响应内容,response.text 返回的是Unicode格式的数据
print(response.text)
 
# 查看响应内容,response.content返回的字节流数据
print(respones.content)
 
# 查看完整url地址
print(response.url)
 
# 查看响应头部字符编码
print(response.encoding)
 
# 查看响应码
print(response.status_code)
使用response.text 时,Requests 会基于 HTTP 响应的文本编码自动解码响应内容,大多数 Unicode 字符集都能被无缝地解码。
使用response.content 时,返回的是服务器响应数据的原始二进制字节流,可以用来保存图片等二进制文件。

基本POST请求(data参数)

最基本的GET请求可以直接用post方法
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-20
import requests
response = requests.post("https://www.lidihuo.com/python/spider-test.html", data = data)
传入data数据对于 POST 请求来说,我们一般需要为它增加一些参数。那么最基本的传参方法可以利用 data 这个参数。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-20
import requests
formdata = {请求参数}
url = "https://xxxx.com"
headers={ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"}
response = requests.post(url, data = formdata, headers = headers)
print(response.text)
# 如果是json文件可以直接显示
print(response.json())

代理(proxies参数)

如果需要使用代理,你可以通过为任意请求方法提供 proxies 参数来配置单个请求:
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-20
import requests
# 根据协议类型,选择不同的代理
proxies = {
  "http": "http://127.0.0.1:8088",
  "https": "http://127.0.0.1:8088",
}
 
response = requests.get("https://www.lidihuo.com/python/spider-test.html", proxies = proxies)

私密代理验证 和 Web客户端验证

相比于urllib,requests请求更简单只需要一步:

私密代理

# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-20
import requests
# 如果代理需要使用HTTP Basic Auth,可以使用下面这种格式:
proxy = { "http": "username:password@ip:port" }
 
response = requests.get(url, proxies = proxy)
 
print(response.text)

web客户端验证

如果是Web客户端验证,需要添加 auth = (账户名, 密码)
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-20
import requests
auth=(username, password)
response = requests.get(url, auth = auth)

HTTPS请求 SSL证书验证

Requests也可以为HTTPS请求验证SSL证书:要想检查某个主机的SSL证书,你可以使用 verify 参数(也可以不写)
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-20
import requests
response = requests.get("https://www.lidihuo.com/python/spider-test.html", verify=True)
 
# 也可以省略不写
# response = requests.get("https://www.lidihuo.com/python/spider-test.html")
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4