[英]How can I web scrape certain words that don't have an attribute attached to them?
首先,我想指出,我是網絡抓取的初學者。 我剛剛開始一個從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 行,但很難弄清楚這一點。 我很感激你能給我的任何幫助或建議。
要從此頁面獲取加密貨幣的名稱和代碼,您可以使用下一個示例:
import requests
from bs4 import BeautifulSoup
url = "https://coinmarketcap.com"
soup = BeautifulSoup(requests.get(url).content, "html.parser")
for td in soup.select("td:nth-of-type(3)"):
t = " ".join(tag.text for tag in td.select("p, span")).strip()
print("{:<30} {:<10}".format(*t.rsplit(maxsplit=1)))
印刷:
Bitcoin BTC
Ethereum ETH
Tether USDT
Binance Coin BNB
Cardano ADA
XRP XRP
USD Coin USDC
Dogecoin DOGE
Polkadot DOT
Binance USD BUSD
Uniswap UNI
Bitcoin Cash BCH
Litecoin LTC
Chainlink LINK
Solana SOL
Wrapped Bitcoin WBTC
Polygon MATIC
Ethereum Classic ETC
Stellar XLM
THETA THETA
...and so on.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.