在我之前的问题中,我对我的问题并不是非常具体(使用Scrapy进行经过身份验证的会话),希望能够从更一般的答案中推断出解决方案.我应该更喜欢使用这个词crawling.
所以,到目前为止我的代码是:
class MySpider(CrawlSpider):
name = 'myspider'
allowed_domains = ['domain.com']
start_urls = ['http://www.domain.com/login/']
rules = (
Rule(SgmlLinkExtractor(allow=r'-\w+.html$'), callback='parse_item', follow=True),
)
def parse(self, response):
hxs = HtmlXPathSelector(response)
if not "Hi Herman" in response.body:
return self.login(response)
else:
return self.parse_item(response)
def login(self, response):
return [FormRequest.from_response(response,
formdata={'name': 'herman', 'password': 'password'},
callback=self.parse)]
def parse_item(self, response):
i['url'] = response.url
# ... do more things
return i
Run Code Online (Sandbox Code Playgroud)
如您所见,我访问的第一页是登录页面.如果我还没有通过身份验证(在parse函数中),我会调用我的自定义login函数,该函数会发布到登录表单.然后,如果我我验证,我想继续爬行.
问题是parse我试图覆盖的功能以便登录,现在不再进行必要的调用来刮掉任何其他页面(我假设).而且我不确定如何保存我创建的项目.
以前有人做过这样的事吗?(验证,然后爬行,使用a CrawlSpider)任何帮助将不胜感激.
我想从网站上抓取数据,这需要登录才能到达某个页面,然后才能抓取数据。
使用 Scrapy 登录后有什么方法可以抓取数据吗?或者我们是否可以模拟登录?
注意:我确实有登录凭据。