需要beautifulsoup插件,python版本3
# -*- coding:utf-8 -*-
from bs4 import BeautifulSoup
import urllib
import urllib.request
import http.cookiejar
import string
import re
referurl="http://www.iteye.com"#
hosturl = "http://www.iteye.com/login" #自己填写
##########################session获取
#设置一个cookie处理器,它负责从服务器下载cookie到本地,并且在发送请求时带上本地的cookie
cj = http.cookiejar.LWPCookieJar()
cookie_support = urllib.request.HTTPCookieProcessor(cj)
opener = urllib.request.build_opener(cookie_support, urllib.request.HTTPHandler)
urllib.request.install_opener(opener)
#构造header,一般header至少要包含一下两项。这两项是从抓到的包里分析得出的。
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0',
'Referer' : hosturl}
#打开登录主页面(他的目的是从页面下载cookie,这样我们在再送post数据时就有cookie了,否则发送不成功)
request = urllib.request.Request(hosturl, None, headers)
response = urllib.request.urlopen(request)
htmldata = response.read()
html_doc=htmldata.decode('utf-8')
#print(htmldata.decode('utf-8'))
##########################session获取完毕,以下的操作,在一个session中进行。
soup = BeautifulSoup(html_doc,"html.parser")
tokenfind=soup.find('input', {'name': 'authenticity_token'}).get('value')#一个验证串,同一个session周期内,不会改变
print(tokenfind)
username=input('输入用户名:');
passinput=input('输入密码:');
#构造Post数据,从抓的包里分析得出的。
postData = {'name': username,'password':passinput,'authenticity_token':tokenfind}
postData = urllib.parse.urlencode(postData).encode('utf-8')
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0',
'Referer' : referurl}
request = urllib.request.Request(hosturl, postData, headers)
#print(request)
response = urllib.request.urlopen(request)
text = response.read()
print(text)
print("------------------------login success--------------------------")
request = urllib.request.Request(referurl+'/admin', None, headers)
response = urllib.request.urlopen(request)
text = response.read()
print(text.decode('utf-8'))
分享到:
相关推荐
NULL 博文链接:https://xiaoshenge.iteye.com/blog/1416287
這份文件的目的是要提供Python 之機器學習套件
快速学习python必备的书,里面有学习快速学习python的一些基础知识及技巧
很棒的从零开始学 Python》(第二版),有兴趣学习的,有兴趣的可以自己下载看看
自动登录
这是我在iteye网站的技术博客
iteye.com 自动留言交友推广的小工具 博文链接:https://380071587.iteye.com/blog/1931124
主要为大家详细介绍了python实现自动登录后台管理系统,并进行后续操作,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
NULL 博文链接:https://lichuanbao.iteye.com/blog/1855057
NULL 博文链接:https://itshu.iteye.com/blog/1754672
OpenCV3 计算机视觉 Python语言实现。带书签,高清晰度
《Deep Learning With Python》这本书使用Python语言和强大的Keras库带您进入深度学习领域。 本书由Keras创建者和Google AI研究员FrançoisChollet撰写,通过直观的解释和实际的例子帮助您构建对深度学习的理解。您...
NULL 博文链接:https://enefry.iteye.com/blog/986651
NULL 博文链接:https://techpool.iteye.com/blog/644619
最新的ITeye月刊,覆盖各个It的行业最新信息,了解最新的咨询。
NULL 博文链接:https://jiangtie.iteye.com/blog/1003878
Python In A Nutshell英文版 博文链接:https://superwind.iteye.com/blog/440856
NULL 博文链接:https://radiumxie.iteye.com/blog/836479
pwndb.py是一个python命令行工具,用于使用具有相同名称的Onion服务搜索泄露的凭据
NULL 博文链接:https://zhaoaiqing.iteye.com/blog/712402