Python Simulation Login

Source: Internet
Author: User

Here's a simulated login for Python (www.zhihu.com) to test

First use Firefox and fiddler to intercept all requests, including the URL to the landing page (www.zhihu.com/signin) and the login URL (www.zhihu.com/login/phone_num)

To view the source code of the page, you need to pass in a special value: __XSRF=1A2823B23E14B52E9F4867CB5D60D3D3

This special property is in the request to access the login page, back from the server side, nonsense said, on the code

#coding: Utf-8
Import Urllib2
Import Cookielib
Import Urllib
Import re
Import Sys
From BeautifulSoup import BeautifulSoup

#使用前先安装用python工具pip安装模块
# BeautifulSoup Online Help documentation
# http://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/


#解决中文报错问题
Reload (SYS)
Sys.setdefaultencoding ("UTF8")

#####################################
OPENURL = ' http://www.zhihu.com/#signin '
loginurl = ' Http://www.zhihu.com/login/phone_num '


# bind cookies to a Opener,cookie automatically managed by cookielib
Cookie = Cookielib. Cookiejar ()
Handler = Urllib2. Httpcookieprocessor (Cookie)
Opener = Urllib2.build_opener (handler)

#设置请求头信息
headers = {
' User-agent ': ' mozilla/5.0 (Windows NT 6.1; WOW64; rv:44.0) gecko/20100101 firefox/44.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 ',
' Connection ': ' Keep-alive '
}

#请求参数
data = {
' Account ': ' Phone ',
' Password ': ' pwd ',
' Remember_me ': ' True '
}

Try
# Structuring Request Requests
Request = Urllib2. Request (Openurl,headers=headers)

# receive the Returned data
Response = Urllib2.urlopen (Request)
LoginPage = Response.read ()

#读取页面的name: _XSRF--VALUE:B3BEC39EBC84685244FF7288D000CB2C
Soup = BeautifulSoup (loginpage)
input = Soup.input
#print ' name: ', input[' name '], '--value: ', input[' value ']
data[input[' name ']] = input[' value ']

# Construct request requests, encode parameters
Encodedata = Urllib.urlencode (data)
Print Encodedata
Request = Urllib2. Request (loginurl, Encodedata, Headers=headers)
# receive the Returned data
Response = Urllib2.urlopen (Request)
LoginPage = Response.read ()

Print LoginPage
Except Exception, E:
Print E

Python Simulation Login

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.