通过凭据登录后,Scrapy不会在Linkedin上爬网数据(Linkedin组名)

时间:2018-08-02 19:27:18

标签: python web-scraping scrapy scrapy-spider

我正在尝试从添加了我的linkedin中的一个群组中抓取成员列表。

尽管我运行代码时,没有得到响应/值。和大量的错误。

我已经验证了我的解析代码,看起来不错。

我正在尝试从添加了我的linkedin中的一个群组中抓取成员列表。

尽管我运行代码时,没有得到响应/值。和大量的错误。

我已经验证了我的解析代码,看起来不错。

这是我的代码:

import scrapy
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from scrapy.http import Request
from scrapy.http import FormRequest

class LoginSpider(BaseSpider):
    name = 'jiju'
    start_urls = ['https://www.linkedin.com/groups/58888/members']

    def parse(self, response):
        return [FormRequest.from_response(response,
                    formdata={'username': 'myusername', 'password': 'mypassword'},
                    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
        # We've successfully authenticated, let's have some fun!
        else:
            return Request(url="http://www.example.com/tastypage/",
                   callback=self.parse_tastypage)

    def parse_tastypage(self, response):
        for j in response.xpath('//*[@id="ember2299"]'):
            yield {
              'detail':j.xpath('//*[@id="ember2299"]/span').extract(),
        }

这是我得到的答复。

C:\Users\Yash\tutorial\tutorial\spiders\jij.py:1: ScrapyDeprecationWarning: Module `scrapy.contrib.spiders` is deprecated, use `scrapy.spiders` instead
  from scrapy.contrib.spiders.init import InitSpider
C:\Users\Yash\tutorial\tutorial\spiders\jij.py:1: ScrapyDeprecationWarning: Module `scrapy.contrib.spiders.init` is deprecated, use `scrapy.spiders.init` instead
  from scrapy.contrib.spiders.init import InitSpider
C:\Users\Yash\tutorial\tutorial\spiders\jij.py:6: ScrapyDeprecationWarning: Module `scrapy.spider` is deprecated, use `scrapy.spiders` instead
  from scrapy.spider import BaseSpider
C:\Users\Yash\tutorial\tutorial\spiders\jiju.py:7: ScrapyDeprecationWarning: tutorial.spiders.jiju.LoginSpider inherits from deprecated class scrapy.spiders.BaseSpider, please inherit from scrapy.spiders.Spider. (warning only on first subclass, there may be others)
  class LoginSpider(BaseSpider):
2018-08-03 00:51:07 [scrapy.utils.log] INFO: Scrapy 1.4.0 started (bot: tutorial)
2018-08-03 00:51:07 [scrapy.utils.log] INFO: Overridden settings: {'BOT_NAME': 'tutorial', 'NEWSPIDER_MODULE': 'tutorial.spiders', 'SPIDER_MODULES': ['tutorial.spiders']}
2018-08-03 00:51:07 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
 'scrapy.extensions.telnet.TelnetConsole',
 'scrapy.extensions.logstats.LogStats']
2018-08-03 00:51:07 [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.httpcompression.HttpCompressionMiddleware',
 'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
 'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
 'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
 'scrapy.downloadermiddlewares.stats.DownloaderStats']
2018-08-03 00:51:07 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
 'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
 'scrapy.spidermiddlewares.referer.RefererMiddleware',
 'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
 'scrapy.spidermiddlewares.depth.DepthMiddleware']
2018-08-03 00:51:07 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2018-08-03 00:51:07 [scrapy.core.engine] INFO: Spider opened
2018-08-03 00:51:07 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2018-08-03 00:51:07 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
2018-08-03 00:51:07 [scrapy.downloadermiddlewares.redirect] DEBUG: Redirecting (302) to <GET https://www.linkedin.com/uas/login?session_redirect=https%3A%2F%2Fwww.linkedin.com%2Fgroups%2F58888%2Fmembers> from <GET https://www.linkedin.com/groups/58888/members>
2018-08-03 00:51:08 [scrapy.downloadermiddlewares.redirect] DEBUG: Redirecting (302) to <GET https://www.linkedin.com/start/join?session_redirect=https%3A%2F%2Fwww.linkedin.com%2Fgroups%2F58888%2Fmembers&trk=login_reg_redirect> from <GET https://www.linkedin.com/uas/login?session_redirect=https%3A%2F%2Fwww.linkedin.com%2Fgroups%2F58888%2Fmembers>
2018-08-03 00:51:08 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.linkedin.com/start/join?session_redirect=https%3A%2F%2Fwww.linkedin.com%2Fgroups%2F58888%2Fmembers&trk=login_reg_redirect> (referer: None)
2018-08-03 00:51:08 [scrapy.core.engine] DEBUG: Crawled (422) <POST https://www.linkedin.com/start/reg/api/createAccount> (referer: https://www.linkedin.com/start/join?session_redirect=https%3A%2F%2Fwww.linkedin.com%2Fgroups%2F58888%2Fmembers&trk=login_reg_redirect)
2018-08-03 00:51:08 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <422 https://www.linkedin.com/start/reg/api/createAccount>: HTTP status code is not handled or not allowed
2018-08-03 00:51:08 [scrapy.core.engine] INFO: Closing spider (finished)
2018-08-03 00:51:08 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 2810,
 'downloader/request_count': 4,
 'downloader/request_method_count/GET': 3,
 'downloader/request_method_count/POST': 1,
 'downloader/response_bytes': 20952,
 'downloader/response_count': 4,
 'downloader/response_status_count/200': 1,
 'downloader/response_status_count/302': 2,
 'downloader/response_status_count/422': 1,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2018, 8, 2, 19, 21, 8, 574170),
 'httperror/response_ignored_count': 1,
 'httperror/response_ignored_status_count/422': 1,
 'log_count/DEBUG': 5,
 'log_count/INFO': 8,
 'request_depth_max': 1,
 'response_received_count': 2,
 'scheduler/dequeued': 4,
 'scheduler/dequeued/memory': 4,
 'scheduler/enqueued': 4,
 'scheduler/enqueued/memory': 4,
 'start_time': datetime.datetime(2018, 8, 2, 19, 21, 7, 742810)}
2018-08-03 00:51:08 [scrapy.core.engine] INFO: Spider closed (finished)

1 个答案:

答案 0 :(得分:2)

问题:

发生了什么事,Scrapy试图访问start_urlshttps://www.linkedin.com/groups/58888/members

发出此请求且您尚未登录时,LinkedIn重定向了您 到https://www.linkedin.com/start/join,这是一个页面,用于创建新用户。

您的parse函数试图在此页面上找到一个表单,并使用您的凭据设置输入字段usernamepassword。 由于注册表单包含password字段,因此Scrapy尝试将包含您的数据的表单发布到https://www.linkedin.com/start/reg/api/createAccount,这失败了,这就是LinkedIn返回422错误的原因。

解决方案:

在向LinkedIn发出任何请求之前,您需要确保已登录。 为此,您的start_urls应该包含登录页面。 由于LinkedIn上的登录表单不使用usernamepassword字段,因此必须进行更改。您可以转到登录页面,检查这些字段并找到其名称。它们分别是:session_keysession_password。之后,您可以请求要解析的页面:

class LoginSpider(BaseSpider):
    name = 'jiju'
    start_urls = ['https://www.linkedin.com/uas/login']

    def parse(self, response):
        return FormRequest.from_response(response,
                    formdata={'session_key': 'your_login', 'session_password': 'your_pass'},
                    callback=self.after_login)

    def after_login(self, response):
        return Request(url="https://www.linkedin.com/groups/58888/members", callback=self.parse_members)

但是,会有更多问题。首先,LinkedIn使用JS代码和AJAX请求来加载页面内容。因此,以这种方式收到的回复可能不会包含您需要的数据。

如果您想执行JS代码并收到在浏览器中看到的响应,则可以使用Splash。无论如何,这样做之后,您将不得不与reCAPTCHA作斗争。您应该查看服务条款,看看它们是否允许爬行器或机器人。