【发布时间】:2015-06-29 14:32:46
【问题描述】:
我有一些来自我之前帖子How to set up XPath query for HTML parsing? 的http://chem.sis.nlm.nih.gov/chemidplus/rn/75-07-0 的html 代码,现在想创建一个逻辑过程,因为许多其他页面都相似,但并不完全相同。所以,
<div id="names">
<h2>Names and Synonyms</h2>
<div class="ds">
<button class="toggle1Col" title="Toggle display between 1 column of wider results and multiple columns.">↔</button>
<h3>Name of Substance</h3>
<ul>
<li id="ds2"><div>Acetaldehyde</div></li>
</ul>
<h3>MeSH Heading</h3>
<ul>
<li id="ds3"><div>Acetaldehyde</div></li>
</ul>
</div>
现在在我的 python 脚本中,我想选择节点“物质名称”和“MeSH 标题”并检查它们是否存在,如果存在则选择其中的数据,否则返回一个空字符串。有没有办法在 python 中像在 Javascript 中那样使用 Node myNode = doc.DocumentNode.SelectNode(/[text()="Name Of Substance"/)?
from lxml import html
import requests
import csv
page = requests.get(http://chem.sis.nlm.nih.gov/chemidplus/rn/75-07-0)
tree = html.fromstring(page.text)
if( Name of substance is there )
chem_name = tree.xpath('//*[text()="Name of Substance"]/..//div')[0].text_content()
else
chem_name = []
if ( MeSH Heading there )
mesh_name = tree.xpath('//*[text()="MeSH Heading"]/..//div')[1].text_content()
else
mesh_name = []
names1 = [chem_name, mesh_name]
with open('testchem.csv', 'wb') as myfile:
wr = csv.writer(myfile)
wr.writerow(names1)
【问题讨论】: