python爬虫——BeautifulSoup详解(附加css选择器)

   2023-03-08 学习力0
核心提示: BeautifulSoup是一个灵活有方便的网页解系库,处理搞笑,支持多种解析器,利用他可以不编写正贼表达式即可方便实现网页信息的提取。  解析库:我们主要用lxml解析器  标签选择器:# coding=utf-8from bs4 import BeautifulSoup as bshtml = """htmlhead

 

BeautifulSoup是一个灵活有方便的网页解系库,处理搞笑,支持多种解析器,利用他可以不编写正贼表达式即可方便实现网页信息的提取。

 

 

解析库:

python爬虫——BeautifulSoup详解(附加css选择器)

我们主要用lxml解析器

 

 

标签选择器

# coding=utf-8
from bs4 import BeautifulSoup as bs

html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister"

soup = bs(html, 'lxml')
print(soup.title)
print(type(soup.title))
print(soup.head)
print(type(soup.head))
print(soup.p)
print(type(soup.p))

这里我们print了soup.title、head、p三个标签以及他们的类型,结果如下:

python爬虫——BeautifulSoup详解(附加css选择器)

他们的类型都是bs.elment.tag,类型,类就是标签类型,并且对于soup.p,是把第一个p标签输出,也就是说有多个相同的标签,只输出第一个

 

 

获取名称:

print(soup.title.name)

python爬虫——BeautifulSoup详解(附加css选择器)

输出结果就是title

 

 

获取属性: 

print(soup.title.attrs['name'])

python爬虫——BeautifulSoup详解(附加css选择器)

print(soup.p['name'])

python爬虫——BeautifulSoup详解(附加css选择器)

可以看到这两种方式都是相同的

 

 

获取内容:

print(soup.p.string)

python爬虫——BeautifulSoup详解(附加css选择器)

 

 

嵌套选择:

 python爬虫——BeautifulSoup详解(附加css选择器)

也就是说从body到p,是一个嵌套的关系,p也是说,通过 .head得到的tag还可以进一步 向下索取,通过.body.p得到p标签

 

 

子节点和子孙节点(children和contents):

contents:

# coding=utf-8
from bs4 import BeautifulSoup as bs

html = """
 <html>
 <head>
  <title>
   The Dormouse's story
  </title>
 </head>
 <body>
  <p class="story">
   Once upon a time there were three little sisters; and their names were
   <a class="sister" href="http://example.com/elsie" >
    Elsie
   </a>
   ,
   <a class="sister" href="http://example.com/lacie" >
    Lacie
   </a>
   and
   <a class="sister" href="http://example.com/tillie" >
    Tillie
   </a>
   ; and they lived at the bottom of a well.
  </p>
  <p class="story">
   ...
  </p>
 </body>
</html>
"""

soup = bs(html, 'lxml')
print(soup.body.contents)

python爬虫——BeautifulSoup详解(附加css选择器)

可以看到contents属性返回了一个列表,整个p中的内容。把所有的换行符 标签放进了列表

children:

当我们把contents换成children:

print(soup.body.children)

 

 

contents:

python爬虫——BeautifulSoup详解(附加css选择器)

它返回了一个迭代器,需要用for循环遍历使用

 

 

后代descendants:

print(soup.body.descendants)

python爬虫——BeautifulSoup详解(附加css选择器)

还是一个迭代器,并且descendants是获得所有子孙节点,也就是儿子的儿子也会获得

 

 

父节点parent:

返回父节点

python爬虫——BeautifulSoup详解(附加css选择器)

 

 

父节点parents:

python爬虫——BeautifulSoup详解(附加css选择器)

 

 

兄弟节点siblings:

python爬虫——BeautifulSoup详解(附加css选择器)

 

以上是标签选择器,是通过名字进行选择,但是在选择时候,往往会有很多名字相同的标签,所以我们不能完全用标签选择器进行选择,故引入标准选择器:

标准选择器:

 

 

find_all(name, attrs, recursive, text, **kwargs)

可根据标签名、属性、内容查找文档, 把所有符合条件的结果,并以列表的形式返回

name:

python爬虫——BeautifulSoup详解(附加css选择器)

可以看到findall返回的列表中的每一个项哦都是tag类型

由此我们可以嵌套for循环:

for p in soup.find_all('p'):
    print(p.find_all('a'))

attrs:

print(soup.find_all(attrs={'id': 'list-1'}))
print(soup.find_all(attrs={'name': 'elements'}))

attr需要传入一个字典

并且对于某一些属性,可以直接用参数传入:

print(soup.find_all(id='list-1'))
print(soup.find_all(class_='elements'))   #class 是python的一个关键词,所以我们用class_代替class

text:

根据文本的内容选择,而它的返回值仅仅是文本的列表而不是tag

python爬虫——BeautifulSoup详解(附加css选择器)

python爬虫——BeautifulSoup详解(附加css选择器)

 

 

find(name, attrs, recursive, text, **kwargs)

与find_all不同是  find返回单个元素,fan_all返回所有元素。 find查找一个不存在的元素返回None

 

find_parent()和find_parents():

find_parent()返回所有祖先节点,find_parent()返回直接父节点。

 

find_next_siblings() 和 find_next_sibling()

find_next_siblings() 返回后面所有兄弟节点 find_next_sibling()返回前面一个兄弟节点

 

find_all_next() 和find_next()

find_all_next()返回节点后所有符合条件的节点,find_next()返回第一个符合条件的节点 

 

find_all_previous()和find_previous()

find_all_previous()返回节点钱所有符合条件的节点,find_previous返回第一个符合条件的节点

 

 

 

css选择器

通过css()直接传入css选择器即可完成选择

标签(什么都不用加).属性(加点) #id(加井号) 

  1. import requests  
  2. from bs4 import BeautifulSoup as bs  
  3.   
  4. html = """ 
  5. <html> 
  6.     <head> 
  7.         <title>The Dormouse's story</title> 
  8.     </head> 
  9.     <body> 
  10.         <p class="title" name="dromouse"> 
  11.             <b>The Dormouse's story</b> 
  12.         </p> 
  13.         <p class="story"> 
  14.             Once upon a time there were three little sisters; and their names were 
  15.             <a class="mysis" href="http://example.com/elsie" > 
  16.                 <b>the first b tag<b> 
  17.                 Elsie 
  18.             </a>, 
  19.             <a class="mysis" href="http://example.com/lacie" > 
  20.                 Lacie 
  21.             </a>and 
  22.             <a class="mysis" href="http://example.com/tillie" > 
  23.                 Tillie 
  24.             </a>;and they lived at the bottom of a well. 
  25.         </p> 
  26.         <p class="story"> 
  27.             myStory 
  28.             <a>the end a tag</a> 
  29.         </p> 
  30.         <a>the p tag sibling</a> 
  31.     </body> 
  32. </html> 
  33. """  
  34. soup = bs(html, 'lxml')  
  35. print(soup.select('p'))  
  36. print(soup.select('p a'))  
  37. print(type(soup.select('p')[0]))  

输出结果1是一个包含所有p标签的列表 2是一个包含所有p标签下的a标签的列表,3是python爬虫——BeautifulSoup详解(附加css选择器),也就是说。css选择器生成的结果就是一个tag类型的列表

同时对于soup.select('a.mysis‘表示class属性为mysis的所有标签。也即没有空格的表示有某一个属性的或者id的标签。 有空格代表是同等的

又因为select返回的是tag类型的列表,所以我们可以继续使用上面的方法获得属性即:、

  1. for a in soup.select('p a'):    
  2.     #方法一    
  3.     print(a['href'])    
  4.     #方法二    
  5.     print(a.attrs['href'])    

 以下罗列出一些css选择器的方法:(以下内容转自https://www.cnblogs.com/kongzhagen/p/6472746.html)


1、通过标签选择

  1. # 选择所有title标签  
  2. soup.select("title")  
  3. # 选择所有p标签中的第三个标签  
  4. soup.select("p:nth-of-type(3)") 相当于soup.select(p)[2]  
  5. # 选择body标签下的所有a标签  
  6. soup.select("body a")  
  7. # 选择body标签下的直接a子标签  
  8. soup.select("body > a")  
  9. # 选择id=link1后的所有兄弟节点标签  
  10. soup.select("#link1 ~ .mysis")  
  11. # 选择id=link1后的下一个兄弟节点标签  
  12. soup.select("#link1 + .mysis")  

2、通过类名查找

  1. # 选择a标签,其类属性为mysis的标签  
  2. soup.select("a.mysis")  

3、通过id查找

  1. # 选择a标签,其id属性为link1的标签  
  2. soup.select("a#link1")  

4、通过【属性】查找,当然也适用于class

  1. # 选择a标签,其属性中存在myname的所有标签  
  2. soup.select("a[myname]")  
  3. # 选择a标签,其属性href=http://example.com/lacie的所有标签  
  4. soup.select("a[href='http://example.com/lacie']")  
  5. # 选择a标签,其href属性以http开头  
  6. soup.select('a[href^="http"]')  
  7. # 选择a标签,其href属性以lacie结尾  
  8. soup.select('a[href$="lacie"]')  
  9. # 选择a标签,其href属性包含.com  
  10. soup.select('a[href*=".com"]')  
  11. # 从html中排除某标签,此时soup中不再有script标签  
  12. [s.extract() for s in soup('script')]  
  13. # 如果想排除多个呢  
  14. [s.extract() for s in soup(['script','fram']  

5、获取文本及属性

  1. html_doc = """<html> 
  2.     <head> 
  3.         <title>The Dormouse's story</title> 
  4.     </head> 
  5. <body> 
  6.     <p class="title"><b>The Dormouse's story</b></p> 
  7.     <p class="story">Once upon a time there were three little sisters; and their names were 
  8.         <a href="http://example.com/elsie" class="sister" >Elsie</a>, 
  9.         <a href="http://example.com/lacie" class="sister" >Lacie</a> and 
  10.         <a href="http://example.com/tillie" class="sister" >Tillie</a>; 
  11.     </p> 
  12.         and they lived at the bottom of a well. 
  13.     <p class="story">...</p> 
  14. </body> 
  15. """  
  16. from bs4 import BeautifulSoup  
  17. ''''' 
  18. 以列表的形式返回 
  19. '''  
  20. soup = BeautifulSoup(html_doc, 'html.parser')  
  21. s = soup.select('p.story')  
  22. s[0].get_text()  # p节点及子孙节点的文本内容  
  23. s[0].get_text("|")  # 指定文本内容的分隔符  
  24. s[0].get_text("|", strip=True)  # 去除文本内容前后的空白  
  25. print(s[0].get("class"))  # p节点的class属性值列表(除class外都是返回字符串)  

6、UnicodeDammit.detwingle() 方法只能解码包含在UTF-8编码中的Windows-1252编码内容

  1. new_doc = UnicodeDammit.detwingle(doc)  
  2. print(new_doc.decode("utf8"))  
  3. # ☃☃☃“I like snowmen!”  

在创建 BeautifulSoup 或 UnicodeDammit 对象前一定要先对文档调用 UnicodeDammit.detwingle() 确保文档的编码方式正确.如果尝试去解析一段包含Windows-1252编码的UTF-8文档,就会得到一堆乱码,比如: ☃☃☃“I like snowmen!”.

7 、其他

  1. html_doc = """<html> 
  2.     <head> 
  3.         <title>The Dormouse's story</title> 
  4.     </head> 
  5. <body> 
  6.     <p class="title"><b>The Dormouse's story</b></p> 
  7.     <p class="story">Once upon a time there were three little sisters; and their names were 
  8.         <a href="http://example.com/elsie" class="sister" >Elsie</a>, 
  9.         <a href="http://example.com/lacie" class="sister" >Lacie</a> and 
  10.         <a href="http://example.com/tillie" class="sister" >Tillie</a>; 
  11.     </p> 
  12.         and they lived at the bottom of a well. 
  13.     <p class="story">...</p> 
  14. </body> 
  15. """  
  16. from bs4 import BeautifulSoup  
  17. ''''' 
  18. 以列表的形式返回 
  19. '''  
  20. soup = BeautifulSoup(html_doc, 'html.parser')  
  21. soup.select('title')  # title标签  
  22. soup.select("p:nth-of-type(3)")  # 第三个p节点  
  23. soup.select('body a')  # body下的所有子孙a节点  
  24. soup.select('p > a')  # 所有p节点下的所有a直接节点  
  25. soup.select('p > #link1')  # 所有p节点下的id=link1的直接子节点  
  26. soup.select('#link1 ~ .sister')  # id为link1的节点后面class=sister的所有兄弟节点  
  27. soup.select('#link1 + .sister')  # id为link1的节点后面class=sister的第一个兄弟节点  
  28. soup.select('.sister')  # class=sister的所有节点  
  29. soup.select('[class="sister"]')  # class=sister的所有节点  
  30. soup.select("#link1")  # id=link1的节点  
  31. soup.select("a#link1")  # a节点,且id=link1的节点  
  32. soup.select('a[href]')  # 所有的a节点,有href属性  
  33. soup.select('a[href="http://example.com/elsie"]')  # 指定href属性值的所有a节点  
  34. soup.select('a[href^="http://example.com/"]')  # href属性以指定值开头的所有a节点  
  35. soup.select('a[href$="tillie"]')  # href属性以指定值结尾的所有a节点  
  36. soup.select('a[href*=".com/el"]')  # 支持正则匹配  

 

 

 
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • css实现弹出框 css弹出菜单
    弹出框也是前端里面经常使用的一个应用场景了,最开始想到的是用js实现这个效果,看到codepen上面有用css实现的。其实就是用到了css里面的一个:target选择器+visibility属性。URL 带有后面跟有锚名称 #,指向文档内某个具体的元素。这个被链接的元素就是目标
    03-08
  • jfinal框架页面找不到相关css,js文件404
    在JFinalConfig中添加配置: @Overridepublic void configHandler(Handlers handlers) {handlers.add(new ContextPathHandler());}页面中添加:base href="${CONTEXT_PATH}/"/之前页面找不到资源,把tomcat工程路径去掉了(path=""),这样暂时解决了问题推荐
    03-08
  • 纯CSS3实现的一些酷炫效果 css实现炫酷背景
    纯CSS3实现的一些酷炫效果 css实现炫酷背景
      之前在网上看到一些用纯CSS3实现的酷炫效果,以为实现起来比较困难,于是想看看具体是怎么实现的。一、笑脸猫动画实现效果如下:这个实现起来确实比较麻烦,很多地方需要花时间,有耐心地调整。1.先看下页面结构:bodydiv class="container"!-- 脸 --div
    03-08
  • 移动端CSS底部固定和fixed定位在ios上的bug
    fixed定位在ios上的bugcss三栏布局假设我们页面的 HTML 结构是这样: div class="wrapper"div class="content"!-- 页面主体内容区域 --/divdiv class="footer"!-- 需要做到 Sticky Footer 效果的页脚 --/div/div方法1.:absolute通过绝对定位处理应该是常见的
    03-08
  • css实现图片翻转 css使图片旋转
    !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"html xmlns="http://www.w3.org/1999/xhtml"headmeta http-equiv="Content-Type" content="text/h
    03-08
  • css3 渐变gradient
    css3 渐变gradient
           CSS3引入了背景渐变、background-origin、background-clip、background-size、遮罩等多个属性。这里将记录我从网上和书上学习的笔记和心得体会。   首先是渐变,我这里讲述的渐变不仅仅是背景色的渐变还将包括透明度渐变。以前做这种背景色渐
    03-08
  • CSS3中的px,em,rem,vh,vw辨析 css3 vw
    1、px:像素,精确显示2、em:继承父类字体的大小,相当于“倍”,如:浏览器默认字体大小为16px=1em,始终按照div继承来的字体大小显示,进场用于移动端          em换算工具:http://www.runoob.com/tags/ref-pxtoemconversion.html3、rem:与em类似,
    03-08
  • gulp自动化构建工具--自动编译less为css--学习
     1.安装      命令:npm install gulp-less 或者 cnpm install gulp-less2.编写文件 //获取gulpvar gulp = require('gulp')//获取gulp-less模块var less = require("gulp-less")//编译less//在命令行输入gulp less启动此任务gulp.task('less',function(){
    03-08
  • 关于动画Animate.css的使用方法
        首先贴个官网:https://daneden.github.io/animate.css/  1、引入animate css文件 1 head2 link rel="stylesheet" href="animate.min.css"3 /head   2、给指定的元素加上指定的动画样式名div class="animated bounceOutLeft"/div    这里包
    03-08
  • selenium Firefox禁用js,css.falsh,缓存,设置
    1 profile = FirefoxProfile() 2 #请求头 3 #profile.set_preference("general.useragent.override", util.http_agent_insert(xinhuaUtil.agent_path)) 4 # 激活手动代理配置(对应着在 profile(配置文件)中设置首选项) 5 profile.set_preference("network
    03-08
点击排行