如何获取没有HTML标记的文本

时间:2018-03-17 07:31:45

标签: xpath beautifulsoup ixmldomelement

以下是HTML:

<div class="ajaxcourseindentfix">
    <h3>CPSC 353 - Introduction to Computer Security (3) </h3>
    <hr>Security goals, security systems, access controls, networks and security, integrity, cryptography fundamentals, authentication. Attacks: software, network, website; management considerations, security standards in government and industry; security issues in requirements, architecture, design, implementation, testing, operation, maintenance, acquisition, and services.
    <br>
    <br>Prerequisite: <a href="preview_course_nopop.php?catoid=16&amp;coid=96570" onclick="acalogPopup()">CPSC 253U</a>
    <span style="display: none !important">&nbsp;</span>&nbsp;or <a href="#" onclick="acalogPopup()" target="_blank">CPSC 254</a>
    <span style="display: none !important">&nbsp;</span>&nbsp;and <a href="#" onclick="acalogPopup()" target="_blank">CPSC 351</a>
    <span style="display: none !important">&nbsp;</span>
    , declared major/minor in CPSC, CPEN, or CPEI
    <br>
</div>

我需要从此HTML中获取以下文字:

从第6行 -
从第7行 -
,在CPSC,CPEN或CPEI中宣布主要/次要

我能够通过以下XPath获得href [课程编号:CPSC 254等...]:

 # This xpath gives me all the tags followed by h3 and then I iterate through them in my script.  
//div[@class='ajaxcourseindentfix']/h3/following-sibling::text()[2]/following-sibling::*

更新

然后是带有以下XPath的文本:

# This xpath gives me all the text after the h3 tag.  
//div[@class='ajaxcourseindentfix']/h3/following-sibling::text()[2]/following-sibling::text()

我需要像在网址1一样使用这些课程名称/先决条件。

enter image description here

在这种方法中,我首先获得所有HREF,然后是所有文本。有没有更好的方法来实现这一目标?我不想迭代2个XPath来获得HREF,然后是Text,然后将它们组合起来形成先决条件字符串。

1 http://catalog.fullerton.edu/ajax/preview_course.php?catoid=16&coid=99648&show

1 个答案:

答案 0 :(得分:2)

尝试使用以下代码获取所需的输出:

div = soup.select("div.ajaxcourseindentfix")[0]
" ".join([word for word in div.stripped_strings]).split("Prerequisite: ")[-1]

输出

'CPSC 253U or CPSC 254 and CPSC 351 , declared major/minor in CPSC, CPEN, or CPEI'
相关问题