从HTML页面中删除样板内容

时间:2015-06-13 09:22:45

标签: python request response htmlcleaner

我想使用https://github.com/miso-belica/jusText中的jusText实现来从html页面中获取干净的内容。基本上它的工作原理如下:

import requests
import justext

response = requests.get("http://planet.python.org/")
paragraphs = justext.justext(response.content, justext.get_stoplist("English"))
for paragraph in paragraphs:
  if not paragraph.is_boilerplate:
      print paragraph.text

我已经使用此工具下载了我想要解析的页面(其中一些已不再在线提供),并且我从中提取了html内容。由于jusText似乎只处理请求的输出(这是一个响应类型对象),我想知道是否有任何自定义方法来设置响应对象的内容以包含我想要解析的html文本。

1 个答案:

答案 0 :(得分:1)

response.content属于<type 'str'>

>>> from requests import get
>>> r = get("http://www.google.com/")
>>> type(r.content)
<type 'str'>

所以请致电:

justext.justext(my_html_string, justext.get_stoplist("English"))