Skip to content

Instantly share code, notes, and snippets.

@WyattJia
Created April 18, 2017 08:14
Show Gist options
  • Save WyattJia/8ad2a0b868c0544febee6b5ba3f8de43 to your computer and use it in GitHub Desktop.
Save WyattJia/8ad2a0b868c0544febee6b5ba3f8de43 to your computer and use it in GitHub Desktop.
自动评论 CSDN 的一段小脚本
# -*- coding=utf-8 -*-
# -*- author: wellls -*-
from __future__ import division
from bs4 import BeautifulSoup
from bs4.element import (
CharsetMetaAttributeValue,
Comment,
ContentMetaAttributeValue,
Doctype,
SoupStrainer,
Tag,
NavigableString
)
import re
import requests
import os
import time
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
import shutil
import urllib2
import urllib
import cookielib
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import zipfile
def AutoComment(username,password):
#login
url="https://passport.csdn.net/account/login"
head={
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36",
}
s=requests.session()
r=s.get(url,headers=head)
lt_execution_id=re.findall('name="lt" value="(.*?)".*\sname="execution" value="(.*?)"', r.text, re.S)
#print lt_execution_id
payload={
"username":username,
"password":password,
"lt":lt_execution_id[0][0],
"execution":lt_execution_id[0][1],
"_eventId":"submit"
}
r2=s.post(url,headers=head,data=payload)
#print r2.text #login success and response a js
print "*"*100 #split
NotCommented = [] #store the url to be commented
for page in range(1,54): # get url
r3=s.get("http://download.csdn.net/my/downloads/" + str(page),headers=head)
#print page
result_file = open("r3.html",'w')
result_file.write(r3.text)
#print r3.text.encode("GBK")
#Get the list to be commented
soup = BeautifulSoup(open("r3.html"),"html.parser")
soup_body = soup.body
i = 0
for child in soup_body.children:
if type(child) == Tag and child.name == "div":
i = i + 1
if i == 2:
div_1 = child
break
#print div_1.encode("GBK")
div_2 = div_1.link.link.link.link.link.div
#print div_2.encode("GBK")
j = 0
for child in div_2.children:
if type(child) == Tag:
j = j + 1
if j == 2:
div_3 = child
break
#print div_3.encode("GBK")
k = 0
for child in div_3.children:
if type(child) == Tag and child.name == "div":
k = k + 1
if k == 2:
dl_1 = child.dl
#print dl_1.encode("GBK")
for child in dl_1.children:
if type(child) == Tag and child.name == "dt":
if child.a.attrs.has_key("target") == False: #to be commented
NotCommented.append(child.a["href"])
print len(NotCommented)
#comment
#'''
for url in NotCommented:
searchObj = re.search(r'\/(.*)\/(.*?)\/(.*?)#comment', url, re.M|re.I)
id = searchObj.group(3)
print "comment id: " + id
#time.sleep(60)
r4=s.get("http://download.csdn.net/index.php/comment/post_comment?jsonpcallback=&sourceid=" + id + "&content=%E8%B5%84%E6%BA%90%E5%BE%88%E4%B8%8D%E9%94%99%EF%BC%8C%E6%84%9F%E8%B0%A2%E5%8D%9A%E4%B8%BB%E7%9A%84%E5%88%86%E4%BA%AB%EF%BC%81&txt_validcode=undefined&rating=5&t=&_=",headers=head)
if r4.text.encode("GBK") == "({\"succ\":1})":
print id + " done"
else:
print id + " something wrong"
#'''
try:
AutoComment(sys.argv[1],sys.argv[2])
except Exception,e:
print "Usage: CsdnAutoComment.py username password"
print str(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment