V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
wyzerg
V2EX  ›  Python

scrapy 的 Spider 通过 url 请求居然和浏览器通过 url 请求的 requests 的 html 不一样!?我傻了

  •  
  •   wyzerg · 2020-01-27 16:50:36 +08:00 · 3476 次点击
    这是一个创建于 1543 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我在 Spider 的 parse 函数是这样写的

    import scrapy
    
    class ZhipinSpider(scrapy.Spider):
        name = 'zhipin'
       
        start_urls = ['https://www.zhipin.com/job_detail/?query=python&city=101010100']
    
        def parse(self, response):
            list = response.xpath('//body/div[1]')
    
            print('list:',list)
    

    运行之后,解析到的结果是body 下面的子标签我通过 chrome 开发工具看到的 html,和爬虫的结果 html 不同,我傻了

    比如我浏览器看到的是body/div[@id="wrap"] 就能继续向下解析

    但是 scrapy 的 Spider 解析 body 下面 是

     ['<div class="data-tips">\n            <div class="tip-inner">\n                <div class="boss-loading">\n                    <span class="component-b">B</span><span class="component-o">O</span><span class="component-s1">S</span><span class="component-s2">S</span>\n                    <p class="gray">正在加载中...</p>\n                </div>\n            </div>\n        </div>']
    

    what ?这些**div class="data-tips" ** 我从来没看到。。。

    网上搜了下有朋友给出答案:

    同样的 URL 浏览器会加载 js 事件,所以浏览器和 python 爬虫类 requests 打开同样的 URL 获取的源码肯定是不一样的,你拷贝谷歌的 xpath 去提取 requests 的 html 大部分肯定出问题。失败原因一般是多了不存在的 tobdy 节点、标签样式(id、class)不同、标签顺序不同等
    

    请问 如何实现 爬虫的 Spider 去抓取 能像浏览器一样 实现浏览器一样的结果.....我猜这是反爬措施吧,但是有没有老哥能帮忙看看如何搞 :(

    9 条回复    2020-01-29 01:25:13 +08:00
    jugelizi
        1
    jugelizi  
       2020-01-27 16:52:45 +08:00
    都知道 js 返回直接拿接口啊
    wyzerg
        2
    wyzerg  
    OP
       2020-01-27 17:03:15 +08:00
    @jugelizi 老哥我看那个 get 请求回来的就是 html 是这个 https://www.zhipin.com/job_detail/?query=python&city=101010100,返回的 html 从里面继续解析数据了
    sunwei0325
        3
    sunwei0325  
       2020-01-27 17:09:31 +08:00
    如果页面数据是 js 渲染上去的可以用 pyppeteer 作为 download middleware
    itskingname
        4
    itskingname  
       2020-01-27 20:01:32 +08:00   ❤️ 2
    这个时候,与其网上乱搜索,不如买一本我写的书: https://item.jd.com/12436581.html

    你遇到的所有情况我都给你写好了。
    lichdkimba
        5
    lichdkimba  
       2020-01-27 21:15:22 +08:00
    不行就 headless 一把梭啊
    lbfeng
        6
    lbfeng  
       2020-01-27 22:34:42 +08:00
    到 network 里找第一个 type 是 document 的请求。。。那才是原始的 html 文件
    Juszoe
        7
    Juszoe  
       2020-01-28 11:22:43 +08:00 via Android
    不太像反爬,只是正常的业务逻辑,你没有分析人家的网络请求
    wyzerg
        8
    wyzerg  
    OP
       2020-01-29 00:53:27 +08:00
    @lbfeng 老哥,,,,,emm 没找到 我直接 com+F 搜索 document,搜出来都是返回的 json 里和 script 脚本里有 document。。。 -,-
    lbfeng
        9
    lbfeng  
       2020-01-29 01:25:13 +08:00
    network 里有 doc tab。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3471 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 00:07 · PVG 08:07 · LAX 17:07 · JFK 20:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.