gpt4 book ai didi

xpath - Scrapy:如何获得正确的选择器

转载 作者:行者123 更新时间:2023-12-03 16:58:00 24 4
gpt4 key购买 nike

我想选择以下文本:

粗体普通斜体

我需要选择并得到:大胆的普通斜体字。

的HTML是:

<a href=""><strong>Bold</strong> normal <i>Italist</i></a>


但是, a/text()会产生


正常


只要。有人知道解决方法吗?我正在测试bing爬网,根据查询,粗体文本的位置不同。

最佳答案

您可以使用a//text()代替a/text()来获取所有文本项。

# -*- coding: utf-8 -*-
from scrapy.selector import Selector

doc = """
<a href=""><strong>Bold</strong> normal <i>Italist</i></a>
"""

sel = Selector(text=doc, type="html")

result = sel.xpath('//a/text()').extract()
print result
# >>> [u' normal ']

result = u''.join(sel.xpath('//a//text()').extract())
print result
# >>> Bold normal Italist

关于xpath - Scrapy:如何获得正确的选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44332752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com