[英]Login automation and crawling using Scrapy Python
我一直在试图写一个脚本来找回我接受的解决办法上SPOJ 查看更多
我被困在自动化登录过程中。 我发现Scrapy难以理解。 经过多次文档和代码示例之后,我对幕后发生的事情有一个模糊的印象,这就是我现在所处的位置:
(我已在所需位置注释了该代码)
import os
import os.path
import scrapy
import urllib.request
from scrapy.selector import HtmlXPathSelector
from scrapy.http import Request
from bs4 import BeautifulSoup
class LoginSpider(scrapy.Spider):
name = 'spoj'
start_urls = ['http://www.spoj.com/login']
outputFile = open('output.txt' , 'w')
def parse(self, response):
username = input('Enter username\n')
password = input('Enter password\n')
return scrapy.FormRequest.from_response(
response,
formdata={'username': username, 'password': password},
callback=self.after_login
)
def after_login(self, response):
# Even if I type in correct username and password it always leads to
# authentication faliure and the following if statement evaluates to true.
if str.encode('Authentication failed!') in response.body:
self.logger.error("Login failed")
print ('Incorrect credentials')
return
print('lol') # ofcourse this isn't printed
return scrapy.Request(url = "http://www.spoj.com/myaccount/" , callback = self.retrieve_codes )
# needless to say, the following function is never called
def retrieve_codes(self, response):
print('Hello testing!')
url = 'http://www.spoj.com/files/src/16731976/'
html = urllib.request.urlopen(url).read()
soup = BeautifulSoup(html , 'html.parser')
self.outputFile.write(str(soup.prettify()))
在文档中if "authentication failed" in response.body:
我改为
if str.encode('Authentication failed!') in response.body:
原因为
a byte like object is required not 'str'
这样a byte like object is required not 'str'
Authentication failed!
显示且未authentication failed
。 在这里我们需要精确。 请告诉我我在哪里做错了。 我没有在网上找到任何很好的资源来详细讨论表单验证问题。 从文档中看到此代码后,我最初的问题是,
我也曾尝试使用robobrowser,但徒劳。 我有点期待像美丽汤一样的优质文档。
谢谢!
您使用了错误的formdata
字段名称。 您需要将示例代码从草率文档调整为特定网站。 当前,您使用username
和password
作为formdata
字段。
如果您在登录时使用浏览器的开发人员工具,则可以看到POST
发送的字段标记为login_user
和password
。
因此,这应该很容易解决:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.