0

以下是输入文件的示例:

<html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
    HERE IS A LOT OF TEXT, THAT IS NOT INTERESTING 
    <br>
      <div id="text"><div id="text-interesting1">11/222-AA</div>
      <h2>This is the title</h2>

            <P>Here is some multiline desc-<br>
                 cription about what is <br><br>
                 going on here
      </div>

      <div id="text2"><div id="text-interesting2">IV-VI</div>
        <br>
        <h1> Some really interesting text</h1>
</body>
</html>

现在我想 grep 这个文件的多个块,比如介于之间<div id="text-interesting1"></div>然后介于之间,然后介于<P></div>之间<div id="text-interesting2">以及</div>更多。关键是,我要检索多个值。

我想将这些值写入文件,例如逗号分隔。怎么可能呢?

从卢克提供的例子中,我做了以下几点:

import os, re
path = 'C:/Temp/Folder1/allTexts'
listing = os.listdir(path)
for infile in listing:
    text = open(path + '/' + infile).read()
    match = re.search('<div id="text-interesting1">', text)
    if match is None:
        continue
    start = match.end()
    end = re.search('</div>', text).start()
    print (text[start:end])


    match = re.search('<h2>', text)
    if match is None:
        continue
    start = match.end()
    end = re.search('</h2>', text).start()
    print (text[start:end])


    match = re.search('<P>', text)
    if match is None:
        continue
    start = match.end()
    end = re.search('</div>', text).start()
    print (text[start:end])


    match = re.search('<div id="text-interesting2">', text)
    if match is None:
        continue
    start = match.end()
    end = re.search('</div>', text).start()
    print (text[start:end])


    match = re.search('<h1>', text)
    if match is None:
        continue
    start = match.end()
    end = re.search('</h1>', text).start()
    print (text[start:end])

    print ('--------------------------------------')

输出是:

11/222-AA
This is the title


 Some really interesting text
--------------------------------------
22/4444-AA
22222 This is the title2


22222222222222222222222
--------------------------------------
33/4444-AA
3333 This is the title3


333333333333333333333333
--------------------------------------

为什么

部分不起作用?

4

2 回答 2

1

这是一个开始:

import os, re
path = 'C:/Temp/Folder1/allTexts'
listing = os.listdir(path)
for infile in listing:
    text = open(path + '/' + infile).read()
    match = re.search('<div id="text-interesting1">', text)
    if match is None:
        continue
    start = match.start()
    end = re.search('<div id="text-interesting2">', text).start()
    print text[start:end]
于 2012-07-17T17:09:28.357 回答
0

另一种策略是解析 XML。您需要整理文件,因为严格的 XML 需要匹配标签、大小写一致性等。这是一个示例:

from xml.etree import ElementTree
from cStringIO import StringIO
import sys
tree = ElementTree.ElementTree()
tree.parse(StringIO(sys.stdin.read()))
print "All tags:"
for e in tree.getiterator():
    print e.tag
    print e.text
print "Only div:"
for i in tree.find("{http://www.w3.org/1999/xhtml}body").findall("{http://www.w3.org/1999/xhtml}div"):
    print i.text

对文件稍作修改后运行:

<html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
HERE IS A LOT OF TEXT, THAT IS NOT INTERESTING 
<br></br>
<div id="text"><div id="text-interesting1">11/222-AA</div>
  <h2>This is the title</h2>
  <p>Here is some multiline desc-<br></br>
      cription about what is <br></br><br></br>
    going on here</p>
</div>
  <div id="text-interesting2">IV-VI</div>
      <br></br>
    <h1> Some really interesting text</h1>
</body>
</html>

示例输出,

> cat file.xml | ./tb.py 
All tags:
{http://www.w3.org/1999/xhtml}html


{http://www.w3.org/1999/xhtml}head


{http://www.w3.org/1999/xhtml}body

HERE IS A LOT OF TEXT, THAT IS NOT INTERESTING 

{http://www.w3.org/1999/xhtml}br
None
{http://www.w3.org/1999/xhtml}div
None
{http://www.w3.org/1999/xhtml}div
11/222-AA
{http://www.w3.org/1999/xhtml}h2
This is the title
{http://www.w3.org/1999/xhtml}p
Here is some multiline desc-
{http://www.w3.org/1999/xhtml}br
None
{http://www.w3.org/1999/xhtml}br
None
{http://www.w3.org/1999/xhtml}br
None
{http://www.w3.org/1999/xhtml}div
IV-VI
{http://www.w3.org/1999/xhtml}br
None
{http://www.w3.org/1999/xhtml}h1
Some really interesting text
Only div:
None
IV-VI

但是很多 HTML 很难解析为严格的 XML,因此对于您的情况,这可能很难实现。

于 2012-07-17T17:50:27.733 回答