Created
September 30, 2019 16:14
-
-
Save caspartse/b8de152f4a5955ac99c5a6c2f5460063 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # -*- coding:utf-8 -*- | |
| from time import time, sleep | |
| import requests | |
| import re | |
| import ujson as json | |
| import redis | |
| rd = redis.Redis() | |
| def getFunds(): | |
| sess = requests.Session() | |
| headers = { | |
| 'Host': 'fund.jd.com', | |
| 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0', | |
| 'Accept': 'text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01', | |
| 'Accept-Language': 'en-US,en;q=0.5', | |
| 'Accept-Encoding': 'gzip, deflate', | |
| 'X-Requested-With': 'XMLHttpRequest', | |
| 'Referer': 'http://fund.jd.com/', | |
| 'DNT': '1', | |
| 'Connection': 'keep-alive' | |
| } | |
| sess.headers.update(headers) | |
| funds = [] | |
| url = 'http://fund.jd.com/getLeftTab' | |
| ops = { | |
| '60:202': u'股票型', | |
| '60:203': u'混合型', | |
| '60:206': u'QDII', | |
| '60:207': u'指数型', | |
| } | |
| for op in ops.iterkeys(): | |
| i = 1 | |
| loop = 1 | |
| while loop: | |
| params = { | |
| 'callback': 'jQuery18309142654922922692_%d' % (time() * 1000), | |
| 'sort': '', | |
| 'theme': '', | |
| 'industry': '', | |
| 'op': op, | |
| 'man': '', | |
| 'style': '', | |
| 'years': '', | |
| 'risk': '', | |
| 'label': '', | |
| 'sortType': '31', | |
| 'page': i, | |
| 'q': '' | |
| } | |
| resp = sess.get(url, params=params) | |
| if resp.status_code == 200: | |
| pattern = r'^jQuery\d{20}_\d{13}\(|\)$' | |
| raw = re.sub(pattern, '', resp.content) | |
| items = json.loads(raw)['items'] | |
| for fund in items: | |
| print fund['itemCode'] | |
| funds.append(str(fund['itemCode'])) | |
| pageCount = json.loads(raw)['pageCount'] | |
| i += 1 | |
| if i > pageCount: | |
| break | |
| return funds | |
| def getTotalPrice(code): | |
| try: | |
| sleep(0.1) | |
| url = 'http://fund.stockstar.com/funds/f10/fundjz.aspx?code=%s' % (code) | |
| resp = requests.get(url) | |
| content = resp.content | |
| pattern = r'<td align="right" style="padding-right:10px;">([^<]+)</td>' | |
| totalPrice = re.findall(pattern, content)[1] | |
| rd.set(code, totalPrice) | |
| except Exception, e: | |
| print e | |
| if __name__ == '__main__': | |
| funds = getFunds() | |
| map(getTotalPrice, funds[5:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment