鱼C论坛

 找回密码
 立即注册
查看: 3715|回复: 3

xpath对没有值的元素能否返回空而不是忽略

[复制链接]
发表于 2020-11-12 21:32:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
<tr class="">
                <td class="title">
                    
                    <a  title="9.29演出
" class="">
                       9.29演出

                    </a>
                </td>
                <td nowrap="nowrap">
                    <a  class="">meu</a>
                </td>
                <td nowrap="nowrap" class="r-count "></td>
                <td nowrap="nowrap" class="time">2018-09-07</td>
</tr>

-------------------------------------------------------------
<tr class="">
                <td class="title">
                    
                    <a  title="9.29演出
" class="">
                       9.29演出

                    </a>
                </td>
                <td nowrap="nowrap">
                    <a  class="">meu</a>
                </td>
                <td nowrap="nowrap" class="r-count ">50</td>
                <td nowrap="nowrap" class="time">2018-09-07</td>
</tr>





类似这样的html用xpath获取 /tr/td[@class="r-count "]/text(),但第一行为空,第二行可以正常取得50,能不能第一行没有数据获取留个空啊什么的,不要直接忽略
不然这样获得的列表长度就不固定了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-13 01:16:21 | 显示全部楼层
本帖最后由 YunGuo 于 2020-11-13 01:17 编辑

如果标签之间有空格,可以尝试parsel模块:
  1. from parsel import Selector

  2. html = """
  3. <tr class="">
  4.                 <td class="title">
  5.                     <a  title="9.29演出" class="">
  6.                        9.29演出
  7.                     </a>
  8.                 </td>
  9.                 <td nowrap="nowrap">
  10.                     <a  class="">meu</a>
  11.                 </td>
  12.                 <td nowrap="nowrap" class="r-count "> </td>
  13.                 <td nowrap="nowrap" class="time">2018-09-07</td>
  14. </tr>
  15. <tr class="">
  16.                 <td class="title">
  17.                     <a  title="9.29演出" class="">
  18.                        9.29演出
  19.                     </a>
  20.                 </td>
  21.                 <td nowrap="nowrap">
  22.                     <a  class="">meu</a>
  23.                 </td>
  24.                 <td nowrap="nowrap" class="r-count ">50</td>
  25.                 <td nowrap="nowrap" class="time">2018-09-07</td>
  26. </tr>
  27. """
  28. sel = Selector(html)
  29. count = sel.xpath('//*[@class="r-count "]/text()').extract()
  30. print(count)
复制代码


如果标签之间没有空格,可以用尝试pyquery模块:
  1. from pyquery import PyQuery as pq

  2. html = """
  3. <tr class="">
  4.                 <td class="title">
  5.                     <a  title="9.29演出" class="">
  6.                        9.29演出
  7.                     </a>
  8.                 </td>
  9.                 <td nowrap="nowrap">
  10.                     <a  class="">meu</a>
  11.                 </td>
  12.                 <td nowrap="nowrap" class="r-count "></td>
  13.                 <td nowrap="nowrap" class="time">2018-09-07</td>
  14. </tr>
  15. <tr class="">
  16.                 <td class="title">
  17.                     <a  title="9.29演出" class="">
  18.                        9.29演出
  19.                     </a>
  20.                 </td>
  21.                 <td nowrap="nowrap">
  22.                     <a  class="">meu</a>
  23.                 </td>
  24.                 <td nowrap="nowrap" class="r-count ">50</td>
  25.                 <td nowrap="nowrap" class="time">2018-09-07</td>
  26. </tr>
  27. """
  28. doc = pq(html)
  29. count = doc('.r-count').items()
  30. counts = []
  31. for i in count:
  32.     num = i.text()
  33.     counts.append(num)
  34. print(counts)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-15 19:31:07 | 显示全部楼层
YunGuo 发表于 2020-11-13 01:16
如果标签之间有空格,可以尝试parsel模块:

xpath做不出来吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-16 00:34:17 | 显示全部楼层
你非得要用xpath那也可以,可以用parsel + w3lib模块,利用remove_tags方法删除标签,可以得到包含空值的结果:

  1. from parsel import Selector
  2. from w3lib.html import remove_tags

  3. html = """
  4. <tr class="">
  5.                 <td class="title">
  6.                     <a  title="9.29演出" class="">
  7.                        9.29演出
  8.                     </a>
  9.                 </td>
  10.                 <td nowrap="nowrap">
  11.                     <a  class="">meu</a>
  12.                 </td>
  13.                 <td nowrap="nowrap" class="r-count "></td>
  14.                 <td nowrap="nowrap" class="time">2018-09-07</td>
  15. </tr>
  16. <tr class="">
  17.                 <td class="title">
  18.                     <a  title="9.29演出" class="">
  19.                        9.29演出
  20.                     </a>
  21.                 </td>
  22.                 <td nowrap="nowrap">
  23.                     <a  class="">meu</a>
  24.                 </td>
  25.                 <td nowrap="nowrap" class="r-count ">50</td>
  26.                 <td nowrap="nowrap" class="time">2018-09-07</td>
  27. </tr>
  28. """
  29. sel = Selector(html)
  30. cou = list(map(remove_tags, sel.xpath('//*[@class="r-count "]').extract()))
  31. print(cou)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-29 19:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表