欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)模擬登錄及表單提交的方法

 更新時(shí)間:2015年07月25日 13:01:46   作者:Sephiroth  
這篇文章主要介紹了Python實(shí)現(xiàn)模擬登錄及表單提交的方法,涉及Python正則匹配、cookie及URL操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了Python實(shí)現(xiàn)模擬登錄及表單提交的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

# -*- coding: utf-8 -*- 
import re 
import urllib 
import urllib2 
import cookielib 
#獲取CSDN博客標(biāo)題和正文 
url = "http://blog.csdn.net/[username]/archive/2010/07/05/5712850.aspx" 
sock = urllib.urlopen(url) 
html = sock.read() 
sock.close() 
content = re.findall('(?<=blogstory">).*(?=<p class="right artical)', html, re.S) 
content = re.findall('<script.*>.*</script>(.*)', content[0], re.S) 
title = re.findall('(?<=<title>)(.*)-.* - CSDN.*(?=</title>)', html, re.S) 
#根據(jù)上文獲取內(nèi)容新建表單值 
blog = {'spBlogTitle': title[0].decode('utf-8').encode('gbk'), #百度博客標(biāo)題 
    'spBlogText': content[0].decode('utf-8').encode('gbk'),#百度博客內(nèi)容 
    'ct': "1", 
    'cm': "1"} 
del content 
del title 
#模擬登錄 
cj = cookielib.CookieJar() 
#用戶名和密碼 
post_data = urllib.urlencode({'username': '[username]', 'password': '[password]', 'pwd': '1'}) 
#登錄路徑 
path = 'https://passport.baidu.com/?login' 
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) 
opener.addheaders = [('User-agent', 'Opera/9.23')] 
urllib2.install_opener(opener) 
req = urllib2.Request(path, post_data) 
conn = urllib2.urlopen(req) 
#獲取百度發(fā)布博客的認(rèn)證令牌 
bd = urllib2.urlopen(urllib2.Request('http://hi.baidu.com/[username]/creat/blog')).read() 
bd = re.findall('(?<=bdstoken\" value=\").*(?=ct)', bd, re.S) 
blog['bdstoken'] = bd[0][:32] 
#設(shè)置分類名 
blog['spBlogCatName'] = 'php' 
#比較表單發(fā)布博客 
req2 = urllib2.Request('http://hi.baidu.com/[username]/commit', urllib.urlencode(blog)) 
#查看表單提交后返回內(nèi)容 
print urllib2.urlopen(req2).read() 
#請(qǐng)將[username]/[password]替換為您的真實(shí)用戶名和密碼

希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論