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
Frankhong
V2EX  ›  Python

爬虫问题。使用 Chrome 复制得到的 Xpath,抓取到的内容为空

  •  
  •   Frankhong · 2020-07-27 18:05:18 +08:00 · 1890 次点击
    这是一个创建于 1340 天前的主题,其中的信息可能已经有所发展或是发生改变。
    抓取的网址: https://leetcode-cn.com/problemset/all/?topicSlugs=greedy
    response.xpath("//*[@id="question-app"]/div/div[2]/div[2]/div[2]/table/tbody[1]/tr")的结果为空。请教各位大佬,问题出在了哪里呢?
    5 条回复    2020-07-28 09:41:19 +08:00
    coderunI
        1
    coderunI  
       2020-07-27 18:36:52 +08:00
    css 可以用的 .reactable-data .question-title a
    musi
        2
    musi  
       2020-07-27 18:49:07 +08:00
    ajax 请求?
    nullboy
        3
    nullboy  
       2020-07-27 18:53:26 +08:00 via Android
    尽量不用浏览器复制出来的 xpath 。不要那么长,尽量短些。另外尽量不要用 index 去定位。
    sheep3
        4
    sheep3  
       2020-07-27 19:10:02 +08:00
    你看看实际 http 返回的数据是什么

    view-source:https://leetcode-cn.com/problemset/all/?topicSlugs=greedy

    <div class="row" id="question-app"></div>内的数据都是生成的,你需要寻找接口数据来源,F12 看 network 具体找一下
    xueyoucai
        5
    xueyoucai  
       2020-07-28 09:41:19 +08:00
    xpath
    不识别 tbody 的 所以在代码中去掉 tbody 就好
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1981 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 16:21 · PVG 00:21 · LAX 09:21 · JFK 12:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.