如何从 Coinmarketcap 中提取所有加密符号
How to Extract All Crypto Symbols from Coinmarketcap
我正在使用以下代码通过一个一个地插入硬币的符号从 Coinmarketcap 中提取一些数据,但是由于每天都有新的密码添加到列表中,我需要一次从中提取所有密码https://coinmarketcap.com/coins/views/all/ 为了每天都有更新的列表。我如何修改以下代码,以便我首先将所有加密符号都放在一个文本文件中,然后再执行其余的操作?非常感谢您的帮助。
import time
import requests
a = []
while True:
try:
x = input("Type the symbols of The currencies. Write 'done' when you are done: ")
open('/Users/DearUser/crypto_symbol_3.txt', 'a').close()
if x.upper() == "DONE":
break
def get_timestamp(datetime: str):
return int(time.mktime(time.strptime(datetime, '%Y-%m-%d')))
def get_btc_quotes(start_date: str, end_date: str):
start = get_timestamp(start_date)
end = get_timestamp(end_date)
url = f'https://web-api.coinmarketcap.com/v1/cryptocurrency/ohlcv/historical?symbol={x}&convert=USD&time_start={start}&time_end={end}'
return requests.get(url).json()
data = get_btc_quotes(start_date='2021-11-09',
end_date='2021-11-11')
import pandas as pd
data_flat = [quote['quote']['USD'] for quote in data['data']['quotes']]
df = pd.DataFrame(data_flat)
c = df.iat[1,5]
if c==0:
print("The marketcap of your entry is zero. Please try another one...")
if x not in open('/Users/DearUser/crypto_symbol_3.txt', 'r').read() and c!=0:
open('/Users/DearUser/crypto_symbol_3.txt', 'a').write(f'\"{x}\": {c},\n')
elif x in open('/Users/DearUser/crypto_symbol_3.txt', 'r').read():
print("Your entry already exists! Please try again...")
except:
print("Your input is not valid! Please try again...")
s = open('/Users/DearUser/crypto_symbol_3.txt', 'r').read()
print(s)
似乎 API 有这个 Listings Latest 的功能。
此功能的文档甚至提到您在问题中添加的页面
https://coinmarketcap.com/coins/views/all/
函数有 limit
5000 个符号,但如果您更改 start
,那么您可以获得下一个符号。
这样我得到 7541
个符号。
import requests
url = 'https://web-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
for start in range(1, 20000, 5000):
params = {
'start': start,
'limit': 5000,
}
r = requests.get(url, params=params)
data = r.json()
for number, item in enumerate(data['data']):
print(f"{start+number:4} | {item['symbol']:5} | {item['date_added'][:10]}")
结果:(前 100 个带有 date_added
的符号)
1 | BTC | 2013-04-28
2 | ETH | 2015-08-07
3 | BNB | 2017-07-25
4 | USDT | 2015-02-25
5 | SOL | 2020-04-10
6 | ADA | 2017-10-01
7 | XRP | 2013-08-04
8 | DOT | 2020-08-19
9 | USDC | 2018-10-08
10 | DOGE | 2013-12-15
11 | SHIB | 2020-08-01
12 | AVAX | 2020-07-13
13 | LUNA | 2019-07-26
14 | LTC | 2013-04-28
15 | WBTC | 2019-01-30
16 | BUSD | 2019-09-20
17 | CRO | 2018-12-14
18 | UNI | 2020-09-17
19 | LINK | 2017-09-20
20 | ALGO | 2019-06-20
21 | BCH | 2017-07-23
22 | MATIC | 2019-04-28
23 | VET | 2017-08-22
24 | XLM | 2014-08-05
25 | AXS | 2020-08-31
26 | MANA | 2017-09-17
27 | ICP | 2021-03-23
28 | TRX | 2017-09-13
29 | FTT | 2019-07-31
30 | UST | 2020-09-21
31 | FIL | 2017-12-13
32 | DAI | 2019-11-22
33 | ETC | 2016-07-24
34 | EGLD | 2020-09-04
35 | ATOM | 2019-03-14
36 | THETA | 2018-01-17
37 | BTCB | 2019-06-18
38 | HBAR | 2019-09-17
39 | NEAR | 2020-08-11
40 | FTM | 2018-10-29
41 | XTZ | 2017-10-06
42 | GRT | 2020-12-17
43 | XMR | 2014-05-21
44 | HNT | 2020-06-05
45 | EOS | 2017-07-01
46 | SAND | 2020-08-05
47 | KLAY | 2020-03-30
48 | FLOW | 2021-01-27
49 | CAKE | 2020-09-25
50 | AAVE | 2020-10-02
51 | MIOTA | 2017-06-13
52 | XEC | 2021-07-08
53 | KDA | 2020-05-31
54 | LRC | 2017-08-30
55 | LEO | 2019-05-21
56 | KSM | 2019-12-12
57 | NEO | 2016-09-08
58 | BSV | 2018-11-09
59 | MKR | 2017-01-29
60 | QNT | 2018-08-10
61 | ENJ | 2017-11-01
62 | CHZ | 2019-07-01
63 | RUNE | 2019-07-23
64 | ONE | 2019-06-01
65 | STX | 2019-10-28
66 | WAVES | 2016-06-02
67 | BTT | 2019-01-31
68 | HOT | 2018-04-29
69 | AMP | 2020-09-08
70 | ZEC | 2016-10-29
71 | AR | 2020-05-27
72 | DASH | 2014-02-14
73 | COMP | 2020-06-16
74 | KCS | 2017-10-24
75 | CELO | 2020-05-22
76 | CRV | 2020-08-14
77 | NEXO | 2018-05-01
78 | IOTX | 2018-05-25
79 | TFUEL | 2019-03-28
80 | WAXP | 2017-12-21
81 | HT | 2018-02-03
82 | XEM | 2015-04-01
83 | BAT | 2017-06-01
84 | OKB | 2019-04-30
85 | DCR | 2016-02-10
86 | ICX | 2017-10-27
87 | QTUM | 2017-05-24
88 | OMG | 2017-07-14
89 | TUSD | 2018-03-06
90 | MINA | 2021-03-02
91 | YFI | 2020-07-18
92 | RVN | 2018-03-10
93 | VGX | 2017-07-18
94 | SUSHI | 2020-08-28
95 | UMA | 2020-05-25
96 | AUDIO | 2020-10-20
97 | REV | 2017-11-01
98 | SCRT | 2020-05-19
99 | ZIL | 2018-01-25
100 | VLX | 2019-10-03
我正在使用以下代码通过一个一个地插入硬币的符号从 Coinmarketcap 中提取一些数据,但是由于每天都有新的密码添加到列表中,我需要一次从中提取所有密码https://coinmarketcap.com/coins/views/all/ 为了每天都有更新的列表。我如何修改以下代码,以便我首先将所有加密符号都放在一个文本文件中,然后再执行其余的操作?非常感谢您的帮助。
import time
import requests
a = []
while True:
try:
x = input("Type the symbols of The currencies. Write 'done' when you are done: ")
open('/Users/DearUser/crypto_symbol_3.txt', 'a').close()
if x.upper() == "DONE":
break
def get_timestamp(datetime: str):
return int(time.mktime(time.strptime(datetime, '%Y-%m-%d')))
def get_btc_quotes(start_date: str, end_date: str):
start = get_timestamp(start_date)
end = get_timestamp(end_date)
url = f'https://web-api.coinmarketcap.com/v1/cryptocurrency/ohlcv/historical?symbol={x}&convert=USD&time_start={start}&time_end={end}'
return requests.get(url).json()
data = get_btc_quotes(start_date='2021-11-09',
end_date='2021-11-11')
import pandas as pd
data_flat = [quote['quote']['USD'] for quote in data['data']['quotes']]
df = pd.DataFrame(data_flat)
c = df.iat[1,5]
if c==0:
print("The marketcap of your entry is zero. Please try another one...")
if x not in open('/Users/DearUser/crypto_symbol_3.txt', 'r').read() and c!=0:
open('/Users/DearUser/crypto_symbol_3.txt', 'a').write(f'\"{x}\": {c},\n')
elif x in open('/Users/DearUser/crypto_symbol_3.txt', 'r').read():
print("Your entry already exists! Please try again...")
except:
print("Your input is not valid! Please try again...")
s = open('/Users/DearUser/crypto_symbol_3.txt', 'r').read()
print(s)
似乎 API 有这个 Listings Latest 的功能。
此功能的文档甚至提到您在问题中添加的页面
https://coinmarketcap.com/coins/views/all/
函数有 limit
5000 个符号,但如果您更改 start
,那么您可以获得下一个符号。
这样我得到 7541
个符号。
import requests
url = 'https://web-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
for start in range(1, 20000, 5000):
params = {
'start': start,
'limit': 5000,
}
r = requests.get(url, params=params)
data = r.json()
for number, item in enumerate(data['data']):
print(f"{start+number:4} | {item['symbol']:5} | {item['date_added'][:10]}")
结果:(前 100 个带有 date_added
的符号)
1 | BTC | 2013-04-28
2 | ETH | 2015-08-07
3 | BNB | 2017-07-25
4 | USDT | 2015-02-25
5 | SOL | 2020-04-10
6 | ADA | 2017-10-01
7 | XRP | 2013-08-04
8 | DOT | 2020-08-19
9 | USDC | 2018-10-08
10 | DOGE | 2013-12-15
11 | SHIB | 2020-08-01
12 | AVAX | 2020-07-13
13 | LUNA | 2019-07-26
14 | LTC | 2013-04-28
15 | WBTC | 2019-01-30
16 | BUSD | 2019-09-20
17 | CRO | 2018-12-14
18 | UNI | 2020-09-17
19 | LINK | 2017-09-20
20 | ALGO | 2019-06-20
21 | BCH | 2017-07-23
22 | MATIC | 2019-04-28
23 | VET | 2017-08-22
24 | XLM | 2014-08-05
25 | AXS | 2020-08-31
26 | MANA | 2017-09-17
27 | ICP | 2021-03-23
28 | TRX | 2017-09-13
29 | FTT | 2019-07-31
30 | UST | 2020-09-21
31 | FIL | 2017-12-13
32 | DAI | 2019-11-22
33 | ETC | 2016-07-24
34 | EGLD | 2020-09-04
35 | ATOM | 2019-03-14
36 | THETA | 2018-01-17
37 | BTCB | 2019-06-18
38 | HBAR | 2019-09-17
39 | NEAR | 2020-08-11
40 | FTM | 2018-10-29
41 | XTZ | 2017-10-06
42 | GRT | 2020-12-17
43 | XMR | 2014-05-21
44 | HNT | 2020-06-05
45 | EOS | 2017-07-01
46 | SAND | 2020-08-05
47 | KLAY | 2020-03-30
48 | FLOW | 2021-01-27
49 | CAKE | 2020-09-25
50 | AAVE | 2020-10-02
51 | MIOTA | 2017-06-13
52 | XEC | 2021-07-08
53 | KDA | 2020-05-31
54 | LRC | 2017-08-30
55 | LEO | 2019-05-21
56 | KSM | 2019-12-12
57 | NEO | 2016-09-08
58 | BSV | 2018-11-09
59 | MKR | 2017-01-29
60 | QNT | 2018-08-10
61 | ENJ | 2017-11-01
62 | CHZ | 2019-07-01
63 | RUNE | 2019-07-23
64 | ONE | 2019-06-01
65 | STX | 2019-10-28
66 | WAVES | 2016-06-02
67 | BTT | 2019-01-31
68 | HOT | 2018-04-29
69 | AMP | 2020-09-08
70 | ZEC | 2016-10-29
71 | AR | 2020-05-27
72 | DASH | 2014-02-14
73 | COMP | 2020-06-16
74 | KCS | 2017-10-24
75 | CELO | 2020-05-22
76 | CRV | 2020-08-14
77 | NEXO | 2018-05-01
78 | IOTX | 2018-05-25
79 | TFUEL | 2019-03-28
80 | WAXP | 2017-12-21
81 | HT | 2018-02-03
82 | XEM | 2015-04-01
83 | BAT | 2017-06-01
84 | OKB | 2019-04-30
85 | DCR | 2016-02-10
86 | ICX | 2017-10-27
87 | QTUM | 2017-05-24
88 | OMG | 2017-07-14
89 | TUSD | 2018-03-06
90 | MINA | 2021-03-02
91 | YFI | 2020-07-18
92 | RVN | 2018-03-10
93 | VGX | 2017-07-18
94 | SUSHI | 2020-08-28
95 | UMA | 2020-05-25
96 | AUDIO | 2020-10-20
97 | REV | 2017-11-01
98 | SCRT | 2020-05-19
99 | ZIL | 2018-01-25
100 | VLX | 2019-10-03