Pythonとbeautiuflsoupを使用してスクリーンスクレイピングを試みています。1つの問題に遭遇したのは、以前にテキスト値を取得する方法がわからないということです<br/>
以下に一例を示します。
<h3>
Francois Abboud
</h3>
Professor,
Internal Medicine
<br />
<br />
Pythonとbeautiuflsoupを使用してスクリーンスクレイピングを試みています。1つの問題に遭遇したのは、以前にテキスト値を取得する方法がわからないということです<br/>
以下に一例を示します。
<h3>
Francois Abboud
</h3>
Professor,
Internal Medicine
<br />
<br />
私はあなたが使用できると思います.next_sibling
:
>>> from bs4 import BeautifulSoup
>>>
>>> html = """
... <h3>
... Francois Abboud
... </h3>
... Professor,
... Internal Medicine
... <br />
... <br />
... """
>>>
>>> soup = BeautifulSoup(html)
>>>
>>> for h3 in soup.findAll("h3"):
... print 'h3:'
... print repr(h3)
... print 'next sibling:'
... print repr(h3.next_sibling)
...
h3:
<h3>
Francois Abboud
</h3>
next sibling:
u'\nProfessor,\xa0\nInternal Medicine\n'
2 分前はこれを行う方法がわかりませんでしたが、findAll
. そこで、h3
オブジェクトの 1 つを取得し、IPython のタブ補完を使用してその内部にあるものを確認しh3.next_element
ましh3.next_sibling
た。
tsup、そのhtmlを次のように文字列値に割り当てると:
html = 'your html here'
次に、それを分割すると、指定した区切り文字の両側に文字列を保持する文字列値の配列に文字列が分割されます。
def extractmyCrap(html):
h3splitStrings = html.split('</h3>')
h3splitStrings = h3splitStrings[1]
extractedText = h3splitStrings.split('<br/>')
return extractedText[0]
さらに助けが必要な場合、または質問を間違って理解した場合はコメントしてください