【问题标题】:Why is my program not printing data from the table I am scraping?为什么我的程序没有从我正在抓取的表中打印数据?
【发布时间】:2021-07-29 23:44:58
【问题描述】:

我目前正在努力创建一个从https://coinmarketcap.com 上的表中抓取数据的程序。我看到我有点过头了。但是,我正在尝试了解如何能够自己完成这一切。到目前为止,我的程序打印了加密货币排名、名称和股票代码。现在,我正在努力从表格中抓取动态变化的价格。这是我的代码:

import requests
from bs4 import BeautifulSoup

url = "https://coinmarketcap.com"
soup = BeautifulSoup(requests.get(url).content, "html.parser")
rank = 1

for td in soup.select("td:nth-of-type(3)"):
    t = " ".join(tag.text for tag in td.select("p, span")).strip()
    print(rank, "|", end =" "); print("{:<30} {:<10}".format(*t.rsplit(maxsplit=1)))
    rank = rank + 1

    for td in soup.select("td:nth-of-type(4)"):
        t = " ".join(tag.text for tag in td.select("a")).strip()

    print("{}_1d".format(t.rsplit(maxsplit=1)))

打印如下:

1 | Bitcoin                        BTC
[]_1d
2 | Ethereum                       ETH
[]_1d
3 | Tether                         USDT
[]_1d
4 | Binance Coin                   BNB
[]_1d


and so on...

如何让它打印加密货币的当前价格而不仅仅是文字?我可以自己弄清楚格式,只需要帮助显示实际数据。任何帮助是极大的赞赏。如果您能解释您的解决方案,那将更有帮助。

【问题讨论】:

    标签: python beautifulsoup html-table cryptocurrency inspect


    【解决方案1】:

    我在您的代码中发现了以下问题:

    • 您的print("{}_1d".format(t.rsplit(maxsplit=1))) 行位于内部for 循环之外,这只会打印t 的最后一个值(为空)。 因此,需要更正此问题以将其放入循环中,同时更改为不打印每个 t 值。
    • 您已将价格循环 (td:nth-of-type(4)) 放入 td:nth-of-type(3) 循环中。这使得每次外部循环运行一次时,整个价格循环都会重复运行
    • 如果您在循环中使用 (td:nth-of-type(4)) 打印 td 的值,您会发现在前约 10 个结果之后该标签不在您的响应中。使用 td.text 可以获得所需的结果。

    我稍微修改了您的代码以解决一些问题:

    import requests
    from bs4 import BeautifulSoup
    
    url = "https://coinmarketcap.com"
    soup = BeautifulSoup(requests.get(url).content, "html.parser")
    rank = 1
    
    t1, t2 = [], []
    
    for td in soup.select("td:nth-of-type(3)"):
        t1.append(" ".join(tag.text for tag in td.select("p, span")).strip())
    
    for td in soup.select("td:nth-of-type(4)"):
        t2.append(td.text)
    
    for i in range(0, len(t1)):
        rank = rank + 1
        print(rank, "|", end =" "); print("{:<30} {:<10}".format(*t1[i].rsplit(maxsplit=1)))
        print("{}_1d".format(t2[i]))
    

    【讨论】:

    • 感谢您的帮助。我在下一列(24 小时百分比)中尝试了同样的策略。但是,即使在使用 td.text 时,它也只输出前 10 个值,我错过了什么吗?
    • 从您在问题中编写的代码来看,您似乎正在尝试使用锚标记获取值。而且,由于锚标记仅出现在前 10 个值中,因此您无法输出更多。我认为当你使用td.text 时,你可能已经做了类似的事情,它给了你锚标签,因此只有前 10 个值。
    【解决方案2】:

    要获取加密货币的价格,您可以使用下一个示例:

    import requests
    from bs4 import BeautifulSoup
    
    url = "https://coinmarketcap.com"
    soup = BeautifulSoup(requests.get(url).content, "html.parser")
    
    for rank, td in enumerate(soup.select("td:nth-of-type(3)"), 1):
        t = " ".join(tag.text for tag in td.select("p, span")).strip()
        price = td.find_next("td").text
        print(
            "{:<3} | {:<30} {:<10} {:<10}".format(
                rank, *t.rsplit(maxsplit=1), price
            )
        )
    

    打印:

    1   | Bitcoin                        BTC        $39,971.37
    2   | Ethereum                       ETH        $2,320.27 
    3   | Tether                         USDT       $1.00     
    4   | Binance Coin                   BNB        $314.10   
    5   | Cardano                        ADA        $1.28     
    6   | XRP                            XRP        $0.7078   
    7   | USD Coin                       USDC       $1.00     
    8   | Dogecoin                       DOGE       $0.2043   
    9   | Polkadot                       DOT        $15.11    
    10  | Binance USD                    BUSD       $1.00     
    11  | Uniswap                        UNI        $19.24    
    12  | Bitcoin Cash                   BCH        $517.07   
    13  | Litecoin                       LTC        $140.07   
    14  | Chainlink                      LINK       $19.02    
    15  | Solana                         SOL        $29.55    
    16  | Wrapped Bitcoin                WBTC       $39923.81 
    17  | Polygon                        MATIC      $1.02     
    18  | Stellar                        XLM        $0.27     
    19  | Ethereum Classic               ETC        $48.75    
    20  | THETA                          THETA      $5.99     
    
    ...and so on.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-18
      • 2021-03-06
      • 2021-12-29
      • 1970-01-01
      • 2019-09-17
      相关资源
      最近更新 更多