2014-09-02 64 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") 
相關問題