spary Xpath中的转义逗号

2024-04-19 21:44:18 发布

您现在位置:Python中文网/ 问答频道 /正文

为一只发痒的蜘蛛工作。在

我有这个html:

<div class="sliderContent">
<p>some content, some other content</p>
<p>some content, some other content</p>
<p>some content, some other content</p>
<p>some content, some other content</p>
</div>

我的xpath:

^{pr2}$

我想转义<p>中的逗号并提取所有内容,保留html。我试过了:

    def parse_dir_contents(self, response):
        for sel in response.xpath('//div[@class="container"]'):
        item = LuItem()
        item['Description'] = sel.xpath('div[@class="content"]/div/div[@class="sliderContent"]//p').extract()[0].replace(',','\,')
        yield item

这显然适用于第一个<p>,但是我如何才能为所有<p>实现这一点呢?在

从python开始,任何帮助都非常感谢!在


Tags: div内容responsehtmlsomecontentitemxpath
1条回答
网友
1楼 · 发布于 2024-04-19 21:44:18

您的分析结果是一个列表,您只修改了列表[0]中的第一个元素,您需要浏览整个描述列表:

def parse_dir_contents(self, response):
    for sel in response.xpath('//div[@class="container"]'):
        item = LuItem()
        item['Description'] = sel.xpath('div[@class="content"]/div/div[@class="sliderContent"]//p').extract()
        item['Description'] = [ ''.join(field.split(',')) for field in item.get('Description', [])]
        yield item

相关问题 更多 >