2014-04-02 21 views
29

Tôi đang cố gắng xóa tất cả html/javascript bằng bs4, tuy nhiên, nó không loại bỏ javascript. Tôi vẫn thấy nó ở đó với văn bản. Làm sao để tôi có được xung quanh này?BeatifulSoup4 get_text vẫn có javascript

Tôi đã thử sử dụng nltk hoạt động tốt tuy nhiên, clean_htmlclean_url sẽ bị xóa di chuyển về phía trước. Có cách nào để sử dụng súp get_text và nhận được kết quả tương tự không?

Tôi cố gắng nhìn vào các trang khác:

BeautifulSoup get_text does not strip all tags and JavaScript

Hiện nay tôi đang sử dụng chức năng phản đối của NLTK.

EDIT

Dưới đây là một ví dụ:

import urllib 
from bs4 import BeautifulSoup 

url = "http://www.cnn.com" 
html = urllib.urlopen(url).read() 
soup = BeautifulSoup(html) 
print soup.get_text() 

tôi vẫn thấy sau cho CNN:

$j(function() { 
"use strict"; 
if (window.hasOwnProperty('safaripushLib') && window.safaripushLib.checkEnv()) { 
var pushLib = window.safaripushLib, 
current = pushLib.currentPermissions(); 
if (current === "default") { 
pushLib.checkPermissions("helloClient", function() {}); 
} 
} 
}); 

/*globals MainLocalObj*/ 
$j(window).load(function() { 
'use strict'; 
MainLocalObj.init(); 
}); 

Làm thế nào tôi có thể loại bỏ các js?

tùy chọn Chỉ khác mà tôi tìm thấy là:

https://github.com/aaronsw/html2text

Vấn đề với html2text là nó thực sự thực sự chậm vào những thời điểm, và tạo ra độ trễ đáng chú ý, đó là một điều NLTK luôn luôn rất tốt với .

+0

Nó thực sự sẽ giúp đỡ nếu chúng ta có thể nhìn thấy (một phần của) html bao gồm javascript –

+0

gia tăng một ví dụ. – KVISH

Trả lời

55

Dựa một phần vào Can I remove script tags with BeautifulSoup?

import urllib 
from bs4 import BeautifulSoup 

url = "http://www.cnn.com" 
html = urllib.urlopen(url).read() 
soup = BeautifulSoup(html) 

# kill all script and style elements 
for script in soup(["script", "style"]): 
    script.decompose() # rip it out 

# get text 
text = soup.get_text() 

# break into lines and remove leading and trailing space on each 
lines = (line.strip() for line in text.splitlines()) 
# break multi-headlines into a line each 
chunks = (phrase.strip() for line in lines for phrase in line.split(" ")) 
# drop blank lines 
text = '\n'.join(chunk for chunk in chunks if chunk) 

print(text) 
+5

thay vì 'script.extract()', tốt hơn là sử dụng 'script.decompose()' chỉ xóa mà không trả về đối tượng thẻ. –

7

Để ngăn chặn các lỗi mã hóa ở cuối ...

import urllib 
from bs4 import BeautifulSoup 

url = url 
html = urllib.urlopen(url).read() 
soup = BeautifulSoup(html) 

# kill all script and style elements 
for script in soup(["script", "style"]): 
    script.extract() # rip it out 

# get text 
text = soup.get_text() 

# break into lines and remove leading and trailing space on each 
lines = (line.strip() for line in text.splitlines()) 
# break multi-headlines into a line each 
chunks = (phrase.strip() for line in lines for phrase in line.split(" ")) 
# drop blank lines 
text = '\n'.join(chunk for chunk in chunks if chunk) 

print(text.encode('utf-8')) 
Các vấn đề liên quan