当前位置:   article > 正文

python去除script标签及里面的内容_python html 清除script内容

python html 清除script内容

  1. soup = BeautifulSoup(content.content,'lxml')
  2. text = soup.find('div',{'class':'content'}).get_text().strip()
  3. print text
var ent_common_pic_1 = { "data": { "item": [ { "title": "《快乐大本营》杨紫", "img_url": "http://n.sinaimg.cn/ent/transform/20170527/Le4r-fyfrfvv4614357.jpg", "thumb_url": "http://n.sinaimg.cn/ent/transform/20170527/Le4r-fyfrfvv4614357_h60.jpg"......('entSdPic_1', ent_common_pic_1); entSlide_1.init(); } });   新浪娱乐讯 本周六晚,湖南卫视《快乐大本营》二十周年特别篇持续播出。此次,杨紫[微博]将以二十周年特.


添加一下几行代码,就可以删除掉了:

  1. soup = BeautifulSoup(content.content,'lxml')
  2. for script in soup(["script", "style"]):
  3. script.extract()
  4. text = soup.find('div',{'class':'content'}).get_text().strip()
  5. lines = (line.strip() for line in text.splitlines())
  6. chunks = (phrase.strip() for line in lines
  7. for phrase in line.split(" "))
  8. text = '\n'.join(chunk for chunk in chunks if chunk)
  9. print text

新浪娱乐讯 本周六晚,湖南卫视《快乐大本营》二十周年特别篇持续播出。此次,杨紫[微博]将以


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/350058
推荐阅读
相关标签
  

闽ICP备14008679号