首先,我想指出我是网络抓取的初学者。我刚刚开始一个从https://coinmarketcap.com刮取数据的项目。目前,我专注于抓取加密货币的名称(即比特币、以太坊、Tether 等)。但是,我能得到的最好的结果是货币的名称,后跟一堆格式,如颜色、字体大小、类等。我该如何编码,以便我可以只存储货币的名称而没有这个额外的信息。这是我当前的代码:
import requests
from bs4 import BeautifulSoup
#array of just crypto names
names = []
#gets content from site
site = requests.get("https://coinmarketcap.com")
#opens content from site
info = site.content
soup = BeautifulSoup(info,"html.parser")
#class ID for name of crypto
type_name = 'sc-1eb5slv-0 iJjGCS'
#crypto names + other unnecessary info
names_raw = soup.find_all('p', attrs={'class': 'sc-1eb5slv-0 iJjGCS'})
for type_name in names_raw:
print(type_name.text, type_name.next_sibling)
如果图片更有用: 我当前的代码
如您所见,我只有 20 行,但很难弄清楚这一点。我很感激你能给我的任何帮助或建议。