python beautifulsoup简用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a735311619/article/details/78026225

提示:本文以百度首页为例子写作,本例所获取的百度源码在文末

find_all方法的使用

例1:查找所有的标签

import requests
from bs4 import BeautifulSoup
r = requests.get("http://www.baidu.com")
r.encoding = r.apparent_encoding
html = r.text
soup = BeautifulSoup(html,"html.parser")
all = soup.find_all("a")
for tag in all:
    print(tag)

返回结果

<a class="mnav" href="http://news.baidu.com" name="tj_trnews">新闻</a>
<a class="mnav" href="http://www.hao123.com" name="tj_trhao123">hao123</a>
<a class="mnav" href="http://map.baidu.com" name="tj_trmap">地图</a>
<a class="mnav" href="http://v.baidu.com" name="tj_trvideo">视频</a>
<a class="mnav" href="http://tieba.baidu.com" name="tj_trtieba">贴吧</a>
<a class="lb" href="http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1" name="tj_login">登录</a>
<a class="bri" href="//www.baidu.com/more/" name="tj_briicon" style="display: block;">更多产品</a>
<a href="http://home.baidu.com">关于百度</a>
<a href="http://ir.baidu.com">About Baidu</a>
<a href="http://www.baidu.com/duty/">使用百度前必读</a>
<a class="cp-feedback" href="http://jianyi.baidu.com/">意见反馈</a>

例2:查找所有属性性值包含mnav的a标签

import requests
from bs4 import BeautifulSoup
r = requests.get("http://www.baidu.com")
r.encoding = r.apparent_encoding
html = r.text
soup = BeautifulSoup(html,"html.parser")
all = soup.find_all("a","mnav")
for tag in all:
    print(tag)

返回结果

<a class="mnav" href="http://news.baidu.com" name="tj_trnews">新闻</a>
<a class="mnav" href="http://www.hao123.com" name="tj_trhao123">hao123</a>
<a class="mnav" href="http://map.baidu.com" name="tj_trmap">地图</a>
<a class="mnav" href="http://v.baidu.com" name="tj_trvideo">视频</a>
<a class="mnav" href="http://tieba.baidu.com" name="tj_trtieba">贴吧</a>

例3:获取所有标签的名称

import requests
from bs4 import BeautifulSoup
r = requests.get("http://www.baidu.com")
r.encoding = r.apparent_encoding
html = r.text
soup = BeautifulSoup(html,"html.parser")
all = soup.find_all(True)#设置为True !!!
for tag in all:
    print(tag.name) #获取标签的名称

返回结果

html
head
meta
meta
meta
link
title
body
div
div
div
div
div
div
img
form
input
input
input
input
input
input
span
input
span
input
div
a
a
a
a
a
noscript
a
script
a
div
div
p
a
a
p
a
a
img

例4:获取所有id=head的标签

import requests
from bs4 import BeautifulSoup
r = requests.get("http://www.baidu.com")
r.encoding = r.apparent_encoding
html = r.text
soup = BeautifulSoup(html,"html.parser")
all = soup.find_all(id="head")
for tag in all:
    print(tag)

返回结果

<div id="head"> <div class="head_wrapper"> <div class="s_form"> <div class="s_form_wrapper"> <div id="lg"> <img height="129" hidefocus="true" src="//www.baidu.com/img/bd_logo1.png" width="270"/> </div> <form action="//www.baidu.com/s" class="fm" id="form" name="f"> <input name="bdorz_come" type="hidden" value="1"/> <input name="ie" type="hidden" value="utf-8"/> <input name="f" type="hidden" value="8"/> <input name="rsv_bp" type="hidden" value="1"/> <input name="rsv_idx" type="hidden" value="1"/> <input name="tn" type="hidden" value="baidu"/><span class="bg s_ipt_wr"><input autocomplete="off" autofocus="" class="s_ipt" id="kw" maxlength="255" name="wd" value=""/></span><span class="bg s_btn_wr"><input class="bg s_btn" id="su" type="submit" value="百度一下"/></span> </form> </div> </div> <div id="u1"> <a class="mnav" href="http://news.baidu.com" name="tj_trnews">新闻</a> <a class="mnav" href="http://www.hao123.com" name="tj_trhao123">hao123</a> <a class="mnav" href="http://map.baidu.com" name="tj_trmap">地图</a> <a class="mnav" href="http://v.baidu.com" name="tj_trvideo">视频</a> <a class="mnav" href="http://tieba.baidu.com" name="tj_trtieba">贴吧</a> <noscript> <a class="lb" href="http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1" name="tj_login">登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a class="bri" href="//www.baidu.com/more/" name="tj_briicon" style="display: block;">更多产品</a> </div> </div> </div>

例5:结合正则表达式,搜索包含’百度’的字符串

注意:string参数表明仅搜索标签内的内容,并不搜索标签的属性。

import requests
import re
from bs4 import BeautifulSoup
r = requests.get("http://www.baidu.com")
r.encoding = r.apparent_encoding #一定要注意转码,要不然编码不对可能就查找不到了。
html = r.text
soup = BeautifulSoup(html,"html.parser")
all = soup.find_all(string=re.compile("百度"))
for tag in all:
    print(tag)

返回结果

百度一下,你就知道
关于百度
使用百度前必读

例6:结合正则表达式,搜索id开头是’f’的标签

import requests
import re
from bs4 import BeautifulSoup
r = requests.get("http://www.baidu.com")
r.encoding = r.apparent_encoding #一定要注意转码,要不然编码不对可能就查找不到了。
html = r.text
soup = BeautifulSoup(html,"html.parser")
all = soup.find_all(id=re.compile("f"))#id中包含f不一定搜索得到,必须是开头是f的才能搜索到。
for tag in all:
    print(tag)

返回结果

<form action="//www.baidu.com/s" class="fm" id="form" name="f"> <input name="bdorz_come" type="hidden" value="1"/> <input name="ie" type="hidden" value="utf-8"/> <input name="f" type="hidden" value="8"/> <input name="rsv_bp" type="hidden" value="1"/> <input name="rsv_idx" type="hidden" value="1"/> <input name="tn" type="hidden" value="baidu"/><span class="bg s_ipt_wr"><input autocomplete="off" autofocus="" class="s_ipt" id="kw" maxlength="255" name="wd" value=""/></span><span class="bg s_btn_wr"><input class="bg s_btn" id="su" type="submit" value="百度一下"/></span> </form>
<div id="ftCon"> <div id="ftConw"> <p id="lh"> <a href="http://home.baidu.com">关于百度</a> <a href="http://ir.baidu.com">About Baidu</a> </p> <p id="cp">©2017 Baidu <a href="http://www.baidu.com/duty/">使用百度前必读</a>  <a class="cp-feedback" href="http://jianyi.baidu.com/">意见反馈</a> 京ICP证030173号  <img src="//www.baidu.com/img/gs.gif"/> </p> </div> </div>
<div id="ftConw"> <p id="lh"> <a href="http://home.baidu.com">关于百度</a> <a href="http://ir.baidu.com">About Baidu</a> </p> <p id="cp">©2017 Baidu <a href="http://www.baidu.com/duty/">使用百度前必读</a>  <a class="cp-feedback" href="http://jianyi.baidu.com/">意见反馈</a> 京ICP证030173号  <img src="//www.baidu.com/img/gs.gif"/> </p> </div>

例7:仅搜索同一层的节点,不搜索子节点

import requests
import re
from bs4 import BeautifulSoup
r = requests.get("http://www.baidu.com")
r.encoding = r.apparent_encoding #一定要注意转码,要不然编码不对可能就查找不到了。
html = r.text
soup = BeautifulSoup(html,"html.parser")
soup = soup.body #把soup指到body下
all = soup.find_all(True,recursive=False)#recursive这个参数用来表明是否搜索所有的子孙节点,默认为True
for tag in all:
    print(tag.name)

返回结果

div

百度源码

<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

参考文档

北京理工大学嵩天老师的爬虫与信息提取课程随堂ppt

阅读更多 登录后自动展开
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页