详解用python的beautifulsoup分析html方法

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开头的tagfind(lambda) # 搜索函数返回结果为true的tag, 如:find(lambda name: if len(name) == 1) 搜索长度为1的tagfind(true) # 搜索所有tag

2) 搜索文字(text)

3) recursive, limit:

from bs4 import beautifulsoup
import re
doc = [‘page title’,

this is paragraph one.’,

this is paragraph two.’,
”]
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)

Posted in 未分类

发表评论