2014-09-02 63 views
-1

嘿家伙,所以我刚刚进入请求模块搞乱它,试图找到一个特定的响应文本似乎无法做到这一点?TypeError:str对象不可调用请求模块

我试着做ifr.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") 
+1

请张贴整个追踪! – flakes 2014-09-02 19:45:29

+0

'r.text'不会返回文本内容吗?你期待那条路线要做什么? – FatalError 2014-09-02 19:46:52

+0

完成对不起x.x xD – Shrekt 2014-09-02 19:47:05

回答

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 
+0

感谢您的帮助兄弟:) – Shrekt 2014-09-02 19:51:50

0

它看起来像你的问题是与线r.text

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

你想要编写一行:

if "You have" in r.text:

0

你最有可能内置的思维string.find()功能

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

Return the lowest index in s where the substring sub is found such that sub is wholly contained in s[start:end]. Return -1 on failure. Defaults for start and end and interpretation of negative values is the same as for slices.

在这种情况下,你会改变

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") 

,或者你可以简单地把它写在一个更Python /可读的形式

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