注:以下代碼實作新浪部落格的批量删除功能,在python '2.7.1 (r271:86832, Nov 27 2010,
18:30:46) [MSC v.1500 32 bit
(Intel)]'環境下成功執行。代碼運作除需安裝python運作環境外,還需 cPAMIE 庫支援IE浏覽器的自動化操作。
import sys
import re
sys.path.append(r'C:\Downloads\pamie20') #此處為cPamie庫的位置
import cPAMIE
from cPAMIE import PAMIE
ie=PAMIE()
print 'open blog...'
ie.navigate('http://blog.sina.com.cn/[你的新浪使用者名]')
postDir=ie.elementFind('a', 'href',
r'!http://blog.sina.com.cn/s/articlelist_(\d+)_.+html')
postDir = ie.elementGetValue(postDir, 'href')
userId = re.search(r'_(\d+)_0_1.html', postDir).group(1)
#Get all posts id list
#delete posts to recycle bin
postUrl='http://control.blog.sina.com.cn/admin/article/article_del_recycle.php?blog_id[]=*&uid={0}&varname=requestId_63179297'.format(userId)
while True:
ie.navigate(postDir)
urls =
ie.elementsGetList('span', 'className=atc_set')
if len(urls)
== 0:
print 'No more posts left'
break
urls = [
ie.elementGetChildren(i)[0] for i in urls ]
urls = [
re.search(r'\=(.+)',ie.elementGetValue(i, 'href')).group(1) for i
in urls ]
for i in
range(len(urls)):
print urls[i]
print postUrl.replace('*', urls[i])
#ie.navigate(postUrl.replace('*', urls[i]))
'''
Go to recycle bin
'''
#Delete posts from recycle bin
sum = 0
while True: ie.navigate('http://control.blog.sina.com.cn/blog_rebuild/blog/controllers/articlelist.php?uid={0}&status=0'.format(userId))
postUrl='http://control.blog.sina.com.cn/admin/article/article_recycle_del.php?blog_id[]=*&uid={0}&varname=requestId_32616145'.format(userId)
# Get post
link ids
spans=ie.elementsGetList('cite', 'text=删除')
links=[ie.elementGetParent(i) for i in spans]
links=filter(lambda i:
str(ie.elementGetValue(i,'id')).startswith('a_recyle_del'),
links)
ids =
[ie.elementGetValue(i, 'id') for i in links]
# Detect if
there is any post left
if
len(ids)== 0:
break
# Get post
titles
titles=ie.elementsGetList('span','className=atc_title')
titles=[ie.elementGetValue(i,'innerText') for i in titles]
deletion information
print 'About
to delete',len(links),'posts.'
mre=re.compile(r'.*_(\S+)')
for i in
range(len(links)):
art_id = mre.search(ids[i]).group(1)
art_title=titles[i]
print 'deleting',art_title,
delurl = postUrl.replace('*', art_id)
print '...',
ie.navigate(delurl)
print 'OK'
sum+=1
print 'Delete %d posts this time' % sum
print 'There is no post left to delete.'