TypeError:str对象不是可调用的请求模块

时间:2014-09-02 19:44:35

标签: python

嘿伙计们,所以我刚进入请求模块搞乱它,我试图找到一个特定的响应文本似乎无法做到这一点?

我尝试使用if执行r.text似乎不起作用!\

错误:

C:\Python34\python.exe "C:/Users/Shrekt/PycharmProjects/Python 3/untitleds/gg.py"
Traceback (most recent call last):
File "C:/Users/Shrekt/PycharmProjects/Python 3/untitleds/gg.py", line 12, in <module>
if r.text("You have") !=-1:
TypeError: 'str' object is not callable

import requests

with requests.session() as s:
login_data = dict(uu='Wowsxx', pp='blahpassword', sitem='LIMITEDQTY')

#cookie = s.cookies['']

s.post('http://lqs.aq.com/login-ajax.asp', data=login_data, headers={"Host": "lqs.aq.com", "User-Agent": "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0", "Referer": "http://lqs.aq.com/default.asp", "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"})

r = s.get('http://lqs.aq.com/limited.asp')

if r.text("You have") !=-1:
    print("found")

3 个答案:

答案 0 :(得分:0)

if r.text("You have") !=-1:

不是检查r.text(字符串)是否包含或等于某个字符串的正确方法。

你需要做

if "You have" in r.text:  # Check for substring

if r.text == "You have":  # Check for equality

答案 1 :(得分:0)

您的问题似乎与行r.text有关。

如果您查看documentation的简介,您会看到r.text是一个字符串。

你想写这样的一行:

if "You have" in r.text:

答案 2 :(得分:0)

您很可能会考虑内置string.find()函数

string.find(s, sub[, start[, end]])
  

返回s中找到子字符串sub的最低索引   该sub完全包含在s [start:end]中。失败时返回-1。   开始和结束的默认值以及负值的解释是   与切片相同。

在这种情况下,您可以更改

if r.text("You have") !=-1: // note that text is a string not a function
    print("found")

为:

if r.text.find("You have") !=-1: // note that text.find is a function not a string! :)
    print("found")

或者你可以简单地用更Pythonic /可读的形式写它

if "You have" in r.text:
    print("found")
相关问题