python中BeautifulSoup模块

BeautifulSoup模块是干嘛的?

答:通过html标签去快速匹配标签中的内容。效率相对比正则会好的多。效率跟xpath模块应该差不多。

一:解析器:

  • BeautifulSoup(html,"html.parser")
  • BeautifulSoup(html,'lxml')
  • BeautifulSoup(html,'xml')
  • BeautifulSoup(html,'html5lib')

 假设要匹配a标签里的href属性:

1 html = "<a href='http://baidu.com/'>this is baidu.com</a>"
2 bs = BeautifulSoup(html,"lxml")
3 all_href = bs.find_all('a')
4 for i in all_href:
5     print i['href']

 

 

 1 #!usr/bin/env python
 2 #encding:utf-8
 3 #by i3ekr
 4 
 5 import requests
 6 from bs4 import BeautifulSoup
 7 
 8 html = """
 9 <!DOCTYPE html>
10 <html>
11 <head>
12     <title>title test demo</title>
13 </head>
14 <body>
15     <h1>this is h1</h1>
16     <h1>this is h1 two</h1>
17     <h1>this is h1 stree</h1>
18     <a href="http://baidu.com">this is a href.</a>
19 </body>
20 </html>
21 """
22 bs = BeautifulSoup(html, "lxml")
23 print bs.find_all('h1')

 

转载于:https://www.cnblogs.com/nul1/p/8947034.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值