Javascript 刮痧+;Selenium 302重定向处理
因此,我正在建立一个webcrawler,它可以登录到我的银行账户,收集我的消费数据。我本来打算只使用Scrapy,但它不起作用,因为第一个优点页面使用Javascript登录,所以我把Selenium放在上面 我的代码通过一系列生成请求登录(首先需要输入用户名,然后输入密码,不像在大多数页面中那样结合使用),这些请求带有处理下一步的特定回调函数Javascript 刮痧+;Selenium 302重定向处理,javascript,python,html,selenium,selenium-webdriver,Javascript,Python,Html,Selenium,Selenium Webdriver,因此,我正在建立一个webcrawler,它可以登录到我的银行账户,收集我的消费数据。我本来打算只使用Scrapy,但它不起作用,因为第一个优点页面使用Javascript登录,所以我把Selenium放在上面 我的代码通过一系列生成请求登录(首先需要输入用户名,然后输入密码,不像在大多数页面中那样结合使用),这些请求带有处理下一步的特定回调函数 import scrapy from scrapy import Request from selenium import webdriver fro
import scrapy
from scrapy import Request
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import selenium
import time
class LoginSpider(scrapy.Spider):
name = 'www.firstmerit.com'
# allowed_domains = ['https://www.firstmeritib.com']
start_urls = ['https://www.firstmeritib.com/AccountHistory.aspx?a=1']
def __init__(self):
self.driver = webdriver.Firefox()
def parse(self, response):
self.driver.get(response.url)
# Obtaining necessary components to input my own stuff
username = WebDriverWait(self.driver, 10).until(lambda driver: self.driver.find_element_by_xpath('//*[@id="txtUsername"]'))
login_button = WebDriverWait(self.driver, 10).until(lambda driver: self.driver.find_element_by_xpath('//*[@id="btnLogin"]'))
# The actual interaction
username.send_keys("username")
login_button.click()
# The process of logging in is broken up in two functions since the website requires me
# to enter my username first which redirects me to a password page where I cna finally enter my account (after inputting password)
yield Request(url = self.driver.current_url,
callback = self.password_handling,
meta = {'dont_redirect' : True,
'handle_httpstatus_list': [302],
'cookiejar' : response}
)
def password_handling(self, response):
print("^^^^^^")
print(response.url)
password = WebDriverWait(self.driver, 10).until(lambda driver: self.driver.find_element_by_xpath('//*[@id="MainContent_txtPassword"]'))
login_button2 = WebDriverWait(self.driver, 10).until(lambda driver: self.driver.find_element_by_xpath('//*[@id="MainContent_btnLogin"]'))
password.send_keys("password")
login_button2.click()
print("*****")
print(self.driver.current_url)
print("*****")
yield Request (url = self.driver.current_url,
callback = self.after_login, #, dont_filter = True,
meta = {'dont_redirect' : True,
'handle_httpstatus_list': [302],
'cookiejar' : response.meta['cookiejar'] }
)
def after_login(self, response):
print"***"
print(response.url)
print"***"
print(response.body)
if "Account Activity" in response.body:
self.logger.error("Login failed")
return
else:
print("you got through!")
print()
问题是,一旦我最终进入显示我所有支出的帐户页面,我实际上就无法访问HTML数据。我已经正确地处理了302个重定向,但是“meta=”选项似乎可以通过selenium将我带到页面,但不要让我刮到页面
在after_login函数中,我没有从response.body获取所有数据,而是获得以下内容:
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="/Default.aspx?ReturnURL=%2fAccountHistory.aspx%3fa%3d1">here</a>.</h2>
</body></html>
对象已移动
对象移动到。
我如何才能真正地获取这些信息,从而将其删除?
银行是否实施了此重定向以保护帐户不被爬网?
谢谢大家!