【发布时间】:2017-06-05 04:52:52
【问题描述】:
我尝试使用 Seleniung webdriver PhantomJS 在 span 内获取文本。我的代码是这样的:
href = driver.find_elements_by_xpath("//a[@class='_8mlbc _vbtk2 _t5r8b']")
for rt in href:
rt.click()
if href:
name = driver.find_elements_by_xpath("//*[@class='_99ch8']/span").text
# name = driver.find_element_by_xpath("//li[a[@title='nike']]/span").text
print(name)
在 HTML 中:
<li class="_99ch8"><a class="_4zhc5 notranslate _ebg8h" title="nike" href="/nike/">nike</a><span><span>Nobody believed a boy from Madeira would make it to the stars. Except the boy from Madeira. </span><br><a href="/explore/tags/nike/">#nike</a><span> </span><a href="/explore/tags/soccer/">#soccer</a><span> </span><a href="/explore/tags/football/">#football</a><span> </span><a href="/explore/tags/cr7/">#CR7</a><span> </span><a href="/explore/tags/cristiano/">#Cristiano</a><span> </span><a href="/explore/tags/cristianoronaldo/">#CristianoRonaldo</a><span> </span><a href="/explore/tags/mercurial/">#Mercurial</a><span> </span><a href="/explore/tags/justdoit/">#justdoit</a></span></li>
我想尝试在 span 中获取文本。
【问题讨论】:
-
能否提供HTML
-
这是 HTML,彼得:
- nike没有人相信一个来自马德拉岛的男孩会登上星星。除了来自马德拉岛的男孩。
- nike没有人相信一个来自马德拉岛的男孩会登上星星。除了来自马德拉岛的男孩。
-
您能否使用
HTML代码更新您的问题(使用“编辑”按钮)。 cmets 中提供的示例似乎并不完整。还要指定您想要获得的所需输出 -
@StefaniJohnsson 您可以考虑分享相关的网址和您尝试执行的手动步骤吗?谢谢
-
@Andersson 我在问题中添加 HTML
标签: python selenium selenium-webdriver phantomjs