码迷,mamicode.com
首页 > 其他好文 > 详细

beautifulSoup(1)

时间:2016-12-03 23:24:16      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:enter   nbsp   first   nis   pre   graph   pil   doc   ret   

import re
from bs4 import BeautifulSoup
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())
title=soup.html.head.title
print(title)
print(title.string)
print(len(soup(‘p‘)))
print(soup.findAll(‘p‘,align=‘center‘))
print(soup.find(‘p‘,align=‘center‘))
print(soup(‘p‘,align=‘center‘)[0][‘id‘])
print(soup.find(‘p‘,align=re.compile(‘^b.*‘))[‘id‘])
print(soup.find(‘p‘).b.string)
print(soup(‘p‘)[1].b.string)
-----------------------------------------------------------------------------------

<html>
 <head>
  <title>
   Page title
  </title>
 </head>
 <body>
  <p align="center" id="firstpara">
   This is paragraph
   <b>
    one
   </b>
   .
   <p align="blah" id="secondpara">
    This is paragraph
    <b>
     two
    </b>
    .
   </p>
  </p>
 </body>
</html>
<title>Page title</title>
Page title
2
[<p align="center" id="firstpara">This is paragraph <b>one</b>.<p align="blah" id="secondpara">This is paragraph <b>two</b>.</p></p>]
<p align="center" id="firstpara">This is paragraph <b>one</b>.<p align="blah" id="secondpara">This is paragraph <b>two</b>.</p></p>
firstpara
secondpara
one
two
[Finished in 0.5s]

beautifulSoup(1)

标签:enter   nbsp   first   nis   pre   graph   pil   doc   ret   

原文地址:http://www.cnblogs.com/lfqcode/p/6129556.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!