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