利用requests 模拟登陆csdn

本文介绍了一种使用Python实现优快云网站自动登录的方法。通过解析登录页面获取必要的POST参数,并利用requests库发送登录请求,最后实现了登录后的页面跳转和个人主页访问。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境:python3.6.1 + lxml4.0.0 + requests2.18.4

坑一:登陆时请求的网址需要构造,数据在form标签属性里,
坑二:表单数据的提取
坑三:登陆后的跳转,不然无法访问个人主页

import requests
from lxml import etree


#设置session
s=requests.Session()

#基础参数
url='https://passport.youkuaiyun.com'
headers={'Host':'passport.youkuaiyun.com',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Referer': 'https://passport.youkuaiyun.com/?service=http://write.blog.youkuaiyun.com/postedit'
}

#抓取post参数和jsessionid
f=s.get(url,headers=headers).content.decode('utf-8')
p=etree.HTML(f)
it=p.xpath("//input[@name='lt']/@value")
exe=p.xpath("//input[@name='execution']/@value")
eventId=p.xpath("//input[@name='_eventId']/@value")
jid=p.xpath("//form/@action")
jid=str(jid[0])

#提取postdata#
d={'username':'leiyang_ace@163.com',
    'password':'1005931665asd'}
d['lt']=str(it[0])
d['execution']=str(exe[0])
d['_eventId']=str(eventId[0])
#构造post url
url=url+jid

#进行登陆
gg=s.post(url,data=d,headers=headers)
#重定向
tt=s.get("http://www.youkuaiyun.com/")
print(tt.status_code)
#进入个人主页
hh=s.get("http://write.blog.youkuaiyun.com/postlist").content.decode('utf-8')
print(hh)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值