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

Scrapy xpath 匹配不到一些数据

  •  
  •   yangjiaronga · 2017-02-06 23:48:21 +08:00 · 1967 次点击
    这是一个创建于 2635 天前的主题,其中的信息可能已经有所发展或是发生改变。
    <table class="tabledataformat" cellspacing="0" >
    	<tr>
    		<td style="vertical-align:top;">Copper, Cu&nbsp;</td>
        	<td class="dataCell" style="vertical-align:top;"><= 0.03 %<span 		     class="dataCondition"></span></td>
        	<td class="dataCell" style="vertical-align:top;"><= 0.03 %<span class="dataCondition"></span></td>
        	<td class="dataComment" style="vertical-align:top;"></td>
        </tr>
    </table>
    

    response.xpath('//table[@class="tabledataformat"]/tr').extract() 只能获取到

    <tr>
    		<td style="vertical-align:top;">Copper, Cu&nbsp;</td>
        	<td class="dataCell" style="vertical-align:top;"></td>
        	<td class="dataCell" style="vertical-align:top;"></td>
        	<td class="dataComment" style="vertical-align:top;"></td>
        </tr>
    

    <= 0.03 % 和 消失不见,为什么呢?

    4 条回复    2017-03-07 10:56:49 +08:00
    imn1
        1
    imn1  
       2017-03-04 16:37:21 +08:00
    因为<=的写法不符合 xml 标准
    leavic
        2
    leavic  
       2017-03-04 16:39:45 +08:00
    这部分数据可能是 javascript 异步请求显示的,也就是 ajax 内容, scrapy 是看不到的。
    dsg001
        3
    dsg001  
       2017-03-04 19:24:35 +08:00
    '''
    <tr> <td style="vertical-align:top;">Copper, Cu&#160;</td> <td class="dataCell" style="vertical-align:top;">&lt;= 0.03 %<span class="dataCondition"></span></td> <td class="dataCell" style="vertical-align:top;">&lt;= 0.03 %<span class="dataCondition"></span></td> <td class="dataComment" style="vertical-align:top;"></td> </tr>
    '''

    测试 lxml 能输出, scrapy 应该也没问题,查看 html 源码吧
    crazypig14
        4
    crazypig14  
       2017-03-07 10:56:49 +08:00
    scrapy 爬下来用 beautifulsoup 处理,我觉得方便些
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2916 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 11:22 · PVG 19:22 · LAX 04:22 · JFK 07:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.