【问题标题】:How to scrape dataframe after select options from dropdown list?从下拉列表中选择选项后如何抓取数据框?
【发布时间】:2020-04-26 12:10:46
【问题描述】:

我想用 BeautifulSoup 从下拉列表值中抓取数据框。

  1. 我在两个下拉菜单中都选择了值
  2. 我提交我的选择
  3. 我得到一个数据表

我想用 BS 捕获这个数据帧。 对实现这一目标的过程有任何想法吗?

示例网站:https://coinarbitragebot.com/arbitrage.php

谢谢

【问题讨论】:

  • "How do I xxx" 与堆栈溢出无关。请提供minimal reproducible example,包括您已经尝试过的代码,以及您的结果与预期结果有何不同。在这种情况下,看起来这是一个动态构建的页面,这需要像 selenium 这样的浏览器自动化
  • 仅供参考,它是 scrape(和 scrapingscrapedscraper)而不是 scrap

标签: python css pandas web-scraping beautifulsoup


【解决方案1】:

您可以使用自定义参数发出简单的 POST 请求(单击提交按钮时您将在 Firefox/Chrome 网络选项卡中看到的参数)。然后你可以使用pandas.read_html() 函数来获取你的DataFrame。

例如:

data = {'bibox':   1, 'biki':   1, 'binance':1, 'bit-z':  1, 'bitbns': 1, 'bitfinex':   1, 'bitforex':   1, 'bithumb':1,
'bitkub': 1, 'bitmart':1, 'bitmax': 1, 'bitrue': 1, 'bitso':  1, 'bitstamp':  1, 'bittrex':1, 'bleutrade':  1, 'btcturk':1,
'bw_com':1, 'catex':  1, 'cex_io': 1, 'coinall':1, 'coinbase':   1, 'coinbene':  1, 'coincheck':  1, 'coindeal':   1, 'coineal':1,
'coinsbit':   1, 'cointiger':  1, 'crex24': 1, 'dcoin':  1, 'digifinex':  1, 'exmo':   1, 'exx_com':1, 'fatbtc': 1, 'finexbox':   1,
'gate_io':1, 'graviex':1, 'hitbtc': 1, 'hotbit':1, 'huobi':  1, 'indodax':1, 'koineks':1, 'kraken': 1, 'kucoin': 1, 'latoken':1,
'lbank':  1, 'liquid': 1, 'livecoin':   1, 'mercatox':   1, 'mxc':1, 'okcoin': 1, 'okex':   1, 'p2pb2b': 1, 'poloniex':   1, 'simex':  1,
'sistemkoin': 1, 'stex':   1, 'tokok':  1, 'tradeogre':  1, 'tradesatoshi':   1, 'upbit':  1, 'yobit':  1, 'zb_com': 1, 'zbg':1,
'bcurr': 'usd', 'arb_margin':  25, 'sbmfrm':  1}

import requests
import pandas as pd
from bs4 import BeautifulSoup

url = 'https://coinarbitragebot.com/arbitrage.php'

data['bcurr'] = 'usd'       # <-- set to 'usd', 'btc' or 'all'
data['arb_margin'] = 5      # <-- set to your value

soup = BeautifulSoup( requests.post(url, data=data).text, 'html.parser' )

df = pd.read_html(str(soup.select_one('#tbl1')))[0]
df.columns = df.loc[0]
df = df.iloc[1:].set_index('Coin', drop=True)

print(df)

打印:

0            bibox      biki  binance     bit-z  bitbns  bitfinex    bitforex    bithumb      bitkub  bitmart  ... sistemkoin stex  tokok tradeogre tradesatoshi upbit yobit     zb.com        zbg      %
Coin                                                                                                           ...                                                                                       
DOGE/USD  0.002102  0.002102   0.0021  0.002097       0         0  0.00209838          0  0.00205862        0  ...   0.002178    0      0         0            0     0     0  0.0021027  0.0021013  29.34
TRX/USD          0  0.014055  0.01408   0.01409       0  0.013905      0.0141  0.0137128           0  0.01408  ...   0.014512    0  0.014         0            0     0     0    0.01406     0.0145   7.63
XLM/USD          0         0  0.04733     0.047  0.0472   0.04724      0.0472  0.0460763   0.0471012  0.04733  ...   0.047811    0      0         0            0     0     0     0.0473     0.0475   5.08
BSV/USD          0   113.299        0         0       0    113.27     113.457    110.545     108.698  113.638  ...     113.69    0      0         0            0     0     0    112.172     113.48   5.89
NEO/USD      9.484      9.45    9.483    9.4823   9.386    9.4783        9.49          0           0    9.483  ...       9.91    0  9.483         0            0     0     0     9.4925          0   5.51
...            ...       ...      ...       ...     ...       ...         ...        ...         ...      ...  ...        ...  ...    ...       ...          ...   ...   ...        ...        ...    ...
PCX/USD          0         0        0         0       0         0           0          0           0        0  ...          0    0      0         0            0     0     0          0          0   6.86
QCX/USD          0         0        0         0       0         0           0          0           0        0  ...          0    0      0         0            0     0     0          0          0  24.54
XDCE/USD         0         0        0         0       0         0           0          0           0        0  ...          0    0      0         0            0     0     0          0          0   6.84
YAS/USD          0         0        0         0       0         0           0          0           0        0  ...          0    0      0         0            0     0     0          0          0  14.72
ZEL/USD          0         0        0         0       0         0           0          0           0        0  ...          0    0      0         0            0     0     0          0          0   9.93

[73 rows x 65 columns]

编辑:

要仅选择binancebitfinexbittrex,您可以像这样设置data

data = {'binance':1, 'bitfinex':   1, 'bittrex':1, 'bcurr': 'all', 'arb_margin': 5, 'sbmfrm':  1}

这将打印:

0          binance bitfinex     bittrex     %
Coin                                         
SC/BTC  0.00000018        0  0.00000017  5.56

如果没有找到套利机会,则没有找到表(您可能需要处理这种情况)。

【讨论】:

  • 感谢您的快速回复。是否可以在处理之前过滤感兴趣的平台。我尝试使用 df.filter 函数([“binance”、“bitfinex”、“bittrex”])。它运作良好,但我不感兴趣,因为它是后期处理
  • @Laurent 你只想要 binance、bitfinex 和 bittrex 列吗?如果是,您可以使用df[["binance", "bitfinex", "bittrex"]]。您可以像使用任何标准 pandas DataFrame 一样使用 df
  • 我想在处理之前做出选择,因为“%”列取决于它
  • @Laurent 查看我的编辑。您需要将所有参数放入data 字典中,然后发出请求。
猜你喜欢
  • 2022-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-02
  • 2017-01-11
  • 2017-05-19
相关资源
最近更新 更多