python 模拟登录博客园并且自动发布一篇文章

首先我们用火狐浏览器firebug插件模拟一下在博客园发布一篇文章时post的数据是怎么样的。

打开自己的博客园登录。

python 模拟登录博客园并且自动发布一篇文章

点击添加新随笔。

python 模拟登录博客园并且自动发布一篇文章

写好标题和内容。

打开firebug,点击文章发布。捕捉到post数据如下。

python 模拟登录博客园并且自动发布一篇文章

把post数据填入下面的代码就可。

 

python 模拟登录博客园并且自动发布一篇文章
 1 #! /usr/bin/env python
 2 # coding:utf-8
 3 import urllib,urllib2,cookielib
 4 user_agent = Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)
 5 headers = { User-Agent : user_agent,Referer:https://passport.cnblogs.com/login.aspx?ReturnUrl=http://www.cnblogs.com/yuliyang/admin/EditPosts.aspx,
 6            Host:passport.cnblogs.com,
 7            }
 8 params={tbUserName:用户名,
 9                 tbPassword:密码,
10                 "__EVENTVALIDATION":"/wEdAAUyDI6H/s9f+ZALqNAA4PyUhI6Xi65hwcQ8/QoQCF8JIahXufbhIqPmwKf992GTkd0wq1PKp6+/1yNGng6H71Uxop4oRunf14dz2Zt2+QKDEIYpifFQj3yQiLk3eeHVQqcjiaAP",
11                 "__VIEWSTATE":"/wEPDwULLTE1MzYzODg2NzZkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYBBQtjaGtSZW1lbWJlcm1QYDyKKI9af4b67Mzq2xFaL9Bt",
12                 "btnLogin":"登  录",
13                 "txtReturnUrl":"http://home.cnblogs.com/"}
14 print login......
15 params=urllib.urlencode(params)
16 #设置cookie
17 cj=cookielib.CookieJar()
18 opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
19 urllib2.install_opener(opener)
20 request2=urllib2.Request(https://passport.cnblogs.com/login.aspx,params)
21 response2 =  opener.open(request2)#post 数据
22 #print response2.read()
23 print "log success"
24 params2={__VIEWSTATE:/wEPDwULLTE3NjgxMzM0MDIPZBYCZg9kFgYCBg8WAh4EVGV4dAVKPGxpbmsgcm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YW5jZWQkY2hrQ29tbWVudHMFMEVkaXRvciRFZGl0JEFkdmFuY2VkJGNoa0Rpc2FibGVBbm9ueW1vdXNDb21tZW50cwUnRWRpdG9yJEVkaXQkQWR2YW5jZWQkY2hrTWFpblN5bmRpY2F0aW9uBSVFZGl0b3IkRWRpdCRBZHZhbmNlZCRjaGtGdWxsVGV4dEluUnNzBR5FZGl0b3IkRWRpdCRBZHZhbmNlZCRjaGtQaW5uZWQFLUVkaXRvciRFZGl0JEFkdmFuY2VkJGNoa0lzT25seUZvclJlZ2lzdGVyVXNlcg==,
25                 Editor$Edit$txbTitle:test3,#文章标题
26                 "Editor$Edit$EditorBody":"<p>33333333333333333333333</p>",#文章内容
27                 "Editor$Edit$APOptions$APSiteHome$chkDisplayHomePage":"on",
28                 "Editor$Edit$Advanced$ckbPublished":"on",
29                 "Editor$Edit$Advanced$chkComments":"on",
30                 "Editor$Edit$Advanced$chkMainSyndication":"on",
31                 "Editor$Edit$Advanced$txbEntryName":"",
32                 "Editor$Edit$Advanced$txbExcerpt":"",
33                 "Editor$Edit$Advanced$txbTag":"",
34                 "Editor$Edit$Advanced$tbEnryPassword":"",
35                 "Editor$Edit$lkbPost":"发布"}
36 params2=urllib.urlencode(params2)
37 print "start to write a new blog"
38 request3=opener.open("http://www.cnblogs.com/yuliyang/admin/EditPosts.aspx?opt=1",params2)#post数据
39 print "success write !"
40 #print request3.read()
41 # request3=urllib2.Request("https://www.cnblogs.com/yuliyang/admin/EditPosts.aspx")
View Code

 成功发布!

python 模拟登录博客园并且自动发布一篇文章

python 模拟登录博客园并且自动发布一篇文章

上一篇:在 Borland C++ 及 Visual C++ 环境中使用 STLport (作者:孟岩)


下一篇:jquery 节点的操作