selenium 中如何获取标签下的内容?

比如:

<div class="content"><p> The ele is cu <sub>2</sub></p></div>



  driver = webdriver.PhantomJS(executable_path='/usr/local/bin/phantomjs') 
  driver.get(request.url)
  content=driver.find_element_by_xpath("//div[@class='content']/p").text()
  
  


似乎获取不了The ele is cu <sub>2</sub>

???

阅读 15.2k
2 个回答

可以的,先用xpath定位到<div>这个标签,再这个下边定位<p>标签。
如果没记错,使用text属性就可以取到the ele is cu
定位到<div>这个标签,再这个下边定位标签,使用text,或者叫get_text属性(具体的你需要查询下),
就可以获取到2

#coding:utf-8


from bs4 import BeautifulSoup
import re
html= '''<div class="content"><p> The ele is cu <sub>2</sub></p></div>'''

soup = BeautifulSoup(html,'lxml')
cc = soup.find_all('p')[0]

ll = re.search('<p>(.*)</p>',str(cc)).group(1)

print ll

或者

#coding:utf-8


from bs4 import BeautifulSoup
import re
html= '''<div class="content"><p> The ele is cu <sub>2</sub></p></div>'''

soup = BeautifulSoup(html,'lxml')
cc = soup.find_all('p')[0]

print str(cc)[4:-4]
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题