书签 分享 收藏 举报 版权申诉 / 22
上传文档赚钱

类型Python爬虫程序设计KC23.pptx

  • 上传人(卖家):三亚风情
  • 文档编号:3371920
  • 上传时间:2022-08-24
  • 格式:PPTX
  • 页数:22
  • 大小:76.90KB
  • 【下载声明】
    1. 本站全部试题类文档,若标题没写含答案,则无答案;标题注明含答案的文档,主观题也可能无答案。请谨慎下单,一旦售出,不予退换。
    2. 本站全部PPT文档均不含视频和音频,PPT中出现的音频或视频标识(或文字)仅表示流程,实际无音频或视频文件。请谨慎下单,一旦售出,不予退换。
    3. 本页资料《Python爬虫程序设计KC23.pptx》由用户(三亚风情)主动上传,其收益全归该用户。163文库仅提供信息存储空间,仅对该用户上传内容的表现方式做保护处理,对上传内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知163文库(点击联系客服),我们立即给予删除!
    4. 请根据预览情况,自愿下载本文。本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
    5. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007及以上版本和PDF阅读器,压缩文件请下载最新的WinRAR软件解压。
    配套讲稿:

    如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。

    特殊限制:

    部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。

    关 键  词:
    Python 爬虫 程序设计 KC23
    资源描述:

    1、2.3.1 BeautifulSoup查找查找HTML元素元素2.3.1 BeautifulSoup查找查找HTML元素元素查找文档的元素是我们爬取网页信息的重要手段,BeautifulSoup提供了一系列的查找元素的方法,其中功能强大的find_all函数就是其中常用的一个方法。find_all函数的原型如下:find_all(self,name=None,attrs=,recursive=True,text=None,limit=None,*kwargs)self表明它是一个类成员函数;name是要查找的tag元素名称,默认是None,如果不提供,就是查找所有的元素;attrs是元素的属性

    2、,它是一个字典,默认是空,如果提供就是查找有这个指定属性的元素;recursive指定查找是否在元素节点的子树下面全范围进行,默认是True;后面的text、limit、kwargs参数比较复杂,将在后面用到时介绍;find_all函数返回查找到的所有指定的元素的列表,每个元素是一个bs4.element.Tag对象。find_all函数是查找所有满足要求的元素节点,如果我们只查找一个元素节点,那么可以使用find函数,它的原型如下:find(self,name=None,attrs=,recursive=True,text=None,limit=None,*kwargs)使用方法与find_

    3、all类似,不同的是它只返回第一个满足要求的节点,不是一个列表。例例2-3-1:查找文档中的:查找文档中的元素元素from bs4 import BeautifulSoupdoc=The Dormouses storyThe Dormouses storyOnce upon a time there were three little sisters;and their names wereElsie,Lacie andTillie;and they lived at the bottom of a well.soup=BeautifulSoup(doc,lxml)tag=soup.find(

    4、title)print(type(tag),tag)程序结果:The Dormouses story由此可见查找到元素,元素类型是一个bs4.element.Tag对象。例例2-3-2:查找文档中的所有:查找文档中的所有元素元素from bs4 import BeautifulSoupdoc=The Dormouses storyThe Dormouses storyOnce upon a time there were three little sisters;and their names wereElsie,Lacie andTillie;and they lived at the bo

    5、ttom of a well.soup=BeautifulSoup(doc,lxml)tags=soup.find_all(a)for tag in tags:print(tag)程序结果找到3个元素:ElsieLacieTillie例例2-3-3:查找文档中的第一个:查找文档中的第一个元素元素from bs4 import BeautifulSoupdoc=The Dormouses storyThe Dormouses storyOnce upon a time there were three little sisters;and their names wereElsie,Lacie

    6、andTillie;and they lived at the bottom of a well.soup=BeautifulSoup(doc,lxml)tag=soup.find(a)print(tag)程序结果找到第一个元素:Elsie例例2-3-4:查找文档中:查找文档中class=title的的元素元素from bs4 import BeautifulSoupdoc=The Dormouses storyThe Dormouses storyOnce upon a time there were three little sisters;and their names wereElsi

    7、e,Lacie andTillie;and they lived at the bottom of a well.soup=BeautifulSoup(doc,lxml)tag=soup.find(p,attrs=class:title)print(tag)程序结果找到class=title的元素The Dormouses story很显然如果使用:tag=soup.find(p)也能找到这个元素,因为它是文档的第一个元素。例例2-3-5:查找文档中:查找文档中class=sister的元素的元素from bs4 import BeautifulSoupdoc=The Dormouses st

    8、oryThe Dormouses storyOnce upon a time there were three little sisters;and their names wereElsie,Lacie andTillie;and they lived at the bottom of a well.soup=BeautifulSoup(doc,lxml)tags=soup.find_all(name=None,attrs=class:sister)for tag in tags:print(tag)其中name=None表示无论是什么名字的元素,程序结果找到3个:a class=siste

    9、r href=http:/ id=link1ElsieLacieTillie对于这个文档,很显然语句:tags=soup.find_all(a)或者:tags=soup.find_all(a,attrs=class:sister)效果一样。2.3.2 BeautifulSoup获取元素的属性值获取元素的属性值2.3.2 BeautifulSoup获取元素的属性值获取元素的属性值如果一个元素已经找到,例如找到元素,那么怎么样获取它的属性值呢?BeautifulSoup使用:tagattrName来获取tag元素的名称为attrName的属性值,其中tag是一个bs4.element.Tag对象。

    10、例例2-3-6:查找文档中所有超级链接地址:查找文档中所有超级链接地址from bs4 import BeautifulSoupdoc=The Dormouses storyThe Dormouses storyOnce upon a time there were three little sisters;and their names wereElsie,Lacie andTillie;and they lived at the bottom of a well.soup=BeautifulSoup(doc,lxml)tags=soup.find_all(a)for tag in tags

    11、:print(taghref)程序结果:http:/ BeautifulSoup获取元素包含的文本值获取元素包含的文本值2.3.3 BeautifulSoup获取元素包含的文本值获取元素包含的文本值如果一个元素已经找到,例如找到元素,那么怎么样获取它包含的文本值呢?BeautifulSoup使用:tag.text来获取tag元素包含的文本值,其中tag是一个bs4.element.Tag对象。例例2-3-7:查找文档中所有:查找文档中所有超级链接包含的文本值超级链接包含的文本值from bs4 import BeautifulSoupdoc=The Dormouses storyThe Dor

    12、mouses storyOnce upon a time there were three little sisters;and their names wereElsie,Lacie andTillie;and they lived at the bottom of a well.soup=BeautifulSoup(doc,lxml)tags=soup.find_all(a)for tag in tags:print(tag.text)程序结果:ElsieLacieTillie例例2-3-8:查找文档中所有:查找文档中所有超级链接包含的文本值超级链接包含的文本值from bs4 impor

    13、t BeautifulSoupdoc=The Dormouses storyThe Dormouses storyOnce upon a time there were three little sisters;and their names wereElsie,Lacie andTillie;and they lived at the bottom of a well.soup=BeautifulSoup(doc,lxml)tags=soup.find(p)for tag in tags:print(tag.text)程序结果:The Dormouses story Once upon a

    14、time there were three little sisters;and their names wereElsie,Lacie andTillie;and they lived at the bottom of a well.其中第二个包含的值就是节点子树下面所有文本节点的组合值。2.3.4 BeautifulSoup高级查找高级查找一般find或者find_all都能满足我们的需要,如果还不能那么可以设计一个查找函数来进行查找。例例2-3-9:我们查找文档中的:我们查找文档中的href=http:/ bs4 import BeautifulSoupdoc=The Dormouses

    15、 storyElsieLacieTilliedef myFilter(tag):print(tag.name)return(tag.name=a and tag.has_attr(href)and taghref=http:/ 说明:在程序中我们定义了一个筛选函数myFilter(tag),它的参数是tag对象,在调用soup.find_all(myFilter)时程序会把每个tag元素传递给myFilter函数,由该函数决定这个tag的取舍,如果myFilter返回True就保留这个tag到结果集中,不然就丢掉这个tag。因此程序执行时可以看到html,body,head,title,bod

    16、y,a,a,a等一个个tag经过myFilter的筛选,只有节点Lacie满足要求,因此结果为:Lacie其中:tag.name是tag的名称;tag.has_attr(attName)判断tag是否有attName属性;tagattName是tag的attName属性值;例例2-3-10:通过函数查找可以查找到一些复杂的节点元素,查找文本值以:通过函数查找可以查找到一些复杂的节点元素,查找文本值以cie结尾所有结尾所有节点节点from bs4 import BeautifulSoupdoc=The Dormouses storyElsieLacieTillieTilciedef endsWi

    17、th(s,t):if len(s)=len(t):return slen(s)-len(t):=t return False def myFilter(tag):return(tag.name=a and endsWith(tag.text,cie)soup=BeautifulSoup(doc,lxml)tags=soup.find_all(myFilter)for tag in tags:print(tag)程序结果:LacieTilcie程序中定义了一个endsWIth(s,t)函数判断s字符串是否以字符串t结尾,是就返回True,不然返回False,在myFilter中调用这个函数判断tag.text是否以cie结尾,最后找出所有文本值以cie结尾的节点。

    展开阅读全文
    提示  163文库所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
    关于本文
    本文标题:Python爬虫程序设计KC23.pptx
    链接地址:https://www.163wenku.com/p-3371920.html

    Copyright@ 2017-2037 Www.163WenKu.Com  网站版权所有  |  资源地图   
    IPC备案号:蜀ICP备2021032737号  | 川公网安备 51099002000191号


    侵权投诉QQ:3464097650  资料上传QQ:3464097650
       


    【声明】本站为“文档C2C交易模式”,即用户上传的文档直接卖给(下载)用户,本站只是网络空间服务平台,本站所有原创文档下载所得归上传人所有,如您发现上传作品侵犯了您的版权,请立刻联系我们并提供证据,我们将在3个工作日内予以改正。

    163文库