我正在尝试从 coinmarketcap.com 收集市值数据。事实上,我成功地获得了市值前 10 的硬币,但在前 10 之后它就不起作用了(结果变为无)。
这是我的代码,我使用了 Chrome。
import requests
import time
from bs4 import BeautifulSoup
url = 'https://coinmarketcap.com/'
strhtml = requests.get(url)
soup = BeautifulSoup(strhtml.text, 'lxml')
result={}
baseAddr1 = '#__next > div.bywovg-1.sXmSU > div.main-content > div.sc-57oli2-0.comDep.cmc-
body-wrapper > div > div:nth-child(1) > div.h7vnx2-1.bFzXgL > table > tbody > ' //head of selector
baseAddr3 = ' > td:nth-child(3) > div > a' // end of selector
for i in range(20):
i+=1
while i%10 == 0:
time.sleep(3)
print('resting...')
break
baseAddr2 = 'tr:nth-child(' + str(i) + ')' // middle of selector, i for the order of coin
Addr = baseAddr1 + baseAddr2 + baseAddr3 // full selector
#print(Addr)
data = soup.select(Addr)
for item in data:
result.update({item.get_text(): item.get('href')})
print(result)
谢谢你的帮助!