某财的用户应该都知道这个网站,在“签到有礼”版块,每天会有一贴,用帖子中给出的关键字回帖,得铜钱,据说铜钱可以换现金,还可以换书。

真好,裸辞在家的失业人员最需要这个~每天领之。

基本思路:

  1. 先用抓包工具仔细分析下登陆以及回帖时post了哪些数据,这些数据从何而来(我用的Firefox + Firebug,挺好用的,选上保持+全部,就算页面重定向,所有的请求也都能看到);
  2. python requests库,用requests.Session().post来登陆和回帖,用get来读取页面内容;
  3. 登陆之后,拿到BBS首页HTML源码,正则+BeautifulSoup,找到“签到有礼”子版块的相对URL,以及它的forumID,跟baseURL拼接得到子版块URL;
  4. get子版块URL,拿到子版块首页HTML源码,相同的方法,得到当日签到帖子的相对URL和threadID;
  5. get签到帖子URL,找到帖子里的关键字,post回帖之;
  6. 然后再一路找到自己信息栏,看看自己的铜钱数;
  7. 最后,把这一路走来的中间过程和状态写入到log文件里,方便出问题后反查。
  8. 最后的最后,写个.sh脚本,里面运行这个python程序,配置个相应的plist,每天自动执行(MAC OS)

先说说我踩过的坑:

  • 登陆post之后,返回的是200状态值,也就是成功,但是回帖post时,永远提示未登陆,肯定是cookie出了问题,但是requests是自动保持cookie和session的,吭吭哧哧大半天之后,crab大神一语点醒了我。仔细看抓包工具里的相关包,仔细看post之后,响应的content!!!这个网站登陆数据post之后,响应的content是个scripts,里面有两个链接,乍一看,都是什么API...从抓包工具里也能清楚看到,post登陆数据之后,立马连着两个get,请求的URL正是post之后,响应的content里面的那两个URL。并且这两个get得到的响应都是set-cookie,没错,这两个URL就是传说中『种cookie』的。所以在登录post之后,再get这两个URL,后面就OK了。所以,不管爬什么网站,仔细分析清楚请求包和响应包,这是一切的基础!

小知识点GET:

  • f = open(file,'r+')
  • f = open(file,'w+')
  • 乍一看,r+ 跟 w+ 没区别,其实有很大区别:r+ 方式,文件必须存在,否则会报错,用r+方式写的时候,它是从头开始覆盖的,覆盖到哪里算哪里;而w+方式,文件不存在时会新建,写入的时候是全部清空再写入的。a+则是可读可写,并且是用追加方式写入的。

下面程序运行方法:python 路径/Auto_Login_Reply.py 用户名/密码,这种方式有个好处,不用改.py里面的用户名密码参数,直接带参数运行。

本程序是面向过程的,从头至尾,一气呵成。

python真好,既能面向对象,也能面向过程,灵活巧妙,赞!

#!/usr/bin/env python
#-*- coding:utf-8 -*-__author__ = 'Sophie2805'import re
import os.pathimport requests
from bs4 import BeautifulSoupimport time
import sys'''~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if log.txt does not exist under current executing path, create it.
write log, if the log file is larger than 100 lines, delete all then write from beginning
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'''file = os.path.abspath('.')+'/log.txt'#get the absolute path of .py executing
if not os.path.isfile(file):#not exist, create a new onef = open(file,'w')f.close()if os.path.getsize(file)/1024 > 1024:#larger than 1MBf = open(file,'w')try:f.write('')finally:f.close()'''~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
python Auto_Login_Reply.py user/pwd
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'''args = sys.argv[1]
#print args
username = args[0:args.find('/')]
pwd = args[args.find('/')+1:len(args)]
#print username , pwd'''~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using log_list[] to log the whole process
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'''#print os.path.abspath('.')
log_list = []
log_list.append('+++++++++++++++++++++++++++++++++++++++++++++\n')
log_list.append('++++挖财签到有礼'+(time.strftime("%m.%d %T"))+' 每天签到得铜钱++++\n')
log_list.append('+++++++++++++++++++++++++++++++++++++++++++++\n')s = requests.Session()agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:38.0) Gecko/20100101 Firefox/38.0'
connection = 'keep-alive's.headers. update({'User-Agent':agent,'Connection':connection})'''~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
post login request to this URL, observed in Firebug
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'''login_url = 'https://www.wacai.com/user/user!login.action?cmd=null'login_post_data ={'user.account':username,'user.pwd':pwd
}try:login_r = s.post(login_url,login_post_data)
except Exception,e:log_list.append(time.strftime("%m.%d %T") + '--Login Exception: '+ e + '.\n')f = open(file,'a')#append
try:f.writelines(log_list)
finally:f.close()
log_list=[]'''~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
these two get() are very import!!!
login_r.content return these 2 api URLs.
Without getting these 2 URLs, the BBS will not take our session as already login.
I assume, getting these 2 URLs, some critical cookie will be returned.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'''src1 = login_r.content[login_r.content.find('src')+5:login_r.content.find('"></script>')]
src2 = login_r.content[login_r.content.rfind('src')+5:login_r.content.rfind('"></script><script>')]
#print src1
#print src2
s.get(src1)
s.get(src2)base_url = 'http://bbs.wacai.com/'
homepage_r = s.get(base_url)
if '我的挖财' in homepage_r.content:log_list.append(time.strftime("%m.%d %T") + '--Successfully login.\n')
#print homepage_r.content
'''~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
find the checkin forum URL and ID, which is used as fid parameter in the reply post URL
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'''
pattern = '<.+>签到有礼<.+>'
p = re.compile(pattern)
soup = BeautifulSoup(p.findall(homepage_r.content)[0])
checkin_postfix = soup.a['href']
checkin_forum_url = checkin_postfix
#print checkin_postfix
forum_id = checkin_postfix[checkin_postfix.find('-')+1:checkin_postfix.rfind('-')]
#print forum_id
if forum_id != '':log_list.append(time.strftime("%m.%d %T") + '--Successfully find the checkin forum ID.\n')print '--Successfully find the checkin forum ID'f = open(file,'a')#appendtry:f.writelines(log_list)finally:f.close()log_list=[]'''~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
get the checkin forum portal page and find today's thread URL and ID, which is used as tid parameter in the reply post URL
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'''
checkin_forum_page=s.get(checkin_forum_url)
#print checkin_forum_page.content
#print checkin_forum_page.status_code
title = '签到有礼'+(time.strftime("%m.%d")).lstrip('0')+'每天签到得铜钱,每人限回一次'
print title;
pattern_1 = '<.+>'+title + '<.+>'
p_1 = re.compile(pattern_1)
soup = BeautifulSoup(p_1.findall(checkin_forum_page.content)[0])
thread_postfix = soup.a['href']
thread_url = base_url + thread_postfix
thread_id= thread_postfix[thread_postfix.find('-')+1:thread_postfix.rfind('-')-2]
#print thread_idif thread_id != '':log_list.append(time.strftime("%m.%d %T") + '--Successfully find the thread ID.\n')f = open(file,'a')#appendtry:f.writelines(log_list)finally:f.close()log_list=[]
t = s.get(thread_url)'''~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
formhash is a must in the post data, observed in Firebug.
So get the formhash from the html of the page
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'''
pattern_2 = '<input type="hidden" name="formhash" .+/>'
p_2 = re.compile(pattern_2)
soup = BeautifulSoup(p_2.findall(t.content)[0])
formhash = soup.input['value']pattern_3 = '回帖内容必须为'+'.+'+'</font>非此内容将收回铜钱奖励'
result_3 = re.compile(pattern_3).findall(t.content)
#print result_3
key = result_3[0][result_3[0].find('>')+1:result_3[0].rfind('<')-1]
if key != '':log_list.append(time.strftime("%m.%d %T") + '--Successfully find the key word.\n')f = open(file,'a')#appendtry:f.writelines(log_list)finally:f.close()log_list=[]'''~~~~~~~
auto reply
~~~~~~~~~~'''host='bbs.wacai.com'
s.headers.update({'Referer':thread_url})
s.headers.update({'Host':host})
reply_data={'formhash':formhash,'message':key,'subject':'','usesig':''
}
reply_post_url = 'http://bbs.wacai.com/forum.php?mod=post&action=reply&fid='+forum_id+'&tid='+thread_id+'&extra=&replysubmit=yes&infloat=yes&handlekey=fastpost&inajax=1'
try:reply_r = s.post(reply_post_url,data=reply_data)
except Exception,e:log_list.append(time.strftime("%m.%d %T") + '--Reply exception: '+ e +'.\n' )
if '非常感谢,回复发布成功,现在将转入主题页,请稍候……' in reply_r.content:#successlog_list.append(time.strftime("%m.%d %T") + '--Successfully auto reply.\n')
else:log_list.append(time.strftime("%m.%d %T") + '--Fail to reply: '+ reply_r.content + '.\n')
f = open(file,'a')#append
try:f.writelines(log_list)
finally:f.close()
log_list=[]
'''~~~~~~~~~~~~~~
find my WaCai URL
~~~~~~~~~~~~~~~~~'''
pattern_4 = '<.+访问我的空间.+</a>'
p_4 = re.compile(pattern_4)
soup = BeautifulSoup(p_4.findall(t.content)[0])
if soup.a['href'] != '':log_list.append(time.strftime("%m.%d %T") + '--Successfully find my WaCai link.\n' )f = open(file,'a')#appendtry:f.writelines(log_list)finally:f.close()log_list=[]
mywacai_url = soup.a['href']
mywacai_page = s.get(mywacai_url)'''~~~~~~~~~~~~~
find my info URL
~~~~~~~~~~~~~~~~'''
pattern_5 = '<.+个人资料</a>'
p_5 = re.compile(pattern_5)
soup = BeautifulSoup(p_5.findall(mywacai_page.content)[0])
if soup.a['href'] != '':log_list.append(time.strftime("%m.%d %T") + '--Successfully find my info link.\n' )f = open(file,'a')#appendtry:f.writelines(log_list)finally:f.close()log_list=[]
myinfo_url = base_url+ soup.a['href']
myinfo_page = s.get(myinfo_url)'''~~~~~~~~~~~~~~
find my coin info
~~~~~~~~~~~~~~~~~'''
pattern_6 = '<em>铜钱.+\n.+\n'
p_6 = re.compile(pattern_6)
coin = p_6.findall(myinfo_page.content)[0]
coin = coin[coin.find('</em>')+5:coin.find('</li>')]
if int(coin.strip()) != 0:log_list.append(time.strftime("%m.%d %T") + '--Successfully get my coin amount: %s.\n'% int(coin.strip()))f = open(file,'a')#appendtry:f.writelines(log_list)finally:f.close()log_list=[]

最后是plist,mac电脑用这个配置定时任务,windows的话,写个bat,然后也可以配置的貌似。

先写个test.sh脚本,注意用chmod 777 test.sh给它赋予可执行的权限:

cd /Users/Sophie/PycharmProjects/Auto_Login_Reply_BBS_WaCai

python Auto_Login_Reply.py username/password

然后到如下路径

~/Library/LaunchAgents,新建个plist文件,文件名为:wacai.bbs.auto.login.reply.plist。

注意label不要跟别的重复,写个特别点的,ProgramArguments里面写上test.sh的绝对路径,StartCalendarInterval里面配置成几点几分自动执行,最后的StandardOutPath和StandardErrorPath要不要都行,要更好,出错了可以看看错误信息。

<span style="font-size:14px;"><span style="font-size:12px;"><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict><key>Label</key><string>wacai.bbs.auto.login.reply</string><key>ProgramArguments</key><array><string>/Users/Sophie/PycharmProjects/Auto_Login_Reply_BBS_WaCai/test.sh</string></array><key>StartCalendarInterval</key><dict><key>Minute</key><integer>30</integer><key>Hour</key><integer>1</integer></dict>
<key>StandardOutPath</key>
<string>/Users/Sophie/PycharmProjects/Auto_Login_Reply_BBS_WaCai/run.log</string>
<key>StandardErrorPath</key>
<string>/Users/Sophie/PycharmProjects/Auto_Login_Reply_BBS_WaCai/runerror.log</string>
</dict>
</plist></span></span>

编辑好了之后

launchctl load wacai.bbs.auto.login.reply.plist 启用这个plist

launchctl start wacai.bbs.auto.login.reply 立即执行一次,注意,这里是那个label值,不带plist后缀的

修改plist之后,要launchctl unload .... 再 launchctl load...重新加载

还可以用launchctl list | grep wacai 来看看执行状态,一般,有了PID,并且status为0即为一切正常,否则,哪里有问题导致执行出了问题。

最后附上Log(我铜钱特别少,穷哭了已经!)以及某财网站截图,希望页面不要频繁变动,不然我就得debug改脚本了 = =#

+++++++++++++++++++++++++++++++++++++++++++++
++++挖财签到有礼06.29 02:57:03 每天签到得铜钱++++
+++++++++++++++++++++++++++++++++++++++++++++
06.29 02:57:19--Successfully login.
06.29 02:57:19--Successfully find the checkin forum ID.
06.29 02:57:19--Successfully find the thread ID.
06.29 02:57:19--Successfully find the key word.
06.29 02:57:19--Successfully auto reply.
06.29 02:57:19--Successfully find my WaCai link.
06.29 02:57:19--Successfully find my info link.
06.29 02:57:20--Successfully get my coin amount: 463.

Python requests 自动登录某财BBS,自动签到打卡领铜钱,最后再配个plist,每天自动执行相关推荐

  1. python requests模拟登录淘宝购物车下单_Python使用requests库模拟登录淘宝账号(上)...

    学好Python这款编程语言,我们能够设计出很多程序要帮助我们完成数据采集等工作,ET代理今天要跟大家介绍如何用Python模拟登录淘宝账号? 看了下网上有很多关于模拟登录淘宝,但是基本都是使用scr ...

  2. python requests模拟登录淘宝购物车下单_Python使用requests库模拟登录淘宝账号(下)...

    在文章的上部分,我们已经做好了模拟登录的前期准备,接下来就是让操作实现的部分了.一起来继续看看具体的操作步骤吧: 3.申请st码 上面我们已经申请到了淘宝(http://taobao.com)的tok ...

  3. python+requests 验证码登录

    描述:登录界面存在验证码,需要通过验证码的输入来实现登录.对于验证码的处理,我们可以通过session的方式,来保证访问的多个请求,在一个事务里面, 1.先创建一个session req = requ ...

  4. python 实现华安信达论坛自动登录

    近期需要从论坛采集一些数据,就使用爬虫专属语言python写了一个小爬虫,实现自动登录,并到指定的板块采集帖子标题.作者等信息. 实现自动登录的关键在于模拟浏览器向服务器发送数据包,用Fiddler抓 ...

  5. python网站自动答题_python自动登录网站答题-女性时尚流行美容健康娱乐mv-ida网...

    女性时尚流行美容健康娱乐mv-ida网 mvida时尚娱乐网 首页 美容 护肤 化妆技巧 发型 服饰 健康 情感 美体 美食 娱乐 明星八卦 首页  > 高级搜索 cookie实现 自动 登录 ...

  6. shell 密码输入不显示,Shell输出内容不显示密码,Shell实现有密码自动登录sshpass 应用实践...

    在很多实践项目中,我们经常会通过SSH来进行认证,如通过SSH拷贝文件,虽然我们可以使用公钥与私钥实现ssh 无密码登录,在不同的服务器上又需要配对相应的密钥,切换用户麻烦等问题,在一些需要交互但会涉 ...

  7. C# 网络编程之网页自动登录 (一).使用WebBrower控件模仿登录

    C# 网络编程之网页自动登录 (一).使用WebBrower控件模仿登录 最近学习C#网络编程中,想实现网页自动登录并提交GET/POST信息,再实现循环登录不断发送报文给服务器,服务器发送消息给客户 ...

  8. jetson nano 相关设置(开机自动登录、取消休眠和屏保、开机自启动程序)

    目录 1. 开机自动登录 2. 取消屏保 2.1. 单击左下角图标,依次进入Preferences和 Screensaver 2.2. 进入Display Modes,Mode选择Disable Sc ...

  9. iOS 开发 -- 使用KeyChain保存用户名、密码并实现自动登录

    一.前言 我的话,只是写了个keychain使用的工具类,让我们使用的时候可以直接调用接口,以求方便. 但是关于keychain的一些概念还有一些官方API我都不打算说的,当然你要看下面一些东西的话可 ...

最新文章

  1. 区块链的安全软肋是什么?
  2. Spring Boot + GraphQL 才是 API 的未来!
  3. 页面滚动到指定class样式位置
  4. Liner(分段线性插值)
  5. “编程能力差,90%输在了数学上!”CTO:多数程序员都是瞎努力!
  6. scala 函数中嵌套函数_Scala中的嵌套函数 用法和示例
  7. java中的 FileWriter类 和 FileReader类的一些基本用法
  8. python mongodb 设置密码前一篇ok,csv文件存入mongodb
  9. My Data Sructure TemplatesClass
  10. 微信小程序添加使用外部字体
  11. python3.10下载安装(附python学习教程)
  12. 莱斯康混响插件合集 – Lexicon Plugin Bundle macOS
  13. java 实现将Object类型转换为int类型
  14. java hd sex_Java学习笔记(十八)——Java DTO
  15. 微信该服务器已饱满,微信故障背后:用户91pron过亿后的小故障有大影响
  16. 计算机安全模式快捷键,windows7怎么进入安全模式(快捷键进入的方法)
  17. 产品经理学习笔记(13)-用户反馈的意义
  18. 自编码器的原始形式和各种变体
  19. 常见企业IT支撑【5、内网DNS cache轻量服务dnsmasq】
  20. Hadoop学习环境搭建

热门文章

  1. 在xen virt平台运行xen
  2. 850亿美元市场!赛盈分销洞悉办公家具行业还有哪些赛道值得入局?
  3. 苹果创始人乔布斯简介_带有乔布斯和沃兹尼亚克签名的Apple II盖子将被拍卖
  4. 常用软件广告的去除方法 转载
  5. Shell—各种括号的用法
  6. 计算机网络——码元常见编码方式
  7. 迅雷一度曾打不开torrent文件.
  8. CSS中设置所有td的内边距
  9. [SAP - HCM] 白话版SAP HR
  10. 环材化生劝退文章汇总 2019.3