당신이 우리에게 준 예제에서 말하기는 어렵지만, h2
다음 노드를 얻을 수 있습니다. 이 예에서, 루이스 캐롤는 p
-aragraph 태그를 가지고 있으며, 여러분의 친구 폴은 닫는에게 span
태그가 있습니다
>>> from BeautifulSoup import BeautifulSoup
>>>
>>> html = '''
... <h2 class="sectionTitle">BACKGROUND</h2>
... <p>Mr. Lewis Carroll has bla bla</p>
... <div style="margin-top:8px;">
... <a href="javascript:void(0)" onclick="show_more(this);">Read Full Background</a>
... </div>
... <h2 class="sectionTitle">BACKGROUND</h2>
... Mr. Paul J. Fribourg has bla bla</span>
... <div style="margin-top:8px;">
... <a href="javascript:void(0)" onclick="show_more(this);">Read Full Background</a>
... </div>
... '''
>>>
>>> soup = BeautifulSoup(html)
>>> headings = soup.findAll('h2', text='BACKGROUND')
>>> for section in headings:
... p = section.findNext('p')
... if p:
... print '> ', p.string
... else:
... print '> ', section.parent.next.next.strip()
...
> Mr. Lewis Carroll has bla bla
> Mr. Paul J. Fribourg has bla bla
다음 의견 :
>>> from BeautifulSoup import BeautifulSoup
>>> from urllib2 import urlopen
>>> html = urlopen('http://investing.businessweek.com/research/stocks/private/person.asp?personId=668561&privcapId=160900&previousCapId=285930&previousTitle=LOEWS%20CORP')
>>> soup = BeautifulSoup(html)
>>> headings = soup.findAll('h2', text='BACKGROUND')
>>> for section in headings:
... paragraph = section.findNext('p')
... if paragraph and paragraph.string:
... print '> ', paragraph.string
... else:
... print '> ', section.parent.next.next.strip()
...
> Mr. Paul J. Fribourg has been the President of Contigroup Companies Inc. (for [...]
당신은 할 수있다, 물론을 , 저작권 고지를 확인하고 싶다면, 등등 ...
감사합니다. 친절한 대답! 사실 Mr. Paul 전에는 이 없습니다. 코드를 실행하면 전체 배경 읽기가 표시됩니다 ....이 문제를 해결할 방법을 알려주시겠습니까? – Willy
@Willy : 내 원래 대답은 어떤 사람이 ''태그를 추가 한 질문에 대한 편집 내용이었습니다. 그에 따라 내 대답을 편집했습니다. – Johnsyweb
오, 정말 고마워요! 그것은 정말로 잘하고 있습니다 ..하지만 원래 웹 사이트에서는 작동하지 않습니다. : ((울고 싶습니다 .. – Willy