0

我正在使用一个 python API,它使用 MechanicalSoup 来完成它的大部分功能,突然之间,它不再工作了。我觉得它使用的网站可能已经改变或其他什么。

这是 API 的代码:

def trade(self, symbol, orderType, quantity, priceType="Market", price=False, duration=Duration.good_cancel):
        """
        Executes trades on the platform. See the readme.md file
        for examples on use and inputs. Returns True if the
        trade was successful. Else an exception will be
        raised.

        client.trade("GOOG", Action.buy, 10)
        client.trade("GOOG", Action.buy, 10, "Limit", 500)
        """

        br = self.br
        trade_page = self.fetch('/simulator/trade/tradestock.aspx')
        trade_form = trade_page.soup.select("form#orderForm")[0]

        # input symbol, quantity, etc.
        trade_form.select("input#symbolTextbox")[0]["value"] = symbol
        trade_form.select("input#quantityTextbox")[0]["value"] = str(quantity)

        # input transaction type
        [option.attrs.pop("selected", "") for option in trade_form.select("select#transactionTypeDropDown")[0]("option")]
        trade_form.select("select#transactionTypeDropDown")[0].find("option", {"value": str(orderType.value)})["selected"] = True

        # input price type
        [radio.attrs.pop("checked", "") for radio in trade_form("input", {"name": "Price"})]
        trade_form.find("input", {"name": "Price", "value": priceType})["checked"] = True

        # input duration type
        [option.attrs.pop("selected", "") for option in trade_form.select("select#durationTypeDropDown")[0]("option")]
        trade_form.select("select#durationTypeDropDown")[0].find("option", {"value": str(duration.value)})["selected"] = True

        # if a limit or stop order is made, we have to specify the price
        if price and priceType == "Limit":
            trade_form.select("input#limitPriceTextBox")[0]["value"] = str(price)

        elif price and priceType == "Stop":
            trade_form.select("input#stopPriceTextBox")[0]["value"] = str(price)

        prev_page = br.submit(trade_form, trade_page.url)
        prev_form = prev_page.soup.select("form", {"name": "simTradePreview"})
        br.submit(prev_form, prev_page.url)

        return True

这是我实现它的代码:

def buy(shares, ticker, client):
    client.trade(ticker,ita.Action.buy, shares)
...
if apred[0] -  arl[-1] > 0 and apred[1] - apred[0] > 0 and tickers[0] not in z:
        buy(ashr ,tickers[0], client) 

这是错误消息:

Traceback (most recent call last):
  File "/Users/carson/mlTechnicalAnalysis/investopedia.py", line 146, in <module>
    schedule.run_pending()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/schedule/__init__.py", line 493, in run_pending
    default_scheduler.run_pending()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/schedule/__init__.py", line 78, in run_pending
    self._run_job(job)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/schedule/__init__.py", line 131, in _run_job
    ret = job.run()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/schedule/__init__.py", line 411, in run
    ret = self.job_func()
  File "/Users/carson/mlTechnicalAnalysis/investopedia.py", line 56, in main
    buy(ashr ,tickers[0], client)
  File "/Users/carson/mlTechnicalAnalysis/investopedia.py", line 16, in buy
    client.trade(ticker,ita.Action.buy, shares)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/InvestopediaApi/ita.py", line 240, in trade
    prev_form = prev_page.soup.select("form", {"name": "simTradePreview"})
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/bs4/element.py", line 1532, in select
    for candidate in _use_candidate_generator(tag):
TypeError: 'dict' object is not callable

我已经把头撞在墙上几个小时了,我觉得你们都需要两秒钟才能弄清楚。仅供参考,它是在 Investopedia 的股票交易模拟器上进行交易的 API。

谢谢!

4

1 回答 1

1

.select()简而言之:您向;传递了太多参数。您必须将单个 CSS 选择器作为字符串传递。

查看堆栈跟踪:

  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/InvestopediaApi/ita.py", line 240, in trade
    prev_form = prev_page.soup.select("form", {"name": "simTradePreview"})
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/bs4/element.py", line 1532, in select
    for candidate in _use_candidate_generator(tag):
TypeError: 'dict' object is not callable

您作为第二个参数传递的似乎.select()是 dict ( {"name": "simTradePreview"})。显然这不是预期的。

从堆栈跟踪看来,soup问题是 BeautifulSoup 4 ( bs4); 它select 似乎不接受第二个论点。但是确实有更多带有默认值的未记录参数,特别是,_candidate_generator你破坏了它。

于 2018-04-19T20:56:48.443 回答