gpt4 book ai didi

javascript - 使用 Javascript Scrapy 登录重定向站点

转载 作者:行者123 更新时间:2023-11-28 04:05:26 26 4
gpt4 key购买 nike

我正在尝试登录以下站点:http://go.galegroup.com.proxy-um.researchport.umd.edu/ps/eToc.do?docId=0PQC&userGroupName=umd_um&action=DO_BROWSE_ETOC&inPS=true&prodId=GVRL&etocId=GALE%7CCX2830999001&isDownLoadOptionDisabled=true 。这最初会将我发送到一个重定向页面,该页面似乎使用 Javascript 进行重定向。我无法让 Scrapy 遵循此处的重定向,因此我现在使用splash-scrapy 重定向到登录页面。目前我可以访问登录页面,但是当我尝试使用 scrapy.FormRequest.from_response 时,我得到的响应不是正确的页面。

我的Scrapy程序:

# -*- coding: utf-8 -*-
import scrapy
from scrapy_splash import SplashRequest

class DbioSpider(scrapy.Spider):
name = 'dbio'
start_urls = ['http://go.galegroup.com.proxy-um.researchport.umd.edu/ps/eToc.do?docId=0PQC&userGroupName=umd_um&action=DO_BROWSE_ETOC&inPS=true&prodId=GVRL&etocId=GALE%7CCX2830999001&isDownLoadOptionDisabled=true']

def start_requests(self):
for url in self.start_urls:
yield SplashRequest(url, self.parse, endpoint='render.html', args={'wait': 0.5})

def parse(self, response):
with open('login.html', 'w') as f:
f.write(response.body)
return scrapy.FormRequest.from_response(response, formdata={'username': '???', 'password': '???'},
callback=self.after_login)

def after_login(self, response):
with open('after_login.html', 'w') as f:
f.write(response.body)

Scrapy 的输出:

2017-10-14 09:13:32 [scrapy.utils.log] INFO: Scrapy 1.4.0 started (bot: bio)
2017-10-14 09:13:32 [scrapy.utils.log] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'bio.spiders', 'DUPEFILTER_CLASS': 'scrapy_splash.SplashAwareDupeFilter', 'SPIDER_MODULES': ['bio.spiders'], 'BOT_NAME': 'bio', 'EDITOR': 'emacs', 'HTTPCACHE_STORAGE': 'scrapy_splash.SplashAwareFSCacheStorage'}
2017-10-14 09:13:32 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.memusage.MemoryUsage',
'scrapy.extensions.logstats.LogStats',
'scrapy.extensions.telnet.TelnetConsole',
'scrapy.extensions.corestats.CoreStats']
2017-10-14 09:13:32 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
'scrapy.downloadermiddlewares.retry.RetryMiddleware',
'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
'scrapy_splash.SplashCookiesMiddleware',
'scrapy_splash.SplashMiddleware',
'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
'scrapy.downloadermiddlewares.stats.DownloaderStats']
2017-10-14 09:13:32 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
'scrapy_splash.SplashDeduplicateArgsMiddleware',
'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
'scrapy.spidermiddlewares.referer.RefererMiddleware',
'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
'scrapy.spidermiddlewares.depth.DepthMiddleware']
2017-10-14 09:13:32 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2017-10-14 09:13:32 [scrapy.core.engine] INFO: Spider opened
2017-10-14 09:13:32 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2017-10-14 09:13:32 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
2017-10-14 09:13:33 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://go.galegroup.com.proxy-um.researchport.umd.edu/ps/eToc.do?docId=0PQC&userGroupName=umd_um&action=DO_BROWSE_ETOC&inPS=true&prodId=GVRL&etocId=GALE%7CCX2830999001&isDownLoadOptionDisabled=true via http://192.168.1.7:8050/render.html> (referer: None)
2017-10-14 09:13:33 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.searchum.umd.edu/search?site=UMCP&client=UMCP&proxystylesheet=UMCP&output=xml_no_dtd&as_oq=site%3A&q=Search+UMD.edu&search+button=Search&username=???&password=???> (referer: http://go.galegroup.com.proxy-um.researchport.umd.edu/ps/eToc.do?docId=0PQC&userGroupName=umd_um&action=DO_BROWSE_ETOC&inPS=true&prodId=GVRL&etocId=GALE%7CCX2830999001&isDownLoadOptionDisabled=true)
2017-10-14 09:13:33 [scrapy.core.engine] INFO: Closing spider (finished)
2017-10-14 09:13:33 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 1268,
'downloader/request_count': 2,
'downloader/request_method_count/GET': 1,
'downloader/request_method_count/POST': 1,
'downloader/response_bytes': 18230,
'downloader/response_count': 2,
'downloader/response_status_count/200': 2,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2017, 10, 14, 14, 13, 33, 929932),
'log_count/DEBUG': 3,
'log_count/INFO': 7,
'memusage/max': 50655232,
'memusage/startup': 50655232,
'request_depth_max': 1,
'response_received_count': 2,
'scheduler/dequeued': 3,
'scheduler/dequeued/memory': 3,
'scheduler/enqueued': 3,
'scheduler/enqueued/memory': 3,
'splash/render.html/request_count': 1,
'splash/render.html/response_count/200': 1,
'start_time': datetime.datetime(2017, 10, 14, 14, 13, 32, 187173)}
2017-10-14 09:13:33 [scrapy.core.engine] INFO: Spider closed (finished)

如果有帮助的话,我可以提供返回的页面。

最佳答案

尝试对您的链接进行初始请求: https://login.proxy-um.researchport.umd.edu/login?url=http://go.galegroup.com/ps/eToc.do?docId=0PQC&userGroupName=umd_um&action=DO_BROWSE_ETOC&inPS=true&prodId=GVRL&etocId=GALE|CX2830999001&isDownLoadOptionDisabled=true并使用 scrapy.FormRequest.from_response() 而不使用任何表单数据。之后您将重定向到https://shib.idm.umd.edu/shibboleth-idp/profile/SAML2/POST/SSO?execution=e2s1

像这样的事情:

def start_requests(self):
yield scrapy.Request(self.start_urls[0])

def parse(self, response):
with open('prepare.html', 'w') as f:
f.write(response.body)
return scrapy.FormRequest.from_response(response, callback=self.prepare_login)

def prepare_login(self, response):
with open('login.html', 'w') as f:
f.write(response.body)
return scrapy.FormRequest.from_response(response, formdata={'username': '???', 'password': '???'},
callback=self.after_login)

def after_login(self, response):
with open('after_login.html', 'w') as f:
f.write(response.body)

关于javascript - 使用 Javascript Scrapy 登录重定向站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46745566/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com