用 BeautifulSoup 刮硬币市场

Lau*_*ent 1 python beautifulsoup web-scraping

我想抓取此页面中包含的所有数据。不幸的是,我只能提取前三行。

import requests
from bs4 import BeautifulSoup

response = requests.get("https://www.coingecko.com/fr/pièces/bitcoin#markets")

soup = BeautifulSoup(response.text, "html.parser")
My_table = soup.find("table",{"class":"table table-scrollable"})
My_table
data = []
rows = My_table.find_all('tr')
for row in rows:
    cols = row.find_all('td')
    cols = [ele.text.strip() for ele in cols]
    data.append([ele for ele in cols if ele]) # Get rid of empty values
data 
Run Code Online (Sandbox Code Playgroud)

感谢您的帮助

And*_*ely 5

您在页面上看到的数据是通过 Javascript 加载的。如果您打开 Firefox/Chrome 选项卡,您可以看到数据的来源。

您可以使用此脚本打印一些数据:

import requests
from bs4 import BeautifulSoup

url = 'https://www.coingecko.com/fr/pi%C3%A8ces/1/markets_tab'

soup = BeautifulSoup(requests.get(url).content, 'html.parser')

data = []
for tr in soup.select('tr[class]'):
    if 'sponsored' in tr['class']:
        continue
    _, name, paire, cours, spread, prof, *_ = tr.select('td')

    data.append( (name.get_text(strip=True),
                  paire.get_text(strip=True),
                  cours.div.get_text(strip=True),
                  spread.get_text(strip=True),
                  prof.get_text(strip=True),
                  ) )

# print the data:
print('{:<4} {:<30} {:<20} {:<20} {:<10} {:<20}'.format('No.', 'Name', 'Paire', 'Cours', 'Spread', '+2 % de profondeur'))
for i, row in enumerate(data, 1):
    print('{:<4} {:<30} {:<20} {:<20} {:<10} {:<20}'.format(i, *row))
Run Code Online (Sandbox Code Playgroud)

印刷:

No.  Name                           Paire                Cours                Spread     +2 % de profondeur  
1    Binance                        BTC/USDT             8 094,45 $           0.02%      1 633 105 $         
2    Bitfinex                       BTC/USD              8 118,38 $           0.12%      8 187 887 $         
3    Bitfinex                       BTC/JPY              8 123,59 $           0.11%      8 086 839 $         
4    Bitfinex                       BTC/EUR              8 116,79 $           0.14%      7 791 188 $         
5    Coinbase Pro                   BTC/USD              8 092,08 $           0.01%      597 987 $           
6    Bitfinex                       BTC/GBP              8 074,11 $           0.16%      7 565 745 $         
7    FTX (Spot)                     BTC/USD              8 093,00 $           0.01%      5 621 768 $         
8    BW.com                         BTC/USDT             8 117,56 $           0.02%      2 629 169 $         

...and so on.
Run Code Online (Sandbox Code Playgroud)