【发布时间】:2013-06-19 06:38:39
【问题描述】:
环境:
靓汤4
Python 2.7.5
逻辑:
“find_all”<li> 实例在 <ul> 中,类为 my_class,例如:
<ul class='my_class'>
<li>thing one</li>
<li>thing two</li>
</ul>
澄清:只需获取<li> 标签之间的“文本”。
Python 代码:
(下面的find_all不正确,我只是把它放在上下文中)
from bs4 import BeautifulSoup, Comment
import re
# open original file
fo = open('file.php', 'r')
# convert to string
fo_string = fo.read()
# close original file
fo.close()
# create beautiful soup object from fo_string
bs_fo_string = BeautifulSoup(fo_string, "lxml")
# get rid of html comments
my_comments = bs_fo_string.findAll(text=lambda text:isinstance(text, Comment))
[my_comment.extract() for my_comment in my_comments]
my_li_list = bs_fo_string.find_all('ul', 'my_class')
print my_li_list
【问题讨论】:
标签: python python-2.7 beautifulsoup