【问题标题】:How to get italic and non italic text with lxml如何使用lxml获取斜体和非斜体文本
【发布时间】:2017-11-10 09:18:20
【问题描述】:

我正在对表格中的每一行使用此命令,但我只得到不是斜体的文本。

name = ''.join(row.xpath('td[3]/a/text()'))

a 元素在<em> </em 标签中有一些文本。

<td class="cardname"><a href="http://www.mtgotraders.com/store/PRM_Ball_Lightning_f.html"><em>Ball</em> <em>Lightning</em> *Foil*</a></td>

我想得到Ball Lightning *Foil*

【问题讨论】:

  • 至少用一些可以使用斜体和非斜体文本的元素来更新你的问题。

标签: python xpath web-scraping lxml


【解决方案1】:

这是你想要的吗?无论您使用 xpath 还是 css 选择器,结果总是相同的。试一试:

html_content='''
<td class="cardname"><a href="http://www.mtgotraders.com/store/PRM_Ball_Lightning_f.html">
<em>Ball</em> <em>Lightning</em> *Foil*</a></td>
'''
from lxml.html import fromstring

root = fromstring(html_content)
item = root.cssselect(".cardname a")[0].text_content().strip()
item_alternative = root.xpath("//*[@class='cardname']/a")[0].text_content().strip()

print(item)
print(item_alternative)

结果:

Ball Lightning *Foil*
Ball Lightning *Foil*

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多