爬虫入门 获取节点和解析节点课件_第1页
爬虫入门 获取节点和解析节点课件_第2页
爬虫入门 获取节点和解析节点课件_第3页
爬虫入门 获取节点和解析节点课件_第4页
爬虫入门 获取节点和解析节点课件_第5页
已阅读5页,还剩23页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

动手学

Python,实践出真知!Python爬虫入门BeautifulSoup4解析网页动手学Python,实践出真知!1网络爬虫的基本处理流程保存数据发起请求获取响应内容解析内容通过URL向服务器发起request请求,请求可以包含额外的header信息如果服务器正常响应,会收到一个response(所请求的网页内容),如HTML、JSON字符串或者二进制的数据(视频、图片)等HTML代码网页解析器解析JSON数据转换成JSON对象二进制的数据保存到文件保存到本地文件或保存到数据库(MySQL、Redis、MongoDB等)12234BeautifulSoup4Requests网络爬虫的基本处理流程保存数据发起请求获取响应内容解析内容通2网络爬虫的基本处理流程获取响应内容解析内容如果服务器正常响应,会收到一个response(所请求的网页内容),如HTML、JSON字符串或者二进制的数据(视频、图片)等HTML代码网页解析器解析JSON数据转换成JSON对象二进制的数据保存到文件223BeautifulSoup4Requests使用requests库获取HTML页面并将其转换成字符串后,需要进一步解析HTML页面格式,提取有用信息,这需要处理HTML和XML的函数库。beautifulsoup4库,也称为BeautifulSoup库或bs4库,用于解析和处理HTML和XML。网络爬虫的基本处理流程获取响应内容解析内容如果服务器正常响应3BeautifulSoup4BeautifulSoup提供简单的、Python式的函数用来处理导航、搜索、修改分析树等功能。它是一个工具箱,通过解析文档为用户提供需要抓取的数据。由于使用简单,所以不需要多少代码就可以写出一个完整的应用程序。BeautifulSoup已成为和lxml、html5lib一样出色的Python解析器。BeautifulSoup3目前已经停止开发,不过它已经被移植到BS4了,推荐使用BeautifulSoup4,导入时写为importbs4。BeautifulSoup将复杂的HTML文档转换成树形结构,每个节点都是Python对象。解析网页的核心可以归结为两点:获取节点和从节点中提取信息。BeautifulSoup4BeautifulSoup提供简4提取信息提取信息5任务:提取出第1个段落的文本<p>Hello</p>soup.psoup.p.stringfrombs4importBeautifulSoupsoup=BeautifulSoup('<p>Hello</p><p>BeautifulSoup</p>','lxml')print(soup)#<html><body><p>Hello</p></body></html>print(type(soup.p))#<class'bs4.element.Tag'>print(soup.p)#<p>Hello</p>print()#pprint(soup.p.string)#Hello节点文本在标签唯一的情况下,可直接使用标签作为属性值来获得节点。如果有多个同类标签,如这里有两个段落,则soup.p只能代表第一个。标签任务:提取出第1个段落的文本<p>Hello</p>sou6任务:从超链接中提取属性html="""<li><aclass='seasongreen'href="spring.html">春天</a></li>"""soup=BeautifulSoup(html,'lxml')print(soup.a.string)#春天print(soup.li.string)#春天print(soup.a['href'])#spring.htmlprint(soup.a['class'])#['season','green']print(soup.a.attrs)#字典类型{'class':['season','green'],'href':'spring.html'}<aclass='seasongreen'href="spring.html">春天</a>1223标签名name属性attrs文本string123节点3要素:标签名name、属性attrs和文本string

任务:从超链接中提取属性html="""<li><ac7获取节点获取节点8获取节点的主要方式BeautifulSoup提供了多种方式来获取节点,主要有两种:①方法find_all和find;②CSS选择器。由于每个节点有3个属性:标签名、属性、文本,方法find_all和find也针对这3个属性来搜索节点。从名字上也能看出来,find_all搜索所有满足要求的节点,find搜索满足要求的第一个节点。<aclass='seasonred'id='summer'href="summer.html">夏天</a>12223标签名name属性attrs文本string123获取节点的主要方式BeautifulSoup提供了多种方式来9从超文本中提取出夏天html="""<ul><p>一年有四个季节:</p><li><aclass='seasongreen'href="spring.html">春天</a></li><li><aclass='seasonred'id='summer'href="summer.html">夏天</a></li><li><aclass='seasonyellow'href="autumn.html">秋天</a></li><li><aclass='seasonwhite'href="winter.html">冬天</a></li></ul>"""find_all(name,attrs,recursive,string,limit,**kwargs)123标签名属性文本五种选择:字符串,正则表达式,列表,函数,布尔值True从超文本中提取出夏天html="""<ul><p>一年有10方法1:找出所有季节(标签a)html="""<ul><p>一年有四个季节:</p><li><aclass='seasongreen'href="spring.html">春天</a></li><li><aclass='seasonred'id='summer'href="summer.html">夏天</a></li><li><aclass='seasonyellow'href="autumn.html">秋天</a></li><li><aclass='seasonwhite'href="winter.html">冬天</a></li></ul>"""soup=BeautifulSoup(html,'lxml')print(soup.find_all('a')[1].string)print(soup.find_all('a',class_='season')[1].string)print(soup.find_all('a',{'class':'season'})[1].string)print(soup.find_all('a',attrs={'class':'season'})[1].string)#完整形式方法1:找出所有季节(标签a)html="""<ul><11方法2:精准定位(find)html="""<ul><p>一年有四个季节:</p><li><aclass='seasongreen'href="spring.html">春天</a></li><li><aclass='seasonred'id='summer'href="summer.html">夏天</a></li><li><aclass='seasonyellow'href="autumn.html">秋天</a></li><li><aclass='seasonwhite'href="winter.html">冬天</a></li></ul>"""print(soup.find('a',id='summer').string)print(soup.find('a',href="summer.html").string)print(soup.find('a',{'id':'summer'}).string)print(soup.find('a',{'href':'summer.html'}).string)

print(soup.find(string='夏天').string)print(soup.find(string=pile('夏')).string)方法2:精准定位(find)html="""<ul><p12方法2:定位(find_all)html="""<ul><p>一年有四个季节:</p><li><aclass='seasongreen'href="spring.html">春天</a></li><li><aclass='seasonred'id='summer'href="summer.html">夏天</a></li><li><aclass='seasonyellow'href="autumn.html">秋天</a></li><li><aclass='seasonwhite'href="winter.html">冬天</a></li></ul>"""print(soup.find_all('a',id='summer')[0].string)print(soup.find_all('a',href="summer.html")[0].string)

print(soup.find_all('a',{'id':'summer'})[0].string)print(soup.find_all('a',{'href':'summer.html'})[0].string)

print(soup.find_all(string='夏天')[0].string)print(soup.find_all(string=pile('夏'))[0].string)方法2:定位(find_all)html="""<ul>13方法3:CSS选择器html="""<ul><p>一年有四个季节:</p><li><aclass='seasongreen'href="spring.html">春天</a></li><li><aclass='seasonred'id='summer'href="summer.html">夏天</a></li><li><aclass='seasonyellow'href="autumn.html">秋天</a></li><li><aclass='seasonwhite'href="winter.html">冬天</a></li></ul>"""print(soup.select('a')[1].string)print(soup.select('ula')[1].string)print(soup.select('.season')[1].string)print(soup.select('.red')[0].string)print(soup.select_one('.red').string)#使用select_one获取第1个print(soup.select('ul.season')[1].string)print(soup.select('#summer')[0].string)print(soup.select('li>.season')[1].string)#在子标签中查找print(soup.select('ul>li')[1].string)#在子标签中查找方法3:CSS选择器html="""<ul><p>一年有14动手学

Python,实践出真知!Python爬虫入门BeautifulSoup4解析网页动手学Python,实践出真知!15网络爬虫的基本处理流程保存数据发起请求获取响应内容解析内容通过URL向服务器发起request请求,请求可以包含额外的header信息如果服务器正常响应,会收到一个response(所请求的网页内容),如HTML、JSON字符串或者二进制的数据(视频、图片)等HTML代码网页解析器解析JSON数据转换成JSON对象二进制的数据保存到文件保存到本地文件或保存到数据库(MySQL、Redis、MongoDB等)12234BeautifulSoup4Requests网络爬虫的基本处理流程保存数据发起请求获取响应内容解析内容通16网络爬虫的基本处理流程获取响应内容解析内容如果服务器正常响应,会收到一个response(所请求的网页内容),如HTML、JSON字符串或者二进制的数据(视频、图片)等HTML代码网页解析器解析JSON数据转换成JSON对象二进制的数据保存到文件223BeautifulSoup4Requests使用requests库获取HTML页面并将其转换成字符串后,需要进一步解析HTML页面格式,提取有用信息,这需要处理HTML和XML的函数库。beautifulsoup4库,也称为BeautifulSoup库或bs4库,用于解析和处理HTML和XML。网络爬虫的基本处理流程获取响应内容解析内容如果服务器正常响应17BeautifulSoup4BeautifulSoup提供简单的、Python式的函数用来处理导航、搜索、修改分析树等功能。它是一个工具箱,通过解析文档为用户提供需要抓取的数据。由于使用简单,所以不需要多少代码就可以写出一个完整的应用程序。BeautifulSoup已成为和lxml、html5lib一样出色的Python解析器。BeautifulSoup3目前已经停止开发,不过它已经被移植到BS4了,推荐使用BeautifulSoup4,导入时写为importbs4。BeautifulSoup将复杂的HTML文档转换成树形结构,每个节点都是Python对象。解析网页的核心可以归结为两点:获取节点和从节点中提取信息。BeautifulSoup4BeautifulSoup提供简18提取信息提取信息19任务:提取出第1个段落的文本<p>Hello</p>soup.psoup.p.stringfrombs4importBeautifulSoupsoup=BeautifulSoup('<p>Hello</p><p>BeautifulSoup</p>','lxml')print(soup)#<html><body><p>Hello</p></body></html>print(type(soup.p))#<class'bs4.element.Tag'>print(soup.p)#<p>Hello</p>print()#pprint(soup.p.string)#Hello节点文本在标签唯一的情况下,可直接使用标签作为属性值来获得节点。如果有多个同类标签,如这里有两个段落,则soup.p只能代表第一个。标签任务:提取出第1个段落的文本<p>Hello</p>sou20任务:从超链接中提取属性html="""<li><aclass='seasongreen'href="spring.html">春天</a></li>"""soup=BeautifulSoup(html,'lxml')print(soup.a.string)#春天print(soup.li.string)#春天print(soup.a['href'])#spring.htmlprint(soup.a['class'])#['season','green']print(soup.a.attrs)#字典类型{'class':['season','green'],'href':'spring.html'}<aclass='seasongreen'href="spring.html">春天</a>1223标签名name属性attrs文本string123节点3要素:标签名name、属性attrs和文本string

任务:从超链接中提取属性html="""<li><ac21获取节点获取节点22获取节点的主要方式BeautifulSoup提供了多种方式来获取节点,主要有两种:①方法find_all和find;②CSS选择器。由于每个节点有3个属性:标签名、属性、文本,方法find_all和find也针对这3个属性来搜索节点。从名字上也能看出来,find_all搜索所有满足要求的节点,find搜索满足要求的第一个节点。<aclass='seasonred'id='summer'href="summer.html">夏天</a>12223标签名name属性attrs文本string123获取节点的主要方式BeautifulSoup提供了多种方式来23从超文本中提取出夏天html="""<ul><p>一年有四个季节:</p><li><aclass='seasongreen'href="spring.html">春天</a></li><li><aclass='seasonred'id='summer'href="summer.html">夏天</a></li><li><aclass='seasonyellow'href="autumn.html">秋天</a></li><li><aclass='seasonwhite'href="winter.html">冬天</a></li></ul>"""find_all(name,attrs,recursive,string,limit,**kwargs)123标签名属性文本五种选择:字符串,正则表达式,列表,函数,布尔值True从超文本中提取出夏天html="""<ul><p>一年有24方法1:找出所有季节(标签a)html="""<ul><p>一年有四个季节:</p><li><aclass='seasongreen'href="spring.html">春天</a></li><li><aclass='seasonred'id='summer'href="summer.html">夏天</a></li><li><aclass='seasonyellow'href="autumn.html">秋天</a></li><li><aclass='seasonwhite'href="winter.html">冬天</a></li></ul>"""soup=BeautifulSoup(html,'lxml')print(soup.find_all('a')[1].string)print(soup.find_all('a',class_='season')[1].string)print(soup.find_all('a',{'class':'season'})[1].string)print(soup.find_all('a',attrs={'class':'season'})[1].string)#完整形式方法1:找出所有季节(标签a)html="""<ul><25方法2:精准定位(find)html="""<ul><p>一年有四个季节:</p><li><aclass='seasongreen'href="spring.html">春天</a></li><li><aclass='seasonred'id='summer'href="summer.html">夏天</a></li><li><aclass='seasonyellow'href="autumn.html">秋天</a></li><li><aclass='seasonwhite'href="winter.html">冬天</a></li></ul>"""print(soup.find('a',id='summer').string)print(soup.find('a',href="summer.html").string)print(soup.find('a',{'id':'summer'}).string)print(soup.find('a',{'href':'summer.html'}).string)

print(soup.find(string='夏天').string)print(soup.find(string=pile('夏')).string)方法2:精准定位(find)html="""<ul><p26方法2:定位(find_all)html="""<ul><p>一年有四个季节:</p><li><aclass='seasongreen'href="spring.html">春天</a></li><li><aclass='seasonred'id='summer'href="summer.html">夏天</a></li><li><aclass='seasonyellow'href="autumn.html">秋天</a></li><li><aclass='seasonwhite'href="winter.html">冬天</a></li></ul>

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论