日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python博客项目评论_Python 爬虫入门——小项目实战(自动私信博客园某篇博客下的评论人,随机发送一条笑话,完整代码在博文最后)...

發布時間:2024/7/23 python 35 豆豆

之前寫的都是針對爬蟲過程中遇到問題的解決方案,沒怎么涉及到實際案例。這次,就以博客園為主題,寫一個自動私信博客下的評論人員(在本篇留下的評論的同學也會被自動私信,如果不想被私信,同時又有問題,請私信我)。

1).確定監控的博客,這里以http://www.cnblogs.com/hearzeus/p/5226546.html為例,后面會更改為本篇博客的博客地址。

2).獲取博客下的評論人員。

打開瀏覽器控制臺-網絡面板,可以看到如下信息:

分析可知,獲取評論人員的請求為:

http://www.cnblogs.com/mvc/blog/GetComments.aspx?postId=5226546&blogApp=hearzeus&pageIndex=0&anchorCommentId=0&_=1456989055561

python代碼如下:

defgetCommentsHtml(index):

url= "http://www.cnblogs.com/mvc/blog/GetComments.aspx"params={"postId":"5226546","blogApp":"hearzeus","pageIndex":`index`,'anchorCommentId':`0`,'_=':'1456908852216'}

url_params=urllib.urlencode(params)return json.loads(urllib2.urlopen(url,data=url_params).read())['commentsHtml']

可以通過index來遍歷所有的評論人員。如果,評論人員只有1頁,但是,我把index設為2,這個時候就取不到數據。分析有無數據的返回值,可以通過關鍵特征告訴爬蟲,已經遍歷結束了。我用的特征代碼如下:

if(html.count(u"comment_date")<1):print "遍歷結束:"+`i`

即提取返回值中是否有"comment_date"關鍵字來判斷是否遍歷結束

我們將這個鏈接,直接放在瀏覽器里面打開,可以看到請求結果,如下圖所示:

放在json處理工具里面(http://www.bejson.com/jsonviewernew/),可以看到如下:

從圖中可以看出,有效信息為commentsHtml 字段,同時可以發現,返回的用戶列表形式為html,所以還要對返回值進行解析。

經過一步步的分析,我發現解析代碼:

#parseHtml.py#encoding=utf8

from bs4 importBeautifulSoupdefparse(html):

soup= BeautifulSoup(html,"html.parser")

acount= len(soup.find_all("div","post"))

name_list=[]#print acount

for i inrange(acount):

name_list.append(soup.find_all("div","posthead")[i].find_all("a")[2].string)return name_list

3).保存用戶名,以保證不重復給一個人發送私信。代碼如下:

#FileOperation.py

#encoding=utf8

importsys

reload(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 True

checkName函數,是用來檢查該用戶是否已經被發送過私信

writeName函數,是將發送私信成功后的用戶寫入文本

4).發送私信(這個接口可以自己在博客園發送私信截取到,方法同上),代碼如下

#sendMessage.py

#encoding=utf8

importurllibimporturllib2defsend(name,content):

url= "http://msg.cnblogs.com/ajax/msg/send"header={"Cookie":"**********"}#print `name`

params ={"incept":name,"title":"腳本私信","content":`content`

}

url_param=urllib.urlencode(params)request= urllib2.Request(url=url,headers=header,data=url_param)print urllib2.urlopen(request).read()

a).其中header里面的cookie,需要登錄博客園之后獲取,如下圖馬賽克部分,

b).params通過名稱可以看到每個參數的作用。

5).定時器

python定時器,代碼示例:

importthreadingdefsayhello():print "hello world"t= threading.Timer(2.0, sayhello)

t.start()returnsayhello()

附錄——完整代碼

#spider.py#encoding=utf8

importurllib2importurllibimportjsonimportparseHtmlimportsendMessageimportFileOperationimportthreading

defgetCommentsHtml(index):

url= "http://www.cnblogs.com/mvc/blog/GetComments.aspx"params={"postId":"5226546",#不要監控我的"blogApp":"hearzeus",#不要監控我的"pageIndex":`index`,'anchorCommentId':`0`,'_=':'1456908852216'}

url_params=urllib.urlencode(params)return json.loads(urllib2.urlopen(url,data=url_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):

html=getCommentsHtml(i)if(html.count(u"comment_date")<1):print "遍歷結束:"+`i`

t= threading.Timer(10.0, main)

t.start()returnlist_name=getCommentsUser(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更新

有部分同學測試自動回復,這里更新下自動回復的代碼:

#encoding=utf8

importurllibimporturllib2defsendLetter(name,content):#自動私信

url= "http://msg.cnblogs.com/ajax/msg/send"header={"Cookie":""}

params={"incept":name,"title":"腳本私信","content":content

}

url_param=urllib.urlencode(params)

request= urllib2.Request(url=url,headers=header,data=url_param)printurllib2.urlopen(request).read()defsendComments(parentid,contents):#自動回復

url= "http://www.cnblogs.com/mvc/PostComment/Add.aspx"header={"Cookie":""}

params={"blogApp":"hearzeus","postId":"5238867","body":contents,"parentCommentId":parentid

}

url_param=urllib.urlencode(params)printurl_param

request= urllib2.Request(url=url,headers=header,data=url_param)printurllib2.urlopen(request).read()

2016/3/4 11:01更新

已經被封

2016/3/4 11:34更新

自動回復過快也會失敗,所以,就不打算部署了。

說在最后:

這篇博文只是一個簡單的示例,理解就行。不用繼續測試了,我已經在服務器關了這些功能

注意,Cookie 在sendMessage.py里面改成自己的

https://yunpan.cn/cYy5a9aJ3wLaW? 提取碼 f746

總結

以上是生活随笔為你收集整理的python博客项目评论_Python 爬虫入门——小项目实战(自动私信博客园某篇博客下的评论人,随机发送一条笑话,完整代码在博文最后)...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。