很抱歉,如果這個問題已經提交給你,但我發現python正則表達式文檔很難理解,主要是由於缺乏示例。 我想抓取一個頁面源代碼塊,以後再解析。例如:正則表達式多行 - 如何抓取頁面源的一部分
<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_source)的所有頁面源和我想要定義只(DIV ID之間=「觀看」的代碼塊和DIV ID另一個變量=「額定」 )。儘管我可以在兩個實例之間找到\ n或\ r,但我想抓住所有內容。
有人能指出我在正確的方向(正則表達式)?
在此先感謝
正確的方向是使用一個html解析器。 –
是的,我同意btoueg。如果你想要一個快速解決方案,[BeautifulSoup](http://www.crummy.com/software/BeautifulSoup/bs4/doc/)很有用。這非常棒。 –
@ user2574761快速解決方案是使用解析器來模擬您想要解析的語言,而不是嘗試從頭開始編寫這樣一種解析器,該解析器使用的形式不符合您想要解析的語言。 (關於HTML是**不是常規語言的主要示例) – millimoose