我尝试使用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")
答案 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)
答案 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")