标签:shell
http://www.cnblogs.com/twinsclover/archive/2012/04/26/2471704.html 用python的BeautifulSoup分析html
http://www.crummy.com/software/BeautifulSoup/bs3/documentation.zh.html Beautiful Soup 中文文档
1) 搜索tag:
find(tagname) # 直接搜索名为tagname的tag 如:find(‘head‘)
find(list) # 搜索在list中的tag,如: find([‘head‘, ‘body‘])
find(dict) # 搜索在dict中的tag,如:find({‘head‘:True, ‘body‘:True})
find(re.compile(‘‘)) # 搜索符合正则的tag, 如:find(re.compile(‘^p‘)) 搜索以p开头的tag
find(lambda) # 搜索函数返回结果为true的tag, 如:find(lambda name: if len(name) == 1) 搜索长度为1的tag
find(True) # 搜索所有tag
2) 搜索文字(text)
3) recursive, limit:
from bs4 import BeautifulSoup
import re
doc = [‘<html><head><title>Page title</title></head>‘,
‘<body><p id="firstpara" align="center">This is paragraph <b>one</b>.‘,
‘<p id="secondpara" align="blah">This is paragraph <b>two</b>.‘,
‘</html>‘]
soup = BeautifulSoup(‘‘.join(doc))
print soup.prettify()+"\n"
print soup.findAll(‘b‘)
print soup.findAll(text=re.compile("paragraph"))
print soup.findAll(text=True)
print soup.findAll(text=lambda(x):len(x)<12)
a = soup.findAll(re.compile(‘^b‘))
print [tag.name for tag in a]
print [tag.name for tag in soup.html.findAll()]
print [tag.name for tag in soup.html.findAll(recursive=False)]
print soup.findAll(‘p‘,limit=1)用python的BeautifulSoup分析html,布布扣,bubuko.com
标签:shell
原文地址:http://dragonball.blog.51cto.com/1459915/1436515