当前位置: 首页 > 图灵资讯 > 行业资讯> python如何模拟搜索请求

python如何模拟搜索请求

来源:图灵python
时间: 2025-01-22 15:40:54

python模拟搜索请求方法:1、输入“import requests命令导入模块;2、输入“reqheaders={}命令并在括号中定义请求头;3、定义post参数并模拟post请求、打印服务器返回的状态、内容和URL地址。

需要使用第三方module:requests,模拟接口。

#!coding:utf-8
#requeststs导入所需的
importrequests
#定义请求头
reqheaders={'Content-type':'application/x-www-form-urlencoded',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Host':'www.renren.com','Origin':'http://zhichang.renren.com',
'Referer':'http://zhichang.renren.com',
'User-Agent':'Mozilla/5.0(WindowsN6.1)AppleWebKit/537.1(KHTML,likeGecko)Chrome/21.0.1180.89Safari/537.1;,}
#定义post参数,requests模块,无需编码参数
reqdata={'email':'xxxx@xxx.com','password':'xxxx','autoLogin':'on',
'origURL':'http://zhichang.renren.com/?login_state=rr','domain':'renren.com'}
#不允许自动重定向模拟post请求
res=requests.post("http://www.renren.com/PLogin.do",data=reqdata,headers=reqheaders,allow_redirects=False)
#模拟post请求,这是自动重定向
#res=requests.post("http://www.renren.com/PLogin.do",data=reqdata,headers=reqheaders)
#打印服务器返回的状态
print(res.status_code)
#打印服务器返回的内容
print(res.content)
#打印重定向URL地址
print(res.headers['Location'])
#cookiee返回打印服务器
print(res.headers['Set-Cookie'])

以下是程序运行的结果

302
TheURLhasmoved<ahref="http://zhichang.renren.com/?login_state=rr">here</a>
anonymid=hrt38gcmbjulj4;domain=.renren.com;path=/;expires=Sun,
17-Feb-201911:27:44GMT,_de=97FB170A42B442D1C47AD77AAFC133386;domain=.renren.com;path=/;
expires=Fri,13-Feb-201511:27:44GMT,p=3191a0a194e606ef12637;domain=renren.com;path=/;
expires=Thu,20-Mar-201411:27:44GMT,ap=229996362;domain=renren.com;path=/;
expires=Thu,20-Mar-201411:27:44GMT,first_login_flag=1;domain=renren.com;path=/,
t=7f5cc046700354f7346bb1f268;domain=.renren.com;path=/,
t=b2814ad2246bad1b67f,bb67;domain=renren.com;path=/xtalk/,
societyguester=7f5cc046700354f7346bb1f268;domain=.renren.com;path=/,
id=229996362;domain=.renren.com;path=/,xnsid=bb3f9425;domain=.renren.com;path=/,
loginfrom=syshome;domain=.renren.com;path=/

推荐课程:Python高级视频教程

上一篇:

python怎么传送文件

下一篇:

返回列表