本文實例講述了Python實現模擬登錄及表單提交的方法。分享給大家供大家參考。具體實現方法如下:
# -*- coding: utf-8 -*- import re import urllib import urllib2 import cookielib #獲取CSDN博客標題和正文 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) #根據上文獲取內容新建表單值 blog = {'spBlogTitle': title[0].decode('utf-8').encode('gbk'), #百度博客標題 'spBlogText': content[0].decode('utf-8').encode('gbk'),#百度博客內容 '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) #獲取百度發布博客的認證令牌 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] #設置分類名 blog['spBlogCatName'] = 'php' #比較表單發布博客 req2 = urllib2.Request('http://hi.baidu.com/[username]/commit', urllib.urlencode(blog)) #查看表單提交后返回內容 print urllib2.urlopen(req2).read() #請將[username]/[password]替換為您的真實用戶名和密碼希望本文所述對大家的Python程序設計有所幫助。
新聞熱點
疑難解答
圖片精選