Python抓取同花顺股市直播
一直在同花顺炒股软件中关注“阿狸策略”的股市直播,手机版的看着太累,就转到web版时不时看看,圈子改版以后,股市直播变成了群聊模式,看的头皮气昏,于是就花了点时间写了个小玩意。
已实现功能:
- 过滤群聊信息,只看圈主
- 同时关注多人直播
- 支持“仅成员可见”的信息
后续打算加上:
- 自动关注
- 发言,交流功能
最终效果是这样的:
代码如下:
环境:MacBook Air, OS X Yosemite, Python3.5, BeautifulSoup4.2.0, Requests2.8.0
import json
import time
import requests
from bs4 import BeautifulSoup
base_url = 'http://t.10jqka.com.cn/newcircle/live/getChatList/?fid={fid}&sort=down'
rs = requests.Session()
"""阿狸策略是直接抓取的,崔总实盘是需要登录并关注后才能抓取的
也可以把自己的老师加到下面的字典里
键名是直播室的ID,列表的第一个元素是老师的ID,第二个元素是老师姓名。
第三个元素不用管"""
teachers = {
'12198':['280082619','阿狸策略', None],
'10412':['216003609','崔总实盘', None]}
headers = {
"Accept":"application/json, text/javascript, */*; q=0.01",
"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 (KHTML, like Gecko) Version/8.0.8 Safari/600.8.9",
"Referer":"http://t.10jqka.com.cn/circle/10412/",
"Accept-Language":"zh-cn",
"X-Requested-With":"XMLHttpRequest"}
def login(username, password):
url = "http://pass.10jqka.com.cn/login"
data = {
'act':"loginByJsonp",
'username': username,
'password': password,
'longLogin':'true'}
now = int(time.time()*1000)
data['callback'] = 'jQuery1110020031836070120335_' + str(now)
data['_'] = str(now)
# 是的,这个奇葩就是用GET提交的
r = rs.get(url, params=data)
r_json = r.text.split('(')[1].split(')')[0]
r_json = json.loads(r_json)
if r_json['errorcode'] == 0:
print("登录成功")
return True
else:
print(r_json['errormsg'])
return False
def getChatList(fid):
result = []
url = base_url.replace('{fid}', fid)
if teachers[fid][2]:
url = url + '&pid=' + teachers[fid][2]
try:
r = rs.get(url, headers=headers).json()
except:
time.sleep(5)
return getChatList(fid)
if r['errorCode'] == 0:
soup = BeautifulSoup(r['result'], "html.parser")
data_all = soup.find_all('li', class_="chat-data-single chat-list-ot clearfix")
if len(data_all) > 0:
data_all = data_all[-1]
teachers[fid][2] = data_all['data-pid']
teacher_chat = soup.find_all('li', attrs={"data-userid": teachers[fid][0]})
for tc in teacher_chat:
tm = tc.find(class_='chat-time')
tt = tc.find(class_='chat-txt')
result.append((teachers[fid][1], tm, tt))
return result
if __name__ == '__main__':
login('your_username', 'your_password')
while 1:
for k in teachers:
lis = getChatList(k)
for l in lis:
tm = l[1].get_text().strip()
tt = l[2].get_text().strip()
word = "%s %s:\n%s\n\n"%(l[0], tm, tt)
print(word)
time.sleep(1)
time.sleep(15)
xie_de_zhen_hao
不错 666666666666666666666