版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
动手学
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 腐乳制作工岗前安全培训效果考核试卷含答案
- 2026中国新能源汽车电机控制器散热方案比较研究
- 中小学教师学术论文写作与发表指南
- 2026能源节约产业市场发展供需调研及投资价值规划分析文献
- 2026生物科技行业市场发展分析及发展战略与投资前景预测研究报告
- 2026欧洲零售行业市场现状分析及数字化转型与实体店运营模式
- 安全考试简答题及参考答案
- 交通三控考试题目与答案解析
- 广东美术竞赛速写试题及参考答案分享
- 蓝冠(东莞)生物技术建设项目环境影响报告表
- 2026年秋季学期每周国旗下讲话稿
- ISOIEC TS 17021-152023 管理体系审核和认证机构的合格评定要求第15部分医疗机构质量管理体系审核与认证的能力要求标准立项发展报告
- 2026年小学心理健康教研教师招聘考试笔试试题【含答案】
- 2026年上海中考(化学)考试试卷真题(含答案)
- 护理个案:消化系统疾病的护理
- 2026年苏教版七年级下册数学期末学业检测卷(含答案可下载)
- 关于《弱胶结地层巷道与应力计锚杆(索)支护技术规范》的解读
- 2026江西省住房和城乡建设厅直属事业单位高层次人才招聘1人备考题库及答案详解(全优)
- 2026年广西公务员申论试题解析及答案
- 初中英语阅读教学中分级阅读策略的实践研究课题报告教学研究课题报告
- GPP解读与医院制剂管理思考
评论
0/150
提交评论