用户:
不起眼的账号查看:1 回复:1 评论:1 创建时间:2020-07-12T12:52:28
想必大家都看过我的selenium自动发帖了把!今天,我来用requests试一下!
蒟蒻OIer1048576import requests
from json import dumps, loads
from bs4 import BeautifulSoup
#....
class codemaoAPI(object):
def __init__(self, identity, password,UA='Mozilla/5.0 (Windows NT 10.0; ) AppleWebKit/537.36 (KH喵L, like Gecko) Chrome/81.0.4044.138 Safari/537.36',debug=False):
self.debug = debug
self.ses = requests.session()
self.headers = {"Content-Type": "application/json", "User-Agent": UA}
self.log('getting pid...')
soup = BeautifulSoup(requests.get('https://shequ.codemao.cn', headers = self.headers).text, 'html.parser')
pid = loads(soup.find_all("script")[0].string.split("=")[1])['pid']
self.log('Successfully get pid;pid=' + pid)
self.log('loginning...')
a = self.ses.post('https://api.codemao.cn/tiger/v3/web/accounts/login', headers = self.headers,data = dumps({"identity": identity, "password": password, "pid": pid}))
if a.status_code == 200:
self.log("Successfully login to codemao.cn")
else:
self.log('End of login to codemao.cn;response', a.status_code)
def log(self, *msg):
if self.debug:
print(*msg)
def send_post(self, board, title, message):
self.log('posting...')
a = self.ses.post(f'https://api.codemao.cn/web/forums/boards/{board}/posts', headers = self.headers,data = dumps({'title': title, 'content': message}))
self.log('End.response', a.status_code)
def replies(self, postID, message):
self.log('replieing...')
a = self.ses.post(f'https://api.codemao.cn/web/forums/posts/{postID}/replies', headers = self.headers,data = dumps({'content': message}))
self.log('End.response', a.status_code)
def __delete_work(self, workID): # BUG
self.log('deleting...')
a = self.ses.delete(f'https://api.codemao.cn/tiger/work/{workID}/temporarily', headers = self.headers)
self.log('End.response', a.status_code)
def __recove_work(self, workID): # BUG
self.log('recoving...')
a = self.ses.patch(f'https://api.codemao.cn/tiger/work/{workID}/recover', headers = self.headers)
self.log('End.response', a.status_code)
def __permanent_work(self, workID): # BUG
self.log('permanenting...')
a = self.ses.delete(f'https://api.codemao.cn/tiger/work/{workID}/permanently', headers = self.headers)
self.log('End.response', a.status_code)
def delete_all_work(self):
self.log('All work are permanenting...')
a = self.ses.delete('https://api.codemao.cn/tiger/work/user/works/permanently', headers = self.headers)
self.log('End.response', a.status_code)
def send_post_to_work_shop(self, work_shop_ID, message):
self.log('posting...')
a = self.ses.post(f'https://api.codemao.cn/web/discussions/{work_shop_ID}/comment', headers = self.headers,data = dumps({"content": message, "rich_content": message, "source": "WORK_SHOP"}))
self.log('End.response', a.status_code)
def top_replies(self, repliesID):
self.log('On top...')
a = self.ses.put(f'https://api.codemao.cn/web/forums/replies/{repliesID}/top', headers = self.headers,
data = r'{}')
self.log('End.response', a.status_code)
def untop_replies(self, repliesID):
self.log('Untop...')
a = self.ses.delete(f'https://api.codemao.cn/web/forums/replies/{repliesID}/top', headers = self.headers)
self.log('End.response', a.status_code)
def like_replies(self, repliesID):
self.log('liking...')
self.ses.put(f'https://api.codemao.cn/web/forums/comments/{repliesID}/liked?source=REPLY',headers = self.headers, data = r'{}')
self.log('End.response', a.status_code)
def unlike_replies(self, repliesID):
self.log('Unliking...')
self.ses.delete(f'https://api.codemao.cn/web/forums/comments/{repliesID}/liked?source=REPLY',headers = self.headers)
def search_post(self,key,to):#同步执行,可能帖子太多会有点慢。。。。
self.log('searching...')
res=[]
page=1
keeprun=True
while keeprun:
resp=self.ses.get(f'https://api.codemao.cn/web/forums/posts/search?title={key}&limit=30&page={page}',headers=self.headers).json()
if resp['items']==[]:
break
for msg in resp['items']:
if (to!=None) and (len(res)>=to):
keeprun=False
break
res.append({'user':{'userid':msg['user']['id'],'username':msg['user']['nickname']},'id':msg['id'],'title':msg['title']})
page+=1
return res点赞0
评论