2016-05-07 5 views
-2

私は、例えば、特定のテキスト"Interested String ZZZ"他の段落の後に続く段落を、一致する文字列で削り取るにはどうすればよいですか?

で別の段落を次の段落のこすりしたい:

<p align="center"><strong><span style="text-decoration: underline;">Interested String ZZZ</span></strong></p> 
<p style="text-align: justify;"><span style="font-size: small;">This is the paragraph string that i want to scrape out</span></p> 

私はPythonでそれをどのように行うのですか?

答えて

0

その後、textそのテキストコンテンツで要素にマッチするパラメータ、および次の<p>兄弟要素を取得するためにfind_next_sibling()を使用用途:

>>> from bs4 import BeautifulSoup 
>>> raw = '''<div> 
... <p align="center"><strong><span style="text-decoration: underline;">Interested String ZZZ</span></strong></p> 
... <p style="text-align: justify;"><span style="font-size: small;">This is the paragraph string that i want to scrape out</span></p> 
... </div>''' 
... 
>>> soup = BeautifulSoup(raw, "lxml") 
>>> [s.find_next_sibling("p").string for s in soup("p", text="Interested String ZZZ")] 
[u'This is the paragraph string that i want to scrape out'] 
関連する問題