同花順問財非常有名,可以根據自然描述獲取選股結果,這里寫了個小工具,也就是爬蟲,來獲取爬取結果
實現的功能:
1.多頁數據全爬取
2.數據解析
歡迎加VX號(edgarcaixm)交流
# -*- coding: utf8 -*-
import requests
from ghost import Ghost
from requests.cookies import RequestsCookieJar
import re
import json
import types
import sys
def printlist(resultList):
if type(resultList) is list:
for i in resultList:
printlist(i)
else:
if type(resultList) is bytes:
print(resultList.encode('utf-8'))
else:
print(resultList)
def dosearch(argname):
# reload(sys)
# sys.setdefaultencoding('utf-8')
ghost = Ghost()
cj = RequestsCookieJar()
headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept - Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Connection': 'Keep-Alive',
'Host': 'www.iwencai.com',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36'}
print(u"同花順問財選股開始")
# 獲取同花順為每個股票代碼設定的fid
parms = {'typed': '0', 'f': '1', 'qs': 'result_original', 'tid': 'stockpick', 'ts': '1',
'w': argname}
url = 'http://www.iwencai.com/stockpick/search'
with ghost.start() as session:
session.delete_cookies()
session.open(url, method="get", timeout=30)
session.save_cookies(cj)
s = requests.Session()
requestPage = s.get(url, params=parms, headers=headers, timeout=3,cookies=cj)
match=re.search(r'allResult = {(.+)};',requestPage.text)
stockinfo=''
if match :
stockinfo = str(match.group(1))
stocklist="{"
stocklist += stockinfo
stocklist += "}"
stockinfo ="{"+stockinfo+"}"
decodejson = json.loads(stockinfo)
total = str(decodejson['total'])
print('總共有:%s個,分別為:' % total)
wccode2hq = decodejson['wccode2hq']
for j in wccode2hq:
print(j)
title = str(decodejson['title'])
# print('標題:%s' % title.decode('unicode-escape').encode('utf-8'))
print('標題:%s' % title)
result = decodejson['result']
dataidx=1
if type(result) is list:
for i in result:
print('------第%d行數據打印開始------'%dataidx)
printlist(i)
# print('------第%d行數據打印結束--------'%dataidx)
dataidx+=1
def main():
args = '本周板塊指數創新高 本周成交量/上周成交量 周陽線 由高到低排序'
args = '板塊指數 本周成交量/上周成交量 由高到低排序 周陽線'
# 所屬概念包含XXX 本周成交量/上周成交量 由高到低 周陽線
# args = '所屬概念包含白馬股'
dosearch(args)
if __name__ == '__main__':
main()
非常好的模式,讓我今年收益翻倍的文章,強烈推薦:
2020年4月至6月凈利潤斷層實戰小結-真香~