【问题】
用python模拟登陆百度空间,再模拟修改帖子,在提交http的POST请求后,返回的html中,出现错误提示:
<div style="margin-top:20px;line-height:24px" class="f14" id="submitinfo"> |
【解决过程】
1.有人也遇到同类问题:
但是没人解决。只说是网络问题。而且和我这里程序模拟操作,不是一回事。
2. 竟然也搜到了,某人遇到同样的问题:
但是没人解决。
3.后来还是自己随便试了试,在添加了对应的refer后,就解决了此问题了。
返回的html中,提示已经修改成功了:
writestr("您的文章已经修改成功。"); |
对应代码是:
#------------------------------------------------------------------------------ # set common header for http request def getUrlResponse(url, postDict={}, headerDict={}) : if (postDict) : print "POST for ",url; #postData = urllib.urlencode(postDict, True); postData = urllib.urlencode(postDict); logging.debug("after urlencode, data:\r\n%s", postData); req = urllib2.Request(url, postData); req.add_header('Content-Type', "application/x-www-form-urlencoded"); if(headerDict) : for key in headerDict.keys() : req.add_header(key, headerDict[key]); else : print "GET for ",url; req = urllib2.Request(url); req.add_header('User-Agent', gConst['userAgentIE9']); req.add_header('Cache-Control', 'no-cache'); req.add_header('Accept', '*/*'); #req.add_header('Accept-Encoding', 'gzip, deflate'); req.add_header('Connection', 'Keep-Alive'); resp = urllib2.urlopen(req); return resp; #-------------------------------------------------------------------------- postDict = { "bdstoken" : gVal['spToken'], "ct" : "1", "mms_flag" : "0", "cm" : "2", "spBlogID" : spBlogID, "spBlogCatName_o" : postDict['category'], # quoted category name "edithid" : "", "previewImg": "", "spBlogTitle" : postDict['titleName'], "spBlogText" : gVal['newPostPatStr'], "spBlogCatName" : postDict['category'], "spBlogPower" : "0", "spIsCmtAllow" : "1", "spShareNotAllow":"0", "spVcode" : "", "spVerifyKey" : "", } headerDict = { # 如果不添加Referer,则返回的html则会出现错误:"数据添加的一般错误" "Referer" : gVal['blogEntryUrl'] + "/blog/modify/" + spBlogID, } resp = getUrlResponse(modifyUrl, postDict, headerDict); soup = BeautifulSoup(resp, fromEncoding="GB18030"); prettifiedSoup = soup.prettify(); logging.debug("Modify post return html\n---------------\n%s", prettifiedSoup); #--------------------------------------------------------------------------
转载请注明:在路上 » 【已解决】百度空间修改帖子出现:文章修改失败,数据添加的一般错误