【问题标题】:What is the best way to extract the content of an HTML file into a String? (in Python) [duplicate]将 HTML 文件的内容提取到字符串中的最佳方法是什么? (在 Python 中)[重复]
【发布时间】:2021-08-20 01:38:03
【问题描述】:

如何将您在网页上看到的内容/内容提取为字符串 例如转这个:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>myWebpage</title>
</head>
<body>
    <p>this</p>
    <p>is</p>
    <p>an</p>
    <p>example</p>
</body>
</html>

变成这样的字符串:

this is an example

【问题讨论】:

标签: python html python-3.x type-conversion


【解决方案1】:

您可以使用 selenium,在此处找到文档:https://pypi.org/project/selenium/

【讨论】:

    【解决方案2】:

    这个程序做你想做的事:https://github.com/Alir3z4/html2text

    你也可以试试:

    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)
    

    这例如从这个网页中提取文本。

    【讨论】:

      猜你喜欢
      • 2011-04-25
      • 2010-09-09
      • 2010-09-09
      • 2021-11-16
      • 2011-10-17
      • 1970-01-01
      • 1970-01-01
      • 2010-11-14
      • 2011-05-31
      相关资源
      最近更新 更多