HTTP请求:Requests的进阶使用方法浅析( 二 )


print(res.status_code)
except ReadTimeout:
print("捕捉到超时异常")
2.1.3 allow_redirects
设置重定向开关 。
>>> import requests
>>> r = requests.get('http://Github.com')
>>> r.url
'https://github.com/'
>>> r.status_code
200
>>> r.history
[<Response [301]>]
# 如果使用GET、OPTIONS、POST、PUT、PATCH或DELETE , 则可以使用allow_redirects参数禁用重定向
>>> r = requests.get('http://github.com', allow_redirects=False)
>>> r.status_code
301
>>> r.history
[]
# 用HEAD启动重定向
>>> r = requests.head('http://github.com', allow_redirects=True)
>>> r.url
'https://github.com/'
>>> r.history
[<Response [301]>]
import requests
import re
# 第一次请求
r1=requests.get('https://github.com/login')
r1_cookie=r1.cookies.get_dict() #拿到初始cookie(未被授权)
authenticity_token=re.findall(r'name="authenticity_token".*?value=https://www.isolves.com/it/wl/zs/2023-06-28/"(.*?)"',r1.text)[0] #从页面中拿到CSRF TOKEN
# 第二次请求:带着初始cookie和TOKEN发送POST请求给登录页面 , 带上账号密码
data=https://www.isolves.com/it/wl/zs/2023-06-28/{
'commit':'Sign in',
'utf8':'?',
'authenticity_token':authenticity_token,
'login':'xxxxxx@qq.com',
'password':'password'
}
# 测试一:没有指定allow_redirects=False,则响应头中出现Location就跳转到新页面 , 
# r2代表新页面的response
r2=requests.post('https://github.com/session',
data=https://www.isolves.com/it/wl/zs/2023-06-28/data,
cookies=r1_cookie
print(r2.status_code) # 200
print(r2.url) # 看到的是跳转后的页面
print(r2.history) # 看到的是跳转前的response
print(r2.history[0].text) # 看到的是跳转前的response.text
# 测试二:指定allow_redirects=False,则响应头中即便出现Location也不会跳转到新页面 , 
# r2代表的仍然是老页面的response
r2=requests.post('https://github.com/session',
data=https://www.isolves.com/it/wl/zs/2023-06-28/data,
cookies=r1_cookie,
allow_redirects=False
print(r2.status_code) # 302
print(r2.url) # 看到的是跳转前的页面https://github.com/session
print(r2.history) # []
2.1.4 proxies
同添加 headers 方法一样 , 代理参数是 dict 。
import requests
import re
def get_html(url):
proxy = {
'http': '120.25.253.234:812',
'https' '163.125.222.244:8123'
}
heads = {}
heads['User-Agent'] = 'Mozilla/5.0 (windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0'
req = requests.get(url, headers=heads,proxies=proxy)
html = req.text
return html
def get_ipport(html):
regex = r'<td data-title="IP">(.+)</td>'
iplist = re.findall(regex, html)
regex2 = '<td data-title="PORT">(.+)</td>'
portlist = re.findall(regex2, html)
regex3 = r'<td data-title="类型">(.+)</td>'
typelist = re.findall(regex3, html)
sumray = []
for i in iplist:
for p in portlist:
for t in typelist:
pass
pass
a = t+','+i + ':' + p
sumray.append(a)
print('代理')
print(sumray)
if __name__ == '__mAIn__':
url = 'http://www.baidu.com'
get_ipport(get_html(url))
某些接口增加了防骚扰模式 , 对于大规模且频繁的请求 , 可能会弹出验证码 , 或者跳转到登录验证页面 , 或者封禁 IP 地址 , 此时如果想要正常访问 , 可以通过设置代理来解决这个问题 。
除了基本的 HTTP 代理外 , requests 还支持 SOCKS 协议的代理 。
# 安装socks库
pip3 install "requests[socks]"
# 进行代理
import requests
proxies = {
'http': 'socks5://user:password@host:port',
'https': 'socks5://user:password@host:port'
}
res = requests.get('http://www.baidu.com', proxies=proxies)
print(res.status) # 200
2.1.5 hooks
即钩子方法 , requests 库只支持一个 response 的钩子 , 即在响应返回时 , 可以捎带执行自定义方法 。可以用于打印一些信息、做一些响应检查、或者向响应中添加额外的信息 。


推荐阅读