xpath对没有值的元素能否返回空而不是忽略
<tr class=""><td class="title">
<atitle="9.29演出
" class="">
9.29演出
</a>
</td>
<td nowrap="nowrap">
<aclass="">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">
<atitle="9.29演出
" class="">
9.29演出
</a>
</td>
<td nowrap="nowrap">
<aclass="">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,能不能第一行没有数据获取留个空啊什么的,不要直接忽略
不然这样获得的列表长度就不固定了 本帖最后由 YunGuo 于 2020-11-13 01:17 编辑
如果标签之间有空格,可以尝试parsel模块:
from parsel import Selector
html = """
<tr class="">
<td class="title">
<atitle="9.29演出" class="">
9.29演出
</a>
</td>
<td nowrap="nowrap">
<aclass="">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">
<atitle="9.29演出" class="">
9.29演出
</a>
</td>
<td nowrap="nowrap">
<aclass="">meu</a>
</td>
<td nowrap="nowrap" class="r-count ">50</td>
<td nowrap="nowrap" class="time">2018-09-07</td>
</tr>
"""
sel = Selector(html)
count = sel.xpath('//*[@class="r-count "]/text()').extract()
print(count)
如果标签之间没有空格,可以用尝试pyquery模块:
from pyquery import PyQuery as pq
html = """
<tr class="">
<td class="title">
<atitle="9.29演出" class="">
9.29演出
</a>
</td>
<td nowrap="nowrap">
<aclass="">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">
<atitle="9.29演出" class="">
9.29演出
</a>
</td>
<td nowrap="nowrap">
<aclass="">meu</a>
</td>
<td nowrap="nowrap" class="r-count ">50</td>
<td nowrap="nowrap" class="time">2018-09-07</td>
</tr>
"""
doc = pq(html)
count = doc('.r-count').items()
counts = []
for i in count:
num = i.text()
counts.append(num)
print(counts) YunGuo 发表于 2020-11-13 01:16
如果标签之间有空格,可以尝试parsel模块:
xpath做不出来吗? 你非得要用xpath那也可以,可以用parsel + w3lib模块,利用remove_tags方法删除标签,可以得到包含空值的结果:
from parsel import Selector
from w3lib.html import remove_tags
html = """
<tr class="">
<td class="title">
<atitle="9.29演出" class="">
9.29演出
</a>
</td>
<td nowrap="nowrap">
<aclass="">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">
<atitle="9.29演出" class="">
9.29演出
</a>
</td>
<td nowrap="nowrap">
<aclass="">meu</a>
</td>
<td nowrap="nowrap" class="r-count ">50</td>
<td nowrap="nowrap" class="time">2018-09-07</td>
</tr>
"""
sel = Selector(html)
cou = list(map(remove_tags, sel.xpath('//*[@class="r-count "]').extract()))
print(cou)
页:
[1]