python 利用requests模块会话session模拟登录URL网址下载数据
来源:互联网 发布:手机网络时间同步软件 编辑:程序博客网 时间:2024/06/05 15:51
PASSWORD = '[{"userId":"**","password":"**"}]'
def getData():
s = requests.Session()#创建一个session对象
s.keep_alive = False #保持一个长连接
s.headers.update({'Connection': 'keep-alive'})
for i in range(0,5):
try:
s.post('URL',data=PASSWORD)#该URL为登录页面的URL
s.headers.update({'Content-Type': 'application/x-www-form-urlencoded'})
break
except requests.exceptions.ConnectionError:
time.sleep(1)
continue
s.headers.update({'Content-Type': 'application/x-www-form-urlencoded'})
payload = ''
for count in range(0,5):
print count
try:
rsp = s.post('URL',data=payload)#该URL为需要获取数据页面的F12请求正文内容
#print "rsp",rsp
break
except requests.exceptions.ConnectionError:
time.sleep(1)
continue
#print "rsp",rsp
exportfile ="D:/data.xls"
with open(exportfile,'wb') as out_file:
for chunk in rsp.iter_content():#将文本流保存到文件
out_file.write(chunk)
#若数据无效,则删除文件
f = open(exportfile, 'rb')
lines = f.read()
isinvalid=re.search("javascript",lines)
if (len(lines)==0) or (isinvalid is not None):
print "this is empty data,system will delete it"
f.close()
os.remove(exportfile)
else:
print "this is valid data"
time.sleep(40)
def getData():
s = requests.Session()#创建一个session对象
s.keep_alive = False #保持一个长连接
s.headers.update({'Connection': 'keep-alive'})
for i in range(0,5):
try:
s.post('URL',data=PASSWORD)#该URL为登录页面的URL
s.headers.update({'Content-Type': 'application/x-www-form-urlencoded'})
break
except requests.exceptions.ConnectionError:
time.sleep(1)
continue
s.headers.update({'Content-Type': 'application/x-www-form-urlencoded'})
payload = ''
for count in range(0,5):
print count
try:
rsp = s.post('URL',data=payload)#该URL为需要获取数据页面的F12请求正文内容
#print "rsp",rsp
break
except requests.exceptions.ConnectionError:
time.sleep(1)
continue
#print "rsp",rsp
exportfile ="D:/data.xls"
with open(exportfile,'wb') as out_file:
for chunk in rsp.iter_content():#将文本流保存到文件
out_file.write(chunk)
#若数据无效,则删除文件
f = open(exportfile, 'rb')
lines = f.read()
isinvalid=re.search("javascript",lines)
if (len(lines)==0) or (isinvalid is not None):
print "this is empty data,system will delete it"
f.close()
os.remove(exportfile)
else:
print "this is valid data"
time.sleep(40)
阅读全文
0 0
- python 利用requests模块会话session模拟登录URL网址下载数据
- Python模拟登录(一) requests.Session应用
- python requests 模拟网站登录
- python模块下载网址
- python 用requests模块自动登录
- python 爬虫 利用selenium模拟登录帐号 向requests中重设 cookie
- 利用python模拟登录
- python各种模块下载网址
- requests 模拟登录百度
- requests模拟登录百度
- requests模拟登录
- python利用cookie模拟登录
- Python-Requests模拟登录-实现图书馆座位自动预约
- 【python爬虫01】使用requests库模拟登录知乎
- 利用Python requests库模拟登陆学校教务系统
- 利用python requests库模拟登陆知乎
- Python requests 模拟登陆
- Python模块学习 - Requests
- HDU1019
- RPC与Apache Thift
- ubuntu piix4_smbus: Host SMBus controller not enabled!解决方案
- 对linux内核的container_of和offsetof宏的理解
- 韩信点兵
- python 利用requests模块会话session模拟登录URL网址下载数据
- 手把手,脚把脚地教你实现网络爬虫程序(上)
- Python学习--常见异常总结(持续更新)
- Eclipse Neon下使用Maven创建Java Web项目
- HTTP和HTTPS 学习笔记
- centos 增加网卡不能获得ip地址
- 《哈佛商业评论》发长文,谈人工智能商业之路的现状、潜力、障碍与风险...
- 算法之快速排序(nodejs)
- Codeforces 46 D Parking Lot(线段树区间更新模板题)