Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7193209
  • 博文数量: 510
  • 博客积分: 12019
  • 博客等级: 上将
  • 技术积分: 6836
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-01 16:46
文章分类

全部博文(510)

文章存档

2022年(2)

2021年(6)

2020年(59)

2019年(4)

2018年(10)

2017年(5)

2016年(2)

2015年(4)

2014年(4)

2013年(16)

2012年(47)

2011年(65)

2010年(46)

2009年(34)

2008年(52)

2007年(52)

2006年(80)

2005年(22)

分类: Python/Ruby

2019-11-21 15:14:26

#coding:utf-8  
import urllib
import re
from bs4 import BeautifulSoup
from distutils.filelist import findall


page = urllib.urlopen('')   
contents = page.read()   
soup = BeautifulSoup(contents,"html.parser")  
tag =soup.find('div', class_='wrap')    
tag3 =tag.find_all('li')    
for v in tag3:
        print v.text.encode("utf8")


#下面是过滤操作
 c = get_http(url)
    c.encoding = "gb2312" #网页若是gb2312
    soup = BeautifulSoup(c.text, 'lxml',from_encoding='utf-8')
     
    #去掉注释
    comments  = soup.findAll(text=lambda text: isinstance(text, Comment))
    [s.extract() for s in comments ]


    sff = soup.find('div',id="contentText")
    #去掉内部script
    [s.extract() for s in sff('script')]
    
       #去掉内部div
    [s.extract() for s in sff('div')]

          
阅读(6235) | 评论(0) | 转发(0) |
0

上一篇:pandas技巧汇总

下一篇:python常见操作

给主人留下些什么吧!~~