1.关于网站的"SSL"证书:
import requests
response=requests.get('https://www.12306.cn',verify=False)
print(response.status_code)
运行的结果,虽然状态码返回200,但是有强烈的ssl提醒,想忽略提醒,可以这么写:
import requests
from requests.packages import urllib3
urllib3.disable_warnings()
response=requests.get('https://www.12306.cn',verify=False)
print(response.status_code)
此时返回的结果,就是干净的200了.
也可以这么写,效果是一样的:
import requests,logging
logging.captureWarnings(True)
response=requests.get('https://www.12306.cn',verify=False)
print(response.status_code)
也是返回干净的200.
当然了,若想指定证书,可以这么写,必须有crt和key文件的支持,而且key必须是解密的,加密的会报错:
import requests
response=requests.get('https://www.12306.cn',
cert=('/path/server.crt','/path/key'))
print(response.status_code)
2.设置代理IP:
import requests
proxies={
'http':'http://101.255.56.201:36501',
'http':'http://1.20.97.190:52693'
}
response=requests.get('http://httpbin.org/get',proxies=proxies)
print(response.text)
找的免费代理,一直提示被远程计算机拒绝,不知道是什么原因...
3.设置网页超时,实际上就是timeout参数
import requests
response=request.get('http://httpbin.org/get',timeout=0.01)
#通常服务器不可能在0.01s内做出响应的...
print(response.text)
4.处理HTTP验证框:
from requests.auth import HTTPBasicAuth
auth=HTTPBasicAuth('username','password')
response=requests.get('http://localhost:8080',auth=('username','password'))
response=requests.get('http://localhost:8080',auth=auth)
print(response.status_code)
---返回200则是成功,返回401就是失败了
5.创建request对象,思路是利用Request()填充数据,利用session()创建prapare对象,并用send()发送:
from requests import Request,session
url='http://httpbin.org/post'
data={'name':'Jim Green'}
headers={'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Mobile Safari/537.36'}
req=Request(method='POST',url=url,headers=headers,data=data)
s_session=session()
prepare=s_session.prepare_request(req)
response=s_session.send(prepare)
print(response.text)
不知道Request()有没有添加代理的参数,我用proxies/params都添加失败,有机会再查查看,先留着.