【发布时间】:2015-09-25 09:26:21
【问题描述】:
我正在从网页中抓取数据,并且已针对具有<br> 标签的某个部分执行此操作。
<div class="scrollWrapper">
<h3>Smiles</h3>
CC=O<br>
<button type="button" id="downloadSmiles">Download</button>
</div>
我通过执行以下脚本输出CC=O 解决了这个问题。
from lxml import html
page = requests.get('http://chem.sis.nlm.nih.gov/chemidplus/name/'+ substance)
tree = html.fromstring(page.text)
if ("Smiles" in page.text):
smiles = tree.xpath('normalize-space(//*[text()="Smiles"]/..//br[1]/preceding-sibling::text()[1])')
else:
smiles = ""
但是,当我浏览其他不同化学品的页面时,我遇到了一些带有标签的页面。我不知道如何在获取它们之间的信息时摆脱它们。下面显示了一个示例,我想要的输出是c1(c2ccccc2)ccc(N)cc1。
<div class="scrollWrapper">
<h3>Smiles</h3>
c1(c2ccccc2)<wbr>ccc(N)<wbr>cc1<br>
<button type="button" id="downloadSmiles">Download</button>
</div>
【问题讨论】: