Страницы

Поиск по вопросам

воскресенье, 7 июля 2019 г.

Выполнить сортировку html таблицы до получения html кода

Получаю данные по этой ссылке. Код на python. Также использую библиотеку BeautifulSoup для последующей обработки HTML кода.
Как мне, перед получением HTML кода страницы, выполнить сортировку таблицы на сайте, например по цене?
url = 'https://coinmarketcap.com/currencies/bitcoin/#markets' html = requests.get(url).text data = get_data(html)


Ответ

Можно воспользоваться модулем Pandas
import pandas as pd
url = 'https://coinmarketcap.com/currencies/bitcoin/#markets' df = pd.read_html(url)[0]
res = (df.assign(pr_num=lambda x: pd.to_numeric(x['Price'].str.replace(r'[\$,\s\*]',''), errors='coerce')) .sort_values('pr_num', ascending=False))
результат:
In [268]: print(res) # Source Pair Volume (24h) Price Volume (%) Updated pr_num 353 354 CoinExchange CUBE/BTC $1,568,170 $13064.30 0.03% Recently 13064.30 13 14 BTCC BTC/USD $58,216,900 $7630.00 1.25% Recently 7630.00 224 225 LakeBTC BTC/USD $3,144,690 $7144.04 0.07% Recently 7144.04 251 252 Bitinka BTC/ARS $2,599,470 $7087.53 0.06% Recently 7087.53 170 171 BCEX NAS/BTC $4,433,300 $7057.71 0.09% Recently 7057.71 .. ... ... ... ... ... ... ... ... 280 281 Bit-Z XAS/BTC $2,230,820 $6297.18 0.05% Recently 6297.18 279 280 BtcTrade.im ETC/BTC $2,270,590 $6287.73 0.05% Recently 6287.73 291 292 OKEx OF/BTC $2,112,840 $6278.22 0.05% Recently 6278.22 369 370 Hotbit BZNT/BTC $1,453,500 $6272.91 0.03% Recently 6272.91 193 194 BCEX ANS/BTC $3,664,540 $5950.85 0.08% Recently 5950.85
[400 rows x 8 columns]
если нужен HTML:
res.to_html(r'c:/temp/res.html')
результат для первых 10 строк:

# Source Pair Volume (24h) Price Volume (%) Updated pr_num
353 354 CoinExchange CUBE/BTC $1,568,170 $13064.30 0.03% Recently 13064.30
13 14 BTCC BTC/USD $58,216,900 $7630.00 1.25% Recently 7630.00
224 225 LakeBTC BTC/USD $3,144,690 $7144.04 0.07% Recently 7144.04
251 252 Bitinka BTC/ARS $2,599,470 $7087.53 0.06% Recently 7087.53
170 171 BCEX NAS/BTC $4,433,300 $7057.71 0.09% Recently 7057.71
223 224 BtcTrade.im LTC/BTC $3,146,020 $7044.13 0.07% Recently 7044.13
146 147 Bit-Z GXS/BTC $5,608,050 $6958.11 0.12% Recently 6958.11
385 386 LBank PNT/BTC $1,385,290 $6904.87 0.03% Recently 6904.87
264 265 Negocie Coins BTC/BRL $2,487,080 $6898.36 0.05% Recently 6898.36
387 388 Trade By Trade DASH/BTC $1,378,990 $6894.70 0.03% Recently 6894.70

PS чтобы удалить созданный столбец pr_num
res = res.drop('pr_num', axis=1)
In [272]: res Out[272]: # Source Pair Volume (24h) Price Volume (%) Updated 353 354 CoinExchange CUBE/BTC $1,568,170 $13064.30 0.03% Recently 13 14 BTCC BTC/USD $58,216,900 $7630.00 1.25% Recently 224 225 LakeBTC BTC/USD $3,144,690 $7144.04 0.07% Recently 251 252 Bitinka BTC/ARS $2,599,470 $7087.53 0.06% Recently 170 171 BCEX NAS/BTC $4,433,300 $7057.71 0.09% Recently .. ... ... ... ... ... ... ... 280 281 Bit-Z XAS/BTC $2,230,820 $6297.18 0.05% Recently 279 280 BtcTrade.im ETC/BTC $2,270,590 $6287.73 0.05% Recently 291 292 OKEx OF/BTC $2,112,840 $6278.22 0.05% Recently 369 370 Hotbit BZNT/BTC $1,453,500 $6272.91 0.03% Recently 193 194 BCEX ANS/BTC $3,664,540 $5950.85 0.08% Recently
[400 rows x 7 columns]

Комментариев нет:

Отправить комментарий