2016-07-23 4 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 Unicodeオブジェクトを取得するにはresponse.textを試してみてください。

+0

は(lxmlのかresponse.textのいずれかを使用して)残念ながら、動作しません。提案をありがとうtho! – Craig

関連する問題