Mediawiki系统开放了API,向wiki系统的api.php发送http请求,可实现很多功能。
1.登陆:原理可查看mediawiki api文档
# Login
userName = '***'
password = '***'
headers = dict()
headers['Content-Type'] = 'application/x-www-form-urlencoded'
headers['Connection'] = 'Keep-Alive'
params = dict()
params['action'] = 'login'
params['format'] = 'xml'
params['lgname'] = userName
params['lgpassword'] = password
response, content = http.request(LOGIN_URL, method='POST', body=urllib.urlencode(params), headers=headers)
cookie = response.get('set-cookie')
xmldoc = xml.dom.minidom.parseString(content)
token = xmldoc.getElementsByTagName('login')[0].getAttribute('token')
headers['Cookie'] = cookie
params['lgtoken'] = token
response, content = http.request(LOGIN_URL, method='POST', body=urllib.urlencode(params), headers=headers)
xmldoc = xml.dom.minidom.parseString(content)
node = xmldoc.getElementsByTagName('login')[0]
status = node.getAttribute('result')
if status != 'Success':
print "Login Not success..."
exit()
print "Success Login..."
token = node.getAttribute('lgtoken')
userId = node.getAttribute('lguserid')
userName = urllib.quote(userName)
cookiePrefix = node.getAttribute('cookieprefix')
# Construct request headers
cookies = list()
cookies.append(cookie)
cookies.append(cookiePrefix + 'UserName=' + userName)
cookies.append(cookiePrefix + 'UserId=' + userId)
cookies.append(cookiePrefix + 'Token=' + token)
headers['Cookie'] = '; '.join(cookies)
然后使用这个包含登录成功后返回的Cookies的Header请求其他页面,就被认为是已登录的
2.编辑:原理也可参见mediawiki api文档,就是先获取edittoken,然后发送请求编辑
page = chineseEncode('首页')
#response, content = http.request('http://****/mediawiki/api.php?format=xml&action=query&titles=%s&prop=revisions&rvprop=content' % (page), method='GET')
response, content = http.request('http://****/mediawiki/api.php?format=xml&action=query&titles=%s&prop=info|revisions&intoken=edit' % (page), method='GET', headers=headers)
match = re.search(r'edittoken="(\w+\+\\)"', content)
if match:
editToken = match.group(1)
print "editToken is %s" % (editToken)
params = {}
params['format'] = 'xml'
params['action'] = 'edit'
params['title'] = 'newPage'
params['summary'] = 'This is a test summary.'
params['text'] = 'This is my edit text content.'
params['token'] = editToken
response, content = http.request('http://****/mediawiki/api.php', method='POST', body=urllib.urlencode(params), headers=headers)
// 查看content内容,发现里面包含success,编辑成功,查看页面,确实内容改变了
print content