2017-11-25 1 views
0

이전 웹 페이지에서 텍스트를 추출하려고하는데 문제가 있습니다. 웹 페이지 (http://www.presidency.ucsb.edu/ws/index.php?pid=119039)의 소스를 검사 텍스트가 시작 : 내가 사용하여 텍스트 추출 시도BeautifulSoup을 사용하여 텍스트 추출하기

> </div></div><span class="displaytext"><b>PARTICIPANTS:</b><br>Former Secretary of State 
> Hillary Clinton (D) and<br>Businessman Donald Trump 
> (R)<p><b>MODERATOR:</b><br>Chris Wallace (Fox News)<p><b>WALLACE:</b> 
> Good evening from the Thomas and Mack Center at the University of 
> Nevada, Las Vegas. I'm Chris Wallace of Fox News, and I welcome you to 
> the third and final of the 2016 presidential debates between Secretary 
> of State Hillary Clinton and Donald J. Trump.<p> 

:

link = "http://www.presidency.ucsb.edu/ws/index.php?pid=119039" 
debate_response = requests.get(link) 
debate_soup = BeautifulSoup(debate_response.content, 'html.parser') 
debate_text = debate_soup.find_all('div',{'span class':"displaytext"}) 
print(debate_text) 

을하지만 그냥 빈 목록을 반환합니다. 어떻게 내가 그 텍스트를 추출 할 수 있는지 어떤 생각?

답변

2

html.parser을 사용하여 최대 재귀 오류가 발생했기 때문에 lxml을 파서로 사용해야했습니다. 다음은 <span> 태그의 하위 태그에있는 모든 텍스트를 하나의 문자열로 추출합니다.

debate_soup = BeautifulSoup(debate_response.content, 'lxml') 
debate_text = debate_soup.find('span', {'class': 'displaytext'}).get_text() 
+0

정확히 내가 필요한 것. 고맙습니다 –

관련 문제