【问题标题】:How to select nodes in html from lxml?如何从lxml中选择html中的节点?
【发布时间】: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.">&#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 脚本中,我想选择节点“物质名称”和“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)

【问题讨论】:

    标签: python html lxml


    【解决方案1】:

    您只需检查Name of SubstanceMeSH Heading 是否在网页的文本中,如果存在则选择内容。

    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)
    

    【讨论】:

    • 谢谢,成功了!我在一般的编程方面是一个完全的新手,像这样的简单事情正在慢慢地融合在一起。
    • 没问题@TimTom 很高兴我能帮上忙 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    相关资源
    最近更新 更多