python beautifulsoup简用

news/2024/7/19 12:32:00 标签: python, 爬虫

    • find_all方法的使用
      • 例1查找所有的标签
      • 例2查找所有属性性值包含mnav的a标签
      • 例3获取所有标签的名称
      • 例4获取所有idhead的标签
      • 例5结合正则表达式搜索包含百度的字符串
      • 例6结合正则表达式搜索id开头是f的标签
      • 例7仅搜索同一层的节点不搜索子节点
    • 百度源码
    • 参考文档

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

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:获取所有标签的名称

python">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参数表明仅搜索标签内的内容,并不搜索标签的属性。

python">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’的标签

python">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:仅搜索同一层的节点,不搜索子节点

python">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


http://www.niftyadmin.cn/n/1666381.html

相关文章

JavaScript中的this关键字使用的四种调用模式

this关键字本意&#xff1a;这个、这里的意思.在JavaScript中是指每一个方法或函数都会有一个this对象&#xff0c;this对象是方法&#xff08;或函数&#xff09;在执行时的那个环境&#xff0c;也可以说是这个函数在那个作用域下运行的一共有四种调用模式&#xff1a;方法调用…

区分 %d, %ld, %lld, %lf, %f 等

区分 %d, %ld, %lld, %lf, %f 等 %d&#xff1a;用于 int %ld&#xff1a;用于 long %lld&#xff1a;用于 longlong 输入时&#xff1a; float 输入用 %f double 输入用 %lf 输出时&#xff1a; float, double都用 %f 输出就行了(不太清楚是什么环境下&#xff0c;double…

[原][译][lua][luabridge]一个简单的luabridge与c++例子结合例子

参考&#xff1a;https://eliasdaler.wordpress.com/tag/luabridge/ https://eliasdaler.wordpress.com/2015/08/10/using-lua-and-cpp-in-practice/ 1. ECS和基本原理介绍 本译文主要说明以下几点&#xff1a; 实体的创建和其他基本的东西&#xff08;你正在阅读这篇文章了&am…

使用Python爬取中国大学排名,并格式化对其输出内容

首先&#xff0c;我们需要注意几点 1.可以使用isinstance语句配合bs4库中的bs4.element.Tag判断获取到的对象是不是标签对象. 2.输出内容并且要求他用空白补齐时&#xff0c;系统默认用的是英文空白符&#xff0c;英文空白符和中文空白符(chr(12288)获取)宽度不一样。 #代码…

Requested Clipboard operation did not succeed的解决办法

在使用Clipboard.SetText(strTar) 将文本复制进剪贴板的时候&#xff0c;有时候会导致Requested Clipboard operation did not succeed的异常&#xff0c;但有时候又不会。 原因&#xff1a; 剪切板被所有进程共享&#xff0c;但是不能同时被两个进程修改。所以&#xff0c;如果…

C语言计算程序段执行时间

直接看代码套模板&#xff1a; #include <stdio.h> #include <time.h> #include <stdlib.h>int main() {clock_t begin, end;double time_cost;// 开始记录begin clock();/*这里输入待测试程序段*/// 结束记录end clock();time_cost (double)(end - beg…

【004】LeetCode769最多能完成排序的块

题目769. 最多能完成排序的块 4.1 官方解析 点击这里 方法一&#xff1a; 暴力 思路和算法 首先找到从左块开始最小块的大小。如果前 k 个元素为 [0, 1, …, k-1]&#xff0c;可以直接把他们分为一个块。当我们需要检查 [0, 1, …, n-1] 中前 k1 个元素是不是 [0, 1, …, k…

【005】LeetCode 232用栈实现队列

题目232. 用栈实现队列 5.1 官方解析 点击这里 方法一&#xff1a;双栈 思路 将一个栈当作输入栈&#xff0c;用于压入 push 传入的数据&#xff1b;另一个栈当作输出栈&#xff0c;用于 pop 和 peek 操作。 每次 pop 或 peek 时&#xff0c;若输出栈为空则将输入栈的全部…