【问题标题】:scrapy response is nothing like the page sourcescrapy 响应与页面源完全不同
【发布时间】:2020-08-06 19:23:32
【问题描述】:

我正在尝试使用 scrapy shell 进入“ykc1.greatwestlife.com”,这应该是一个公共网站,虽然如果我手动查看页面源代码有很多东西,我无法使用 scrapy 获得正确的响应.

scrapy shell response result

在这种情况下我需要使用 scrapy-splash 吗? 有任何想法吗?谢谢

【问题讨论】:

标签: python web-scraping scrapy scrapy-splash


【解决方案1】:

实际上可以看到两个背靠背的请求,由

引起
      <head>
        <script language="javascript">
            document.cookie = "cmsUserPortalLocale=en;path=/";
            document.cookie = "cmsTheme=advgwl;path=/";    
            document.cookie = "siteBrand="+escape(location.hostname)+"; path=/";
            window.location.reload(true);
        </script>

第一个请求要小得多,并且可能会导致您遇到的问题。值得庆幸的是,由于 cookie 看起来是静态的,您可以很容易地重现该行为:

def parse(self, response):
    # this is required because the response that arrives to parse()
    # has session cookies but we need to add 3 more to them
    new_cookies = {
      "cmsUserPortalLocale": "en",
      "cmsTheme": "advgwl",
      "siteBrand": "ykc1.greatwestlife.com",
    }
    yield response.follow(url=request.url, cookies=new_cookies,
                          callback=self.parse_home)

def parse_home(self, response):
    # and now you have the full body

【讨论】:

  • 这很棒,现在可以使用了,非常感谢,mdaniel
  • 我很高兴;如果您可以将答案标记为已接受,它将让其他人知道该解决方案对您有效
猜你喜欢
  • 2016-02-07
  • 2016-05-24
  • 1970-01-01
  • 2022-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-31
  • 1970-01-01
相关资源
最近更新 更多