2017-05-26 2 views
1

Beautifulsoupを使用して、タグで囲まれていない文字列を見つけて<p>で囲んでいますが、その方法はわかりません。タグで囲まれていない文字列の検索方法

<p>string</p> 
<figure class="image"> <img alt="" src="sample.jpg"/> </figure> 
string,string,string<br/> 
<p>string,string</p> 
string 
<p><a href="/test" target="_blank">string</a></p> 
textexttext 
<p>stringstring</p> 
<p><a href="tel:xxxxxxxx" target="_blank">xxxxxxxxxx</a></p> 
<div>textextext</div> 
<p>string,string<br/>string</p> 

私は、次の方法を試してみましたが、それは

from bs4 import BeautifulSoup as BS 

html = """<p>string</p> 
    <figure class="image"> <img alt="" src="sample.jpg"/> </figure> 
    <p>string,string,string</p><br/> <-here 
    <p>string,string</p> 
    <p>string</p> <- here 
    <p><a href="/test" target="_blank">string</a></p> 
    <p>textexttext</p> <- here 
    <p>stringstring</p> 
    <p><a href="tel:xxxxxxxx" target="_blank">xxxxxxxxxx</a></p> 
    <div>textextext</div> 
    <p>string,string<br/>string</p>""" 

soup = BS(html, "html.parser") 

while True: 
    text = soup.find(text=True) 
    if not text: 
     break 
    if not text.parent.name in ['p', 'span', 'a', 'div']: 
     text.wrap(content.new_tag("p")) 
+0

は* BeautifulSoupを使用するには、*あなたが必要ですか? – zezollo

答えて

1

次のことを試してみてください動作しませんでした

<p>string</p> 
<figure class="image"> <img alt="" src="sample.jpg"/> </figure> 
<p>string,string,string</p><br/>  <-here 
<p>string,string</p> 
<p>string</p>      <- here 
<p><a href="/test" target="_blank">string</a></p> 
<p>textexttext</p>    <- here 
<p>stringstring</p> 
<p><a href="tel:xxxxxxxx">xxxxxxxxxx</a></p> 
<div>textextext</div> 
<p>string,string<br/>string</p> 

上記のように、私は、HTMLを変更したい:

from bs4 import BeautifulSoup as bs 

html = """<p>string</p> 
<figure class="image"> <img alt="" src="sample.jpg"/> </figure> 
string,string,string<br/> 
<p>string,string</p> 
string 
<p><a href="/test" target="_blank">string</a></p> 
textexttext 
<p>stringstring</p> 
<p><a href="tel:xxxxxxxx" target="_blank">xxxxxxxxxx</a></p> 
<div>textextext</div> 
<p>string,string<br/>string</p>""" 

soup = bs(html, "html.parser") 

for text in soup.find_all(text=True): 
    if not text.parent.name in ['p', 'span', 'a', 'div'] and len(text.strip()): 
     text.wrap(soup.new_tag("p")) 

print soup  

これはあなたに次のouを与えるでしょうTPUT:

<p>string</p> 
<figure class="image"> <img alt="" src="sample.jpg"/> </figure><p> 
string,string,string</p><br/> 
<p>string,string</p><p> 
string 
</p><p><a href="/test" target="_blank">string</a></p><p> 
textexttext 
</p><p>stringstring</p> 
<p><a href="tel:xxxxxxxx" target="_blank">xxxxxxxxxx</a></p> 
<div>textextext</div> 
<p>string,string<br/>string</p>  
0

文字列は、あなたがそのような何かを行うことができます行の先頭にalwanysある場合:

import re 
text = """<p>string</p> 
<figure class="image"> <img alt="" src="sample.jpg"/> </figure> 
string,string,string<br/> 
<p>string,string</p> 
string 
<p><a href="/test" target="_blank">string</a></p> 
textexttext 
<p>stringstring</p> 
<p><a href="tel:xxxxxxxx" target="_blank">xxxxxxxxxx</a></p> 
<div>textextext</div> 
<p>string,string<br/>string</p>""" 
r = re.compile(r"(?<=\n)([^\n<>\\]*)") 
print(r.sub('<p>\g<1></p>', text)) 
関連する問題