Python 模擬登錄及表單提交 - 代碼分享 - 開源中國社區(qū)
01 | # -*- coding: utf-8 -*- |
08 | url = "http://blog.csdn.net/[username]/archive/2010/07/05/5712850.aspx" |
09 | sock = urllib.urlopen(url) |
12 | content = re.findall( '(?<=blogstory">).*(?=<p class="right artical)' , html, re.S) |
13 | content = re.findall( '<script.*>.*</script>(.*)' , content[ 0 ], re.S) |
14 | title = re.findall( '(?<=<title>)(.*)-.* - CSDN.*(?=</title>)' , html, re.S) |
16 | blog = { 'spBlogTitle' : title[ 0 ].decode( 'utf-8' ).encode( 'gbk' ), #百度博客標題 |
17 | 'spBlogText' : content[ 0 ].decode( 'utf-8' ).encode( 'gbk' ), #百度博客內容 |
24 | cj = cookielib.CookieJar() |
26 | post_data = urllib.urlencode({ 'username' : '[username]' , 'password' : '[password]' , 'pwd' : '1' }) |
28 | path = 'https://passport.baidu.com/?login' |
29 | opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) |
30 | opener.addheaders = [( 'User-agent' , 'Opera/9.23' )] |
31 | urllib2.install_opener(opener) |
32 | req = urllib2.Request(path, post_data) |
33 | conn = urllib2.urlopen(req) |
36 | bd = urllib2.urlopen(urllib2.Request( 'http://hi.baidu.com/[username]/creat/blog' )).read() |
37 | bd = re.findall( '(?<=bdstoken\" value=\").*(?=ct)' , bd, re.S) |
38 | blog[ 'bdstoken' ] = bd[ 0 ][: 32 ] |
40 | blog[ 'spBlogCatName' ] = 'php' |
42 | req2 = urllib2.Request( 'http://hi.baidu.com/[username]/commit' , urllib.urlencode(blog)) |
45 | print urllib2.urlopen(req2).read() |
47 | #請將[username]/[password]替換為您的真實用戶名和密碼 |
本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現有害或侵權內容,請
點擊舉報。