小编Joh*_*000的帖子

用于从格式错误的html页面中提取文本的Python策略

我正在尝试从任意html页面中提取文本.有些页面(我无法控制)有错误的html或脚本,这使得这很困难.此外,我在一个共享的托管环境,所以我可以安装任何python库,但我不能只在服务器上安装我想要的东西.

pyparsing和html2text.py似乎也不适用于格式错误的html页面.

示例URL是http://apnews.myway.com/article/20091015/D9BB7CGG1.html

我目前的实施大致如下:

# Try using BeautifulSoup 3.0.7a
soup = BeautifulSoup.BeautifulSoup(s) 
comments = soup.findAll(text=lambda text:isinstance(text,Comment))
[comment.extract() for comment in comments]
c=soup.findAll('script')
for i in c:
    i.extract()    
body = bsoup.body(text=True)
text = ''.join(body) 
# if BeautifulSoup  can't handle it, 
# alter html by trying to find 1st instance of  "<body" and replace everything prior to that, with "<html><head></head>"
# try beautifulsoup again with new html 
Run Code Online (Sandbox Code Playgroud)

如果beautifulsoup仍然不起作用,那么我采用一种启发式方法来查看第一个字符,最后一个字符(看看它们是否看起来像是一个代码行#<;然后取一行代码然后检查是否有令牌是英文单词或数字.如果很少的标记是单词或数字,那么我猜这行是代码.

我可以使用机器学习来检查每一行,但这看起来有点贵,我可能需要训练它(因为我不太了解无监督的学习机器),当然也可以写它.

任何建议,工具和策略都是最受欢迎的.此外,我意识到后一部分相当混乱,因为如果我得到一行确定包含代码,我现在扔掉整行,即使行中有少量实际的英文文本.

html python text html-content-extraction

5
推荐指数
1
解决办法
1917
查看次数

试图安装R包:'ggplot2'不可用

如果你让我知道我应该运行什么,我可以发布更多数据,但这里是错误和版本.

> install.packages("ggplot2", dependencies = TRUE)
Warning in install.packages :
  package ‘ggplot2’ is not available
Installing package(s) into ‘/home/johnny4000/R/x86_64-pc-linux-gnu-library/2.12’
(as ‘lib’ is unspecified)
Warning in install.packages :
  package ‘ggplot2’ is not available
Run Code Online (Sandbox Code Playgroud)

和版本

> version               
platform       x86_64-pc-linux-gnu          
arch           x86_64                       
os             linux-gnu                    
system         x86_64, linux-gnu            
status                                      
major          2                            
minor          12.1                         
year           2010                         
month          12                           
day            16                           
svn rev        53855                        
language       R                            
version.string R version 2.12.1 (2010-12-16)
Run Code Online (Sandbox Code Playgroud)

r cran

1
推荐指数
1
解决办法
3706
查看次数

标签 统计

cran ×1

html ×1

html-content-extraction ×1

python ×1

r ×1

text ×1