python - scrapy 响应与页面源完全不同
问题描述
我正在尝试使用scrapy shell进入应该是一个公共网站的“ykc1.greatwestlife.com”,尽管如果我手动查看页面源代码会有很多事情,但我无法使用scrapy得到正确的响应。
在这种情况下我需要使用scrapy-splash吗?有任何想法吗?谢谢
解决方案
您实际上可以看到两个背靠背请求,由
<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
推荐阅读
- mysql - 迁移现有数据库字段。运行时出现 E340 错误
- python - 无法从网页中挖出 twitter 链接
- css - 为 verse on prem 自定义加载 CSS 文件而不是内联 css
- android - Flutter 启动画面在加载时将图像拉伸一秒钟
- mongodb - 根据从 mongo 检索到的值获取枚举键
- css - 在 IE 中无法使用的 fontawesome 图标之间的动画
- javascript - 如何使用 jQuery 正确计算输入值?
- ios - iOS - 如何将捆绑包放入 Bundle.main.resourcePath?
- c# - 应该在哪个项目中使用事件溯源在 DDD 中实时读取模型(投影)?
- python - 将 IOB 格式数据列表转换为简单 IOB 格式数据