[爬虫]通过 xpath 提取元素,目标混在多个节点名称相同之中,处理思路应该怎么做?(内详) - V2EX
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
qazwsxkevin
V2EX    Python

[爬虫]通过 xpath 提取元素,目标混在多个节点名称相同之中,处理思路应该怎么做?(内详)

  •  
  •   qazwsxkevin 2019-06-30 17:10:17 +08:00 3324 次点击
    这是一个创建于 2294 天前的主题,其中的信息可能已经有所发展或是发生改变。
    <div id="tranData"> <div class="tyfrom"> <div id="home"><img src="http://www.v2ex.com/img/223325.png" width="80" height="80"></div> </div> <div class="fat4"> <table width="100%" cellspacing="0" cellpadding="0"> <tbody> <tr> <th colspan="5" class="abc" align="center"> 课程表 </th> </tr> </table> </div> <div class="content"> <table width="800" cellspacing="0" cellpadding="0" align="center"> <tbody> <tr><th colspan="5" class="pit" align="center">哑铃</th></tr> <td></td> </tbody> </table> </div> <div class="content"> <table width="800" cellspacing="0" cellpadding="0" align="center"> <tbody> <tr><th colspan="5" class="pit" align="center">跑步机</th></tr> <td> <tr align="center"> <td class="a1" width="320"> <a href="http://192.168.1.155/sport/record/id1661.html" target="_blank" title="38 分钟">38 分钟</a> </td> <td class="a7" width="30"> <img src="http://www.v2ex.com/img/sh_img/finish.png" title="" style="cursor:pointer;"> </td> <td class="a3" width="100">38Min.</td> <td class="a1" width="30">14:29</td> <td class="a3" width="320">15:07</td> </tr> </td> </tbody> </table> </div> <div class="content"> <table width="800" cellspacing="0" cellpadding="0" align="center"> <tbody> <tr><th colspan="5" class="pit" align="center">踏步机</th></tr> <td></td> </tbody> </table> </div> 

    考虑到方便表达 html 代码的结构,瘦身了内容,调整了代码格式缩进,方便大家理解我的问题

    1、通过 xpath,定位到了

    //*[@id="tranData"] 

    2、我想提取 tranData 节点,下面的跑步机内容,在这个代码中是 content[2],但页面会根据情况变化,有可能会是[6]/[7]/[8]这样。在'跑步机'所在的 content 节点里,唯一特征就是有跑步机三个字了(对,就是 text()),其它的 content 格式是一致的
    3、etree.xpath,html.xpath 用什么方法能定位到这个 content,并把节点的代码弄出来呢?
    4、如何按顺序提取跑步机 content 下面的 td 的 text()内容? (td 的 class 并不是每条记录都固定是 a*)

    感谢大家热心解答!!

    3 条回复    2019-06-30 18:26:53 +08:00
    kppwp
        1
    kppwp  
       2019-06-30 18:07:52 +08:00 via iPhone
    //div[...../th/text()=‘跑步机’]获取父节点
    用父节点遍历子节点,不要用硬编码
    my8100
        2
    my8100  
       2019-06-30 18:10:48 +08:00
    <tr><th colspan="5" class="pit" align="center">跑步机</th></tr>
    <td>
    这里第二行的 <td> 应该是多余的

    ```
    In [215]: from scrapy import Selector

    In [216]: sel = Selector(text=doc)

    In [217]: sel.xpath("//th[contains(text(), '跑步机')]/parent::tr/following-sibling::tr/td/text()").extract()
    Out[217]:
    ['\n ',
    '\n ',
    '\n ',
    '\n ',
    '38Min.',
    '14:29',
    '15:07']

    In [218]: sel.xpath("//th[text()='跑步机']/parent::tr/following-sibling::tr/td/text()").extract()
    Out[218]:
    ['\n ',
    '\n ',
    '\n ',
    '\n ',
    '38Min.',
    '14:29',
    '15:07']

    In [219]:
    ```
    my8100
        3
    my8100  
       2019-06-30 18:26:53 +08:00
    参考 #1 的写法:
    ```
    In [229]: sel.xpath("//tbody[tr/th/text()='跑步机']/tr[@align='center']/td/text()").extract()
    Out[229]:
    ['\n ',
    '\n ',
    '\n ',
    '\n ',
    '38Min.',
    '14:29',
    '15:07']

    In [230]:
    ```
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3459 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 00:17 PVG 08:17 LAX 17:17 JFK 20:17
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86