我不知道如何从此类中提取文本
<a href="/all/views/all/" class="cmc-link">7,457</a>
我想获取7,457
,但我不知道如何...我尝试了此方法,但它只向我显示了链接
from bs4 import BeautifulSoup
url = 'https://coinmarketcap.com/'
response = requests.get(url)
soup = BeautifulSoup(response.text)
anchors = soup.find_all('a', {'class': 'cmc-link', 'href': True})
for anchor in anchors:
print (anchor['href'])
答案 0 :(得分:1)
import requests
from bs4 import BeautifulSoup
url = 'https://coinmarketcap.com/'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')
print(soup.select_one('span:contains("Cryptocurrencies") a').text)
打印:
7,457
答案 1 :(得分:0)
使用anchor['href']
仅return
的{{1}}属性。使用href
来获取文本。这是您的操作方式:
anchor tag
这是完整的代码:
.text
答案 2 :(得分:0)
使用.text
属性(将返回锚点或任何其他元素的内部文本,其中['href']
将返回href属性值):
from bs4 import BeautifulSoup
url = 'https://coinmarketcap.com/'
response = requests.get(url)
soup = BeautifulSoup(response.text)
anchors = soup.find_all('a', {'class': 'cmc-link', 'href': True})
for anchor in anchors:
print(anchor.text)