科技网站哪个好,冷库网站建设,静态网页制作,网站在当地做宣传之前写的都是针对爬虫过程中遇到问题的解决方案#xff0c;没怎么涉及到实际案例。这次#xff0c;就以博客园为主题#xff0c;写一个自动私信博客下的评论人员(在本篇留下的评论的同学也会被自动私信#xff0c;如果不想被私信#xff0c;同时又有问题#xff0c;请私信…之前写的都是针对爬虫过程中遇到问题的解决方案没怎么涉及到实际案例。这次就以博客园为主题写一个自动私信博客下的评论人员(在本篇留下的评论的同学也会被自动私信如果不想被私信同时又有问题请私信我)。1).确定监控的博客这里以http://www.cnblogs.com/hearzeus/p/5226546.html为例后面会更改为本篇博客的博客地址。2).获取博客下的评论人员。打开浏览器控制台-网络面板可以看到如下信息分析可知获取评论人员的请求为http://www.cnblogs.com/mvc/blog/GetComments.aspx?postId5226546blogApphearzeuspageIndex0anchorCommentId0_1456989055561python代码如下defgetCommentsHtml(index):url http://www.cnblogs.com/mvc/blog/GetComments.aspxparams{postId:5226546,blogApp:hearzeus,pageIndex:index,anchorCommentId:0,_:1456908852216}url_paramsurllib.urlencode(params)return json.loads(urllib2.urlopen(url,dataurl_params).read())[commentsHtml]可以通过index来遍历所有的评论人员。如果评论人员只有1页但是我把index设为2这个时候就取不到数据。分析有无数据的返回值可以通过关键特征告诉爬虫已经遍历结束了。我用的特征代码如下if(html.count(ucomment_date)1):print 遍历结束i即提取返回值中是否有comment_date关键字来判断是否遍历结束我们将这个链接直接放在浏览器里面打开可以看到请求结果如下图所示放在json处理工具里面(http://www.bejson.com/jsonviewernew/)可以看到如下从图中可以看出有效信息为commentsHtml 字段同时可以发现返回的用户列表形式为html所以还要对返回值进行解析。经过一步步的分析我发现解析代码#parseHtml.py#encodingutf8from bs4 importBeautifulSoupdefparse(html):soup BeautifulSoup(html,html.parser)acount len(soup.find_all(div,post))name_list[]#print acountfor i inrange(acount):name_list.append(soup.find_all(div,posthead)[i].find_all(a)[2].string)return name_list3).保存用户名以保证不重复给一个人发送私信。代码如下#FileOperation.py#encodingutf8importsysreload(sys)sys.setdefaultencoding(utf-8)defcheckName(name):file open(../src/comments)contents file.read().split(\n)for i inrange(len(contents)):if(contents[i].count(name)0):file.close()returnTruedefwirteName(name):file open(../src/comments,a)file.write(name\n)file.close()return TruecheckName函数是用来检查该用户是否已经被发送过私信writeName函数是将发送私信成功后的用户写入文本4).发送私信(这个接口可以自己在博客园发送私信截取到方法同上),代码如下#sendMessage.py#encodingutf8importurllibimporturllib2defsend(name,content):url http://msg.cnblogs.com/ajax/msg/sendheader{Cookie:**********}#print nameparams {incept:name,title:脚本私信,content:content}url_paramurllib.urlencode(params)request urllib2.Request(urlurl,headersheader,dataurl_param)print urllib2.urlopen(request).read()a).其中header里面的cookie需要登录博客园之后获取如下图马赛克部分b).params通过名称可以看到每个参数的作用。5).定时器python定时器代码示例importthreadingdefsayhello():print hello worldt threading.Timer(2.0, sayhello)t.start()returnsayhello()附录——完整代码#spider.py#encodingutf8importurllib2importurllibimportjsonimportparseHtmlimportsendMessageimportFileOperationimportthreadingdefgetCommentsHtml(index):url http://www.cnblogs.com/mvc/blog/GetComments.aspxparams{postId:5226546,#不要监控我的blogApp:hearzeus,#不要监控我的pageIndex:index,anchorCommentId:0,_:1456908852216}url_paramsurllib.urlencode(params)return json.loads(urllib2.urlopen(url,dataurl_params).read())[commentsHtml]defgetCommentsUser(html):returnparseHtml.parse(html)defsendHello(name):#for i in range(len(list_name)):sendMessage.send(name,脚本私信。如有打扰还望海涵)#print(hello:name)defmain():for i in range(10):htmlgetCommentsHtml(i)if(html.count(ucomment_date)1):print 遍历结束it threading.Timer(10.0, main)t.start()returnlist_namegetCommentsUser(html)for i inrange(len(list_name)):if(FileOperation.checkName(list_name[i])!True):sendHello(list_name[i])FileOperation.wirteName(list_name[i])main()其他三个py在上面都给出了注意监控的博客页面一定要改不要监控我的监控的博客页面一定要改不要监控我的监控的博客页面一定要改不要监控我的我说了三遍以上a).代码仅供学习交流b).如有错误多多指教c).转载请注明出处2016/3/4 10:36最新更新估计用不了多久我就会被封了。2016/3/4 10:42更新有部分同学测试自动回复这里更新下自动回复的代码#encodingutf8importurllibimporturllib2defsendLetter(name,content):#自动私信url http://msg.cnblogs.com/ajax/msg/sendheader{Cookie:}params{incept:name,title:脚本私信,content:content}url_paramurllib.urlencode(params)request urllib2.Request(urlurl,headersheader,dataurl_param)printurllib2.urlopen(request).read()defsendComments(parentid,contents):#自动回复url http://www.cnblogs.com/mvc/PostComment/Add.aspxheader{Cookie:}params{blogApp:hearzeus,postId:5238867,body:contents,parentCommentId:parentid}url_paramurllib.urlencode(params)printurl_paramrequest urllib2.Request(urlurl,headersheader,dataurl_param)printurllib2.urlopen(request).read()2016/3/4 11:01更新已经被封2016/3/4 11:34更新自动回复过快也会失败所以就不打算部署了。说在最后这篇博文只是一个简单的示例理解就行。不用继续测试了我已经在服务器关了这些功能注意Cookie 在sendMessage.py里面改成自己的https://yunpan.cn/cYy5a9aJ3wLaW 提取码 f746