【问题标题】:TypeError: 'unicode' object is not callableTypeError:“unicode”对象不可调用
【发布时间】:2014-11-05 14:50:43
【问题描述】:

我想获取源,但我有ERROR

>> from selenium import webdriver
>> driver = webdriver.PhantomJS()
>> url='http://google.com'
>> cont=driver.page_source(url)
>> print cont
>> driver.quit()

错误:

Traceback (most recent call last):
  File "u.py", line 6, in <module>
    cont=driver.page_source(url)
TypeError: 'unicode' object is not callable

【问题讨论】:

    标签: python selenium unicode phantomjs


    【解决方案1】:

    当我们将 unicode 对象作为函数调用时会发生此错误。例如:

    a = u'this is unicode string'
    

    如果您在代码中的某处执行a(),则会收到此错误

    在你的情况下,page_source 是一个 unicode 字符串,而不是一个可调用函数。所以这个page_source(url) 给出了上述错误。

    【讨论】:

      【解决方案2】:

      page_source 不是您使用它的方法。您想在 url 上使用 get 方法,然后驱动程序将包含您要查找的源代码。

      >> from selenium import webdriver
      >> driver = webdriver.PhantomJS()
      >> url='http://google.com'
      >> driver.get(url)
      >> print driver.page_source
      

      【讨论】:

      • 我用print cont.page_source的错误:`AttributeError: 'NoneType' object h
      • 很抱歉仍在尝试找出 stackoverflow 格式
      • 谢谢...您可以编辑您的答案并将您的代码放入 nswer 部分
      • No.. 我仍然有错误:AttributeError: 'NoneType' object has no attribute 'page_source'
      猜你喜欢
      • 2014-12-08
      • 2017-09-13
      • 2012-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-25
      • 2021-04-15
      • 2011-10-01
      相关资源
      最近更新 更多