2016-07-23 1 views
1

사이트를 고치고 단락으로 나눕니다. 스크랩 한 텍스트를 보면 일부 단락 구분 기호가 올바르게 분할되지 않은 것을 볼 수 있습니다. 문제를 재현하는 코드는 아래를 참조하십시오!BeautifulSoup 파서가 태그별로 올바르게 분할되지 않았습니다.

from bs4 import BeautifulSoup 
import requests 

link = "http://www.presidency.ucsb.edu/ws/index.php?pid=111395" 
response = requests.get(link) 
soup = BeautifulSoup(response.content, 'html.parser') 
paras = soup.findAll('p') 
# Note that in printing the below, there are still a lot of "<p>" in that paragraph :( 
print paras[614] 

다른 파서를 사용해 보았습니다. 비슷한 문제입니다.

답변

0

이것은 의도적으로 설계된 동작입니다. 페이지가 중첩 된 단락을 포함하고 있기 때문에 발생 예 :

<p>Neurosurgeon Ben Carson. [<i>applause</i>] <p>New Jersey 

내가 문제를 해결하기 위해이 작은 해킹을 사용 :

html = response.content.replace('<p>', '</p><p>') # so there will be no nested <p> tags in your soup 

# then your code 
0

시도해 보셨습니까? lxml 파서? 비슷한 문제가있어서 lxml이 내 문제를 해결했습니다. 또한

import lxml 
... 
soup = BeautifulSoup(response.text, "lxml")  

대신 response.content의 유니 코드 개체를 가져 response.text을 시도해보십시오.

+0

불행히도 작동하지 않습니다 (LXML 또는 response.text을 사용 중) . 제안 tho 주셔서 감사합니다! – Craig

관련 문제