gpt4 book ai didi

Python -BeautifulSoup - 如何定位第 n 个 child 并打印文本

转载 作者:行者123 更新时间:2023-12-02 18:09:55 24 4
gpt4 key购买 nike

我正在尝试抓取 https://coinmarketcap.com/ 上的“最大 yield 者”硬币列表

如何访问 div class_ = 'sc-1rmt1nr-0 sc-1rmt1nr-2 iMyvIy' 中的第 n 个子级(最大 yield 者)

我设法从“趋势”部分获取数据,但在定位“最大 yield 者”前 3 个文本项时遇到困难。

我收到 AttributeError: 'NoneType' 对象没有属性 'p'

from bs4 import BeautifulSoup
import requests


source = requests.get('https://coinmarketcap.com/').text

soup = BeautifulSoup(source, 'lxml')

section = soup.find(class_='sc-1rmt1nr-0 sc-1rmt1nr-2 iMyvIy')

#List the top 3 Gainers
for top_gainers in section.find_all(class_='sc-16r8icm-0 sc-1uagfi2-0 bdEGog sc-1rmt1nr-1 eCWTbV')[1]:
top_gainers = top_gainers.find(class_='sc-1eb5slv-0 iworPT')
top_coins = top_gainers.p.text
print(top_coins)

最佳答案

我会避免使用这些动态类,而是使用 -:soup-contains 和组合器首先通过文本定位所需的 block ,然后使用组合器指定要从中提取信息的最终元素的关系。

import requests
from bs4 import BeautifulSoup as bs
import pandas as pd

soup = bs(requests.get("https://coinmarketcap.com/").text, "lxml")
biggest_gainers = []

for i in soup.select(
'div[color=text]:has(span:-soup-contains("Biggest Gainers")) > div ~ div'
):
biggest_gainers.append(
{
"rank": int(i.select_one(".rank").text),
"currency": i.select_one(".alias").text,
"% change": f"{i.select_one('.icon-Caret-up').next_sibling}",
}
)

gainers = pd.DataFrame(biggest_gainers)
gainers

关于Python -BeautifulSoup - 如何定位第 n 个 child 并打印文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72563411/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com