My personal blog (Fat Dragon's blog) has published a new article!
Welcome to come to read, the following is the connection address of the article
http://www.comingcode.com/?p=357
because I have a personal blog, but also want to improve the point of exposure, so I think how to put their own blog in other sites exposed more points, of course, the most disgusting is to each website or forum advertising, so not only to increase exposure, the same can pull a bit of hatred. For whatever purpose, it is estimated that this person is busy enough to have a manual on each website.
after learning python, you can automatically sync your blog to another site, only to impersonate a user and then send a request to post. Here I only simulated the Baidu space login and publish the article operation, some other sites on the follow-up.
If you want to ask how to do a site simulation login, then you need to know how to get a log on the operation of the packet, after analysis, using Python to simulate the same packet to send it. Common tools can be Google Chrome, Firefox and other browsers, or use Fiddler to monitor HTTP packets. The overall sentence is to log in manually, while using the tool to monitor the contents of the packets sent and received, after analysis, using Python to simulate the same format of the packet.
The following is posted Baidu space simulation login Code Bar
operating Environment: Python 2.7 (3.x not tested), Windows or Linux
IDE Tools: Pycharm
#-*-Coding:utf-8-*-"" "Author:dragondate:2015-01-02" "" Import cookielibimport urllib2import urllibimport Reclass Baiduspace:def __init__ (self): Self.cookie = Cookielib. Lwpcookiejar () Self.chandle = Urllib2. Httpcookieprocessor (Self.cookie) #http get Operation def getData (self, URL, headers=none): request = None if H Eaders is not none:request = Urllib2. Request (URL, headers=headers) else:request = Urllib2. Request (URL) request = Urllib2. Request (URL) opener = Urllib2.build_opener (self.chandle) response = Opener.open (request) data = Respo Nse.read () Try:data = Data.decode (' utf-8 ') Except:data = Data.decode (' GBK ', ' ignore ') Return Data #http post Operation Def postdata (self, URL, data, header): data = Urllib.urlencode (data); Req = None If header is not none:req = Urllib2. Request (Url,data,headers=header) else:reQ = urllib2. Request (url,data) opener = Urllib2.build_opener (self.chandle) response = Opener.open (req) data = Resp Onse.read () Try:data = Data.decode (' Utf-8 '). Encode (' utf-8 ') Except:data = Data.decod E (' gbk ', ' ignore ') return data #登录 def login (self, name, pwd): #第一次先访问一下, to save a cookie first Qurl = ' Https://passport.baidu.com/v2/api/?getapi&class=login&tpl=qing&tangram=false ' Self.getdata (qurl) #第二次访问 to get token Qurl = ' Https://passport.baidu.com/v2/api/?getapi&class=login&tpl=qing&tangram =false ' RSP = Self.getdata (qurl) login_tokenstr = ' bdpass.api.params.login_token= ' (. *?) '; " Login_tokenobj = Re.compile (login_tokenstr,re. Dotall) Matched_objs = Login_tokenobj.findall (RSP) If Matched_objs:token = Matched_objs[0] print ' token = ', token #然后用token模拟登陆 post_data = {' username ': name, ' Password ':p wd, ' token ': token, ' charset ': ' utf- 8 ', ' Callback ': ' Parent.bd__pcbs__1vwwr5 ', ' Isphone ': ' false ', ' Mem_pass ': ' On ', ' Logintype ': ' Basiclogin ', ' loglogintype ': ' Pc_loginbasic ', ' safeflg ': ' 0 ', ' staticpage ': ' Http://hi.baidu.com/com/sh Ow/proxy?for=v3_pass_login ', ' TPL ': ' Qing ', ' u ': ' http://hi.baidu.com/go/l Ogin?from_page=0&from_mod=101 ', ' apiver ': ' V3 ', ' quick_user ': ' 0 ', ' Ppui_logintime ': ' 15118 '} path = ' Http://passport.baidu.com/v2/ap I/?login ' headers = {"Accept": "Image/gif, */*", "Referer": "Https://passport.baidu. Com/v2/?login&tpl=mn&u=http%3a%2f%2fwww.baidu.com%2f "," Accept-language ":" ZH-CN "," Content-type ":" application/ X-www-form-urlencoded "," accept-encoding ":" gzip, deflate "," user-agent ":" Mozilla/4.0 (Compatib Le MSIE 6.0; Windows NT 5.1; SV1;. NET CLR 2.0.50727) "," Host ":" Passport.baidu.com "," Connection ":" Keep-alive ", "Cache-control": "No-cache"} RSP = Self.postdata (path, post_data,headers) return True return False #创建文章 def sendblog (self, title, content, private=0): #获取bdstoken ret = Self.getdat A (' Http://hi.baidu.com/pub/show/createtext '); index = ret.find (' https://passport.baidu.com?logout&bdstoken= ') Index2 = Ret[index+len (' Https://passport.baidu. Com?logout&bdstoken= '):].find (' & ') if index = =-1 or Index2 = = -1:return False bdstoken = Ret[index+len (' Https://passport.baidu.com?logout&bdstoken= '): Index+len (' https://passport.baidu.com?logout&bdstoken= ') +index2] #构建发送的数据 posttextdata = { ' title ': Title.encode ("UTF8"), ' content ': Content.encode ("UTF8"), ' Priva Te ': Private, ' Imgnum ': 0, ' Bdstoken ': Bdstoken, ' refer ': ' Htt P://hi.baidu.com/home ', ' private1 ': Private, ' qing_request_source ': ' New_request ' } headers = {"Accept": "*/*", "Referer": "http://hi.baidu.com/p Ub/show/createtext "," Accept-language ":" zh-cn,zh;q=0.8,en;q=0.6 "," Content-type ":" Appl Ication/x-www-form-urlencoded "," accept-encoding ":" gzip, deflate "," user-agent ":" Mozil la/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;. NET CLR 2.0.50727) "," Connection ":" Keep-alive "," Cache-contRol ":" No-cache ", ' Origin ': ' http://hi.baidu.com ', ' x-requested-with ': ' XMLHttpRequest ' } retdata = Self.postdata (' Http://hi.baidu.com/pub/submit/createtext ', posttextdata, headers)
Simple test Code:
if __name__ = = ' __main__ ': Tbaidu = Baiduspace () tbaidu.login (' user_name ', ' user_pwd ') #这里填自己的帐号密码 print " Login Baidu ... " for I in range (0, 5): title =" New title%d "% i content =" New content%d "% i tbaidu.se Ndblog (title, content)
Finally, we can see their own Baidu space in more than a few articles
Simulation Baidu Space Login