我正在尝试使用 Scrapy 文档中给出的示例代码使用 python 脚本登录网站。这是修改后的代码:
from scrapy.spider import BaseSpider
from scrapy.http import Request,FormRequest
class LoginSpider(BaseSpider):
name = 'loginspidername'
start_urls = ['http://example.com/index.php']
login_page='http://example.com/index.php'
def parse(self, response):
return [FormRequest.from_response(response,
formdata={'username': 'myname', 'Password': 'mypass'},
formxpath='//*[@id="loginblock"]/div/div/form',
callback=self.after_login)]
def after_login(self, response):
# check login succeed before going on
if "logout" in response.body:
print 'logged in'
else:
print 'not logged in'
return
此外,如果需要,这里是 HTML 部分:
<div class="canvas" name="slideblock" id="loginblock" style="display: none;">
<div class="wrapper">
<div class="loginFloater">
<div align="right"><a href="javascript:hideDiv('loginblock');">close(X)</a></div>
<h2>Login</h2>
<form method="post" action="login.php">
<label>Username</label><input class="data" name="username" value="" placeholder="username" type="text"><br>
<label>Password</label><input class="data" name="password" value="" placeholder="password" type="password"><br>
<label></label><strong style="display:none ">Wrong Password Try again</strong>
<label> </label><input type="submit" class="searchButton" value="Login"></form>
<input type="checkbox" name="rememberme" value="true"> Remember Me<br><br>
<label> </label><a href="#">Forgot</a> Password?
Not a member? <a href="javascript:slideDiv('signupblock');">Signup</a>
<br>
</div>
</div>
</div>
输出始终是“未登录” 就像看起来很简单的代码一样,它根本没有登录。我无法想到代码中可能存在的任何问题。任何形式的帮助表示赞赏。谢谢阅读。