Extracting text from HTML file using Python

后端 未结 30 2611
一生所求
一生所求 2020-11-22 04:05

I\'d like to extract the text from an HTML file using Python. I want essentially the same output I would get if I copied the text from a browser and pasted it into notepad.

30条回答
  •  悲哀的现实
    2020-11-22 04:32

    NOTE: NTLK no longer supports clean_html function

    Original answer below, and an alternative in the comments sections.


    Use NLTK

    I wasted my 4-5 hours fixing the issues with html2text. Luckily i could encounter NLTK.
    It works magically.

    import nltk   
    from urllib import urlopen
    
    url = "http://news.bbc.co.uk/2/hi/health/2284783.stm"    
    html = urlopen(url).read()    
    raw = nltk.clean_html(html)  
    print(raw)
    

提交回复
热议问题