抓取基于登录的网站的最佳方式是什么?

asy*_*ait 2 c# python watin web-crawler html-parsing

我要从网站自动化文件下载活动(类似于,比方说,yahoomail.com).要访问具有此文件下载链接的页面,我要登录,从页面跳转到页面以提供日期等参数,最后单击下载链接.

我在考虑三种方法:

  1. 使用WatIN并开发一个Windows服务,定期执行一些WatiN代码遍历页面并下载文件.

  2. 使用AutoIT(没什么好主意)

  3. 使用简单的HTML解析技术(这里有几个问题,例如,如何在登录后维护会话?如何在执行后退出?

Tar*_*ski 5

我使用scrapy.org,它是一个python库.实际上这很安静.易于编写蜘蛛,它的功能非常广泛.包中提供登录后的刮痧网站.

以下是在身份验证后抓取网站的蜘蛛示例.

class LoginSpider(BaseSpider):
    domain_name = 'example.com'
    start_urls = ['http://www.example.com/users/login.php']

    def parse(self, response):
        return [FormRequest.from_response(response,
                formdata={'username': 'john', 'password': 'secret'},
                callback=self.after_login)]

    def after_login(self, response):
        # check login succeed before going on
        if "authentication failed" in response.body:
            self.log("Login failed", level=log.ERROR)
            return

        # continue scraping with authenticated session...
Run Code Online (Sandbox Code Playgroud)