Skip to content

Instantly share code, notes, and snippets.

@challengeYY
Last active April 10, 2019 09:57
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save challengeYY/a707a5d0bf5bc21c8142fb9252fd5c68 to your computer and use it in GitHub Desktop.
Save challengeYY/a707a5d0bf5bc21c8142fb9252fd5c68 to your computer and use it in GitHub Desktop.
hostloc论坛每日翻牌
#encoding=utf-8
import requests
from bs4 import BeautifulSoup
import re
import sys
import datetime
uid="11111" #用户名
pwd="11111" #密码
http = requests.Session()
http.headers.update({
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'
,'Accept-Language':"zh-CN,zh;q=0.8,ko;q=0.6,zh-TW;q=0.4"
})
#http.proxies = {"http":"http://127.0.0.1:8080","https":"http://127.0.0.1:8080"}
##打开登陆界面
res=http.get("http://www.hostloc.com/member.php?mod=logging&action=login&infloat=yes&handlekey=login&inajax=1&ajaxtarget=fwin_content_login")
match=re.search(r'name="formhash" value="(\S+)"',res.text)
if(match):
formhash=match.group(1)
else:
exit(0)
##登陆
form={
"formhash":formhash
,"referer":"http://www.hostloc.com/thread-12949-1-1.html"
,"loginfield":"username"
,"username":uid
,"password":pwd
,"questionid":0
,"answer":""
,"loginsubmit":"true"
}
res=http.post("http://www.hostloc.com/member.php?mod=logging&action=login&loginsubmit=yes&handlekey=login&loginhash=LWKbr&inajax=1",data=form)
match=re.search(r"'uid':'",res.text)
if(match):
print("登陆成功")
else:
print("登陆失败")
exit(0)
##查询今天访问的空间数量
res=http.get("http://www.hostloc.com/home.php?mod=spacecp&ac=credit&op=log&suboperation=creditrulelog")
bs=BeautifulSoup(res.text,"html.parser")
td=bs.find('td',string="访问别人空间")
if(td==None):
print("信息获取失败")
exit(0)
tds=td.parent.find_all("td")
today_view_count=int(tds[2].text) #今天已经翻牌数
last_view_date=tds[5].text #上次翻牌时间,这个时间不会自动刷新,所以不能仅仅依据数量就决定不翻牌
need_view=last_view_date.find(datetime.datetime.now().strftime("%Y-%m-%d"))==-1 #上次翻牌时间是不是今天,不是今天则需要翻
if(today_view_count>=10 and (not need_view)): #不论数量多少,只要上次翻牌时间不是今天就翻。上次翻牌是今天才去判断数量
print("今日累了,明日再翻!")
exit(0)
##去首页获取等待临幸的网址
res=http.get("http://www.hostloc.com/forum-45-1.html")
users =re.findall("(space-uid\S+)\"",res.text)
viewed=set()
num=0
while num <13:
url = users.pop()
if(url in viewed):continue
viewed.add(url)
print(url)
res=http.get('http://www.hostloc.com/'+url)
users.extend(re.findall("(space-uid\S+)\"",res.text))
num+=1
print("今日累了,明日再翻!")
@challengeYY
Copy link
Author

challengeYY commented Sep 1, 2016

1:使用步骤:

apt-get -y install python-pip
pip install requests
pip install beautifulsoup4
#pip install lxml
#安装cron
#apt-get install cron
#crontab -e
#0 */8 * * * python /tmp/hostloc.py    
#每隔八小时自动操作一次
wget  -O  /tmp/hostloc.py https://gist.githubusercontent.com/challengeYY/a707a5d0bf5bc21c8142fb9252fd5c68/raw/b2de204854263c74a87b6c169648e269d1c2ff2b/hostloc.py

2:修改脚本里面的用户名和密码。

3:将脚本加入cron定时任务中。

@challengeYY
Copy link
Author

update >> 2016-9-2 6:43

上次翻牌数量不会自动更新,因此要配合上次翻牌时间决定是否放弃翻牌。

@hangim
Copy link

hangim commented Sep 2, 2016

you forget
import datetime
at line 6

@challengeYY
Copy link
Author

恩,忘记补上了。

@hangim
Copy link

hangim commented Sep 15, 2016

然而这段代码并不能破金盾硬防。。。

@V-TW-00
Copy link

V-TW-00 commented Nov 13, 2016

自己写了个,随便翻

@MagicHen25
Copy link

UnicodeDecodeError: 'utf8' codec can't decode byte 0xb7 in position 0: invalid start byte

我是刚开始学习,编码问题搞的头大,我自己慢慢调吧

@xudbin
Copy link

xudbin commented Apr 11, 2017

hostloc 貌似改版了, 获取 formhash失败

@BFDZ
Copy link

BFDZ commented Sep 7, 2018

上https了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment