Regex multiline如何获取页面的一部分sou

2024-09-26 18:03:53 发布

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

很抱歉,如果您以前遇到过这个问题,我发现python regex文档很难理解,主要是因为缺少示例。 我想抓取一个页面源代码块,稍后再进行解析。例如:

    <div id="viewed"><div class="shortstory-block">

    <div class="shortstoey-block-image">
        <a href="...."><img src="/uploads/posts/cov.jpg" alt="instance 1"/></a>
        <span class="format"><a href="http://www..../">something</a></span>
    </div>

    <a href="http://....."><span class="shortstory-block-title" style="text-decoration:none !important;">
        Something
    </span>
    </a>

</div><div class="shortstory-block">

    <div class="shortstoey-block-image">
        <a href="...."><img src="/uploads/posts/cov.jpg" alt="something 2"/></a>
        <span class="format"><a href="http://www.website/xfsearch/smth/">something</a></span>
    </div>

    <a href="http://web.html"><span class="shortstory-block-title" style="text-decoration:none !important;">
        Something
    </span>
    </a>
 </div>
  (* x times)
     <div id="rated">....

我把所有的页面源代码都放在一个变量(html\u源代码)中,我只想用这段代码定义另一个变量(在div^{id1}之间)$

有人能给我指出正确的方向(regex表达式)吗?你知道吗

提前谢谢


Tags: imagedividhttpimg源代码页面block
2条回答

re.DOTALL标志使。匹配任何字符。没有那面旗子,它就不符合新行。你知道吗

(DOTALL也可以在regexp本身中拼写为(?s)

有关类似的问题,以及代码示例和更好的方法,请参见: Python's "re" module not working?

如果您确实只是想在文本的两个元素之间找到一些东西,可以使用以下正则表达式:

import re

with open('yourfile') as fin:
    page_source = fin.read()

start_text = re.escape('<div id="viewed">')
until_text = re.escape('<div id="rated">')
match_text = re.search('{}(.*?){}'.format(start_text, until_text), page_source, flags=re.DOTALL)
if match_text:
    print match_text.group(1)

相关问题 更多 >

    热门问题