【问题标题】:How to get the data in between two strings in a html page如何在html页面中的两个字符串之间获取数据
【发布时间】:2018-12-05 16:33:39
【问题描述】:

我有几个看起来像这样的 html 页面:

标题

简介

目录:

选项1。艺术

选项2。运动

选项 3。跳舞

选择说明

选项1。艺术

一个。水彩 描述 湾。油画 描述 C。丙烯画 说明

选项2。运动

一个。篮球 描述 湾。蟋蟀 描述 C。足球 说明

选项 3。舞蹈

一个。霹雳舞

所有这些内容都以不同的 html 格式保存在每个 html 页面中。我想在每个页面的运动选项下收集整个文本。 (因为每个 html 页面的结构不同,所以除了找出 xpath 之外,我还能实现这一点吗)。

请帮忙。谢谢你。

示例 html:

<Document>
<TYPE>
<SEQUENCE>
<FILENAME>
<DESCRIPTION>
<TEXT>
<HTML>
<HEAD>
</HEAD>

<P style="font-family:times;;margin-left:10.0pt;text-indent:-10.0pt;"><FONT SIZE=2><B>

<!-- COMMAND=STYLE_ADDED,"margin-left:10.0pt;text-indent:-10.0pt;" -->

option 1. Art history: </B></FONT></P>

<P style="font-family:times;"><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The nature of art has been described by philosopher Richard Wollheim as "one of the most elusive of the traditional problems of human culture".[19] Art has been defined as a vehicle for the expression or communication of emotions and ideas </FONT></P>

<P style="font-family:times;;margin-left:10.0pt;text-indent:-10.0pt;"><FONT SIZE=2><B>

<!-- COMMAND=STYLE_ADDED,"margin-left:10.0pt;text-indent:-10.0pt;" -->

option 2. Sports division : </B></FONT></P>

<P style="font-family:times;"><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hundreds of sports exist, from those between single contestants, through to those with hundreds of simultaneous participants, either in teams or competing as individuals. In certain sports such as racing, many contestants may compete, simultaneously or consecutively, with one winner; in others, the contest (a match) is between two sides, each attempting to exceed the other.</FONT></P>

<P style="font-family:times;;margin-left:10.0pt;text-indent:-10.0pt;"><FONT SIZE=2><B>

<!-- COMMAND=STYLE_ADDED,"margin-left:10.0pt;text-indent:-10.0pt;" -->

option 3. Dance group: </B></FONT></P>

<P style="font-family:times;"><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;An important distinction is to be drawn between the contexts of theatrical and participatory dance,[4] although these two categories are not always completely separate; both may have special functions, </FONT></P>

【问题讨论】:

  • &lt;div section="sports".*?/&gt;(.*)&lt;div section="Dance"
  • 您好,我已经提供了示例 html。

标签: python html parsing web-scraping lxml


【解决方案1】:

好吧,我会在这里使用 jQuery .text(),因为它似乎完全符合您的要求(仅显示所有选定节点的文本内容)。

除此之外,选择正确的元素是一件简单的事情,因此我们确实需要知道要选择哪个“部分”。可能是这样的:

$("P:contains('option 2')").nextUntil("p:contains('option 3')").text()

console.log($("P:contains('option 2')").nextUntil("p:contains('option 3')").text())
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="scott">
<P style="font-family:times;;margin-left:10.0pt;text-indent:-10.0pt;"><FONT SIZE=2><B>

<!-- COMMAND=STYLE_ADDED,"margin-left:10.0pt;text-indent:-10.0pt;" -->

option 1. Art history: </B></FONT></P>

<P style="font-family:times;"><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The nature of art has been described by philosopher Richard Wollheim as "one of the most elusive of the traditional problems of human culture".[19] Art has been defined as a vehicle for the expression or communication of emotions and ideas </FONT></P>



<P style="font-family:times;;margin-left:10.0pt;text-indent:-10.0pt;"><FONT SIZE=2><B>

<!-- COMMAND=STYLE_ADDED,"margin-left:10.0pt;text-indent:-10.0pt;" -->

option 2. Sports division : </B></FONT></P>

<P style="font-family:times;"><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hundreds of sports exist, from those between single contestants, through to those with hundreds of simultaneous participants, either in teams or competing as individuals. In certain sports such as racing, many contestants may compete, simultaneously or consecutively, with one winner; in others, the contest (a match) is between two sides, each attempting to exceed the other.</FONT></P>

<div>some other random stuff here.</div>

<P style="font-family:times;;margin-left:10.0pt;text-indent:-10.0pt;"><FONT SIZE=2><B>

<!-- COMMAND=STYLE_ADDED,"margin-left:10.0pt;text-indent:-10.0pt;" -->

option 3. Dance group: </B></FONT></P>

<P style="font-family:times;"><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;An important distinction is to be drawn between the contexts of theatrical and participatory dance,[4] although these two categories are not always completely separate; both may have special functions, </FONT></P>

</div>

【讨论】:

    猜你喜欢
    • 2015-09-04
    • 1970-01-01
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    相关资源
    最近更新 更多