Last active
August 29, 2015 14:06
-
-
Save abcfy2/4ff301e5fc75060fbbcf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import requests | |
from lxml import html | |
def login(name,password): | |
url='http://passport.cnblogs.com/login.aspx' | |
session = requests.session() | |
ua = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36' | |
session.headers.update({'User-Agent': ua}) | |
login_page_request = session.get(url) | |
tree = html.fromstring(login_page_request.text) | |
viewstate = tree.xpath('//*[@id="__VIEWSTATE"]/@value')[0] | |
eventvalidation = tree.xpath('//input[@id="__EVENTVALIDATION"]/@value')[0] | |
post_data = { | |
'__VIEWSTATE': viewstate, | |
'__EVENTVALIDATION': eventvalidation, | |
'tbUserName': name, | |
'tbPassword': password, | |
'btnLogin': '登 录', | |
'txtReturnUrl': 'http://home.cnblogs.com/' | |
} | |
login_post_request = session.post(url, data=post_data) | |
print(login_post_request) | |
print(login_post_request.url) | |
print(login_post_request.status_code) | |
print(login_post_request.history) | |
print(login_post_request.text) | |
if __name__ == '__main__': | |
name=input('请输入账号') | |
password=input('请输入密码') | |
login(name,password) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment