如何从lxml中选择html中的节点?

2024-04-28 22:59:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我从上一篇文章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.">&#8596;</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脚本中,我想选择节点“Name of Substance”和“MeSH Heading”,并检查它们是否存在,如果存在,则选择其中的数据,否则返回一个空字符串。有没有一种方法可以像Javascript那样在python中使用Node myNode=doc.DocumentNode.SelectNode(/[text()=“物质名称”/)?在

^{pr2}$

Tags: andofnamedividbuttonlih2
1条回答
网友
1楼 · 发布于 2024-04-28 22:59:14

您只需检查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" in page.text):
    chem_name = tree.xpath('//*[text()="Name of Substance"]/..//div')[0].text_content()
else:
    chem_name = ""

if ("MeSH Heading" in page.text):
    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)

相关问题 更多 >