我需要为div和strong编写xpath
td[contains(div/@class,'lpadding20')//text()
HTML:
<div class="lpadding20" style="font-weight: normal;">
<strong>Published: </strong>6/18/2019 at 11:18 AM. This list includes 501 eligible players.
</div>
答案 0 :(得分:0)
尝试:
//div[@class="lpadding20"]//strong[contains(text(), "text you are looking for")]
以获取准确的文字:
//div[@class="lpadding20"]//strong[text()="text you are looking for"]
答案 1 :(得分:0)
尝试以下xpath
来获取strong
文本。
from lxml.html import fromstring
htmlelem = """
<div class="lpadding20" style="font-weight: normal;">
<strong>Published: </strong>6/18/2019 at 11:18 AM. This list includes 501 eligible players.
</div>
"""
tree = fromstring(htmlelem)
itemstrong1 = tree.xpath("//div[@class='lpadding20']/strong/text()")[0].strip()
print(itemstrong1)
itemstrong2 = tree.xpath("//div[@class='lpadding20']/strong/following::text()")[0].strip()
print(itemstrong2)
答案 2 :(得分:0)
尝试一下
from scrapy.selector import Selector
body = '<div class="lpadding20" style="font-weight: normal;"><strong>Published: </strong>6/18/2019 at 11:18 AM. This list includes 501 eligible players.</div>'
before_strong = Selector(text=body).xpath("//div[@class='lpadding20']/strong/text()").extract_first()
after_strong = Selector(text=body).xpath("//div[@class='lpadding20']/strong/following::text()").extract_first()
print(before_strong)
print(after_strong)
输出:
'Published: '
'6/18/2019 at 11:18 AM. This list includes 501 eligible players.'