首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用MechanicalSoup的API提交表单失败- NoneType

使用MechanicalSoup的API提交表单失败- NoneType
EN

Stack Overflow用户
提问于 2018-04-20 04:36:22
回答 1查看 200关注 0票数 0

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

以下是API的代码:

代码语言:javascript
复制
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

下面是我用来实现它的代码:

代码语言:javascript
复制
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) 

以下是错误消息:

代码语言:javascript
复制
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

我已经把头撞在墙上好几个小时了,我觉得你们中的一个人需要两秒钟才能弄明白。仅供参考,这是一个API,用于在Investopedia的股票交易模拟器上进行交易。

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2018-04-20 04:56:48

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

查看堆栈跟踪:

代码语言:javascript
复制
  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 does not seem需要接受第二个参数。但the source确实有更多未记录的参数与默认的But,特别是_candidate_generator,您可以猛烈抨击它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49929821

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档