资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Python常用爬虫代码总结方便查询-创新互联

beautifulsoup解析页面

创新互联公司主营南岔网站建设的网络公司,主营网站建设方案,重庆APP软件开发,南岔h5微信小程序搭建,南岔网站营销推广欢迎南岔等地区企业咨询
from bs4 import BeautifulSoup
soup = BeautifulSoup(htmltxt, "lxml")
# 三种装载器
soup = BeautifulSoup("

", "html.parser") ### 只有起始标签的会自动补全,只有结束标签的会自动忽略 ### 结果为:
soup = BeautifulSoup("

", "lxml") ### 结果为:
soup = BeautifulSoup("

", "html5lib") ### html5lib则出现一般的标签都会自动补全 ### 结果为:

# 根据标签名、id、class、属性等查找标签 ### 根据class、id、以及属性alog-action的值和标签类别查询 soup.find("a",class_="title",id="t1",attrs={"alog-action": "qb-ask-uname"})) ### 查询标签内某属性的值 pubtime = soup.find("meta",attrs={"itemprop":"datePublished"}).attrs['content'] ### 获取所有class为title的标签 for i in soup.find_all(class_="title"): print(i.get_text()) ### 获取特定数量的class为title的标签 for i in soup.find_all(class_="title",limit = 2): print(i.get_text()) ### 获取文本内容时可以指定不同标签之间的分隔符,也可以选择是否去掉前后的空白。 soup = BeautifulSoup('

The Dormouses story

The Dormouses story

', "html5lib") soup.find(class_="title").get_text("|", strip=True) #结果为:The Dormouses story|The Dormouses story ### 获取class为title的p标签的id soup.find(class_="title").get("id") ### 对class名称正则: soup.find_all(class_=re.compile("tit")) ### recursive参数,recursive=False时,只find当前标签的第一级子标签的数据 soup = BeautifulSoup('abc','lxml') soup.html.find_all("title", recursive=False)</pre> <br> 当前标题:Python常用爬虫代码总结方便查询-创新互联 <br> 浏览地址:<a href="http://cdkjz.cn/article/hoihh.html">http://cdkjz.cn/article/hoihh.html</a> </div> <div class="g-return-wrapper clearfix"> <a href="http://www.cdkjz.cn/" class="home">返回首页</a> <a href="http://www.cdkjz.cn/news/" class="column">了解更多建站资讯</a> </div> </div> </div> <div class="full-related-news"> <h3 class="related-title">相关资讯</h3> <div class="related-news weblg"> <ul class="clearfix"> <li> <a href="/article/hegcpe.html"> <h2 class="title">c语言检测函数,C语言检测</h2> </a> </li><li> <a href="/article/hegcjo.html"> <h2 class="title">大一必学python函数,python大学必学吗</h2> </a> </li><li> <a href="/article/hegchc.html"> <h2 class="title">python隐函数绘制,python画隐函数</h2> </a> </li><li> <a href="/article/hegcgc.html"> <h2 class="title">linux路由路径命令,路由器linux命令</h2> </a> </li><li> <a href="/article/hegcod.html"> <h2 class="title">用Java写名字的代码,用java写名字的代码有哪些</h2> </a> </li><li> <a href="/article/hegcei.html"> <h2 class="title">c语言变量函数怎么调,c语言函数变量为指针类型,怎么调用</h2> </a> </li><li> <a href="/article/hegcic.html"> <h2 class="title">Python统计函数包,python编写函数统计字符个数</h2> </a> </li><li> <a href="/article/hegcpj.html"> <h2 class="title">mysql防火墙怎么处理,mysql数据库防火墙</h2> </a> </li> </ul> </div> </div> <div class="full-icontact-cover m-ft-contact"> <div class="weblg"> <div class="clearfix content"> <div class="motto"> 多年建站经验 </div> <div class="info"> <h3>多一份参考,总有益处</h3> <h2> 联系快上网,免费获得专属《策划方案》及报价</h2> <div class="msg"> <p>咨询相关问题或预约面谈,可以通过以下方式与我们联系</p> <h4>业务热线:<a href="tel:400-028-6601" rel="nofollow">400-028-6601</a> / 大客户专线   成都:<a href="tel:+13518219792" rel="nofollow">13518219792</a>   座机:<a href="tel:02886922220" rel="nofollow">028-86922220</a> </h4> </div> </div> </div> <div class="btns clearfix"> <a href="https://wpa.qq.com/msgrd?v=3&uin=631063699&site=qq&menu=yes" target="_blank" rel="nofollow" class="oline">在线咨询</a> <a href="javascript:;" class="edit" rel="nofollow">提交需求</a> </div> </div> </div> <div class="footer-content"> <div class="weblg clearfix"> <div class="friend-links"> <h6 class="clearfix"> <span class="tilte">友情链接</span> <a class="exchagne" href="http://wpa.qq.com/msgrd?v=3&uin=631063699&site=qq&menu=yes">交换友情链接</a> </h6> <div class="link-list clearfix"> <div class="link-slider"> <a href="http://www.cxhljz.cn/" title="成都网站制作" target="_blank">成都网站制作</a><a href="https://www.cdxwcx.com/wangzhan/case/dfjg.html" title="东电技改" target="_blank">东电技改</a><a href="https://www.cdcxhl.com/" title="成都网站设计" target="_blank">成都网站设计</a><a href="http://www.hfwuji.cn/" title="清舞人间" target="_blank">清舞人间</a><a href="http://www.cdxwcx.cn/tuoguan/ershu.html" title="成都二枢机房" target="_blank">成都二枢机房</a><a href="http://www.scyanting.com/" title="四川盐亭做网站" target="_blank">四川盐亭做网站</a><a href="https://www.cdcxhl.com/quanwang.html" title="全网营销推广" target="_blank">全网营销推广</a><a href="http://www.cdkjz.cn/wangzhan/" title="网站推广" target="_blank">网站推广</a><a href="https://www.cdcxhl.com/idc/yldx.html" title="贵州义龙电信机房" target="_blank">贵州义龙电信机房</a><a href="https://www.cdxwcx.com/city/shuangliu/" title="双流做网站" target="_blank">双流做网站</a> </div> </div> </div> </div> <div class="full-foot-bottom"> <div class="weblg clearfix"> <p>成都网站建设公司地址:成都市青羊区太升南路288号锦天国际A座10层 建设咨询<a href="tel:400-028-6601">400-028-6601</a></p> <p> 成都快上网科技有限公司-四川网站建设设计公司 | <a href="http://www.miitbeian.gov.cn/" target="_blank" rel="nofollow">蜀ICP备19037934号</a> Copyright 2020,ALL Rights Reserved cdkjz.cn | <a href="http://www.cdkjz.cn/" target="_blank">成都网站建设</a> | © Copyright 2020版权所有.</p> <p>专家团队为您提供<a href="http://www.cdkjz.cn/" target="_blank">成都网站建设</a>,<a href="http://www.cdkjz.cn/" target="_blank">成都网站设计</a>,成都品牌网站设计,成都营销型网站制作等服务,成都建网站就找快上网! | 成都网站建设哪家好? | <a href="###">网站建设地图</a></p> </div> </div> </div> <script type="text/javascript" src="../js/idangerous.swiper.min.js"></script> <script type="text/javascript" src="../js/wow.min.js"></script> <script type="text/javascript" src="../js/jquery.mousewheel.min.js"></script> <script type="text/javascript" src="../js/jquery.placeholder.min.js"></script> <script type="text/javascript" src="../js/layout.js"></script> </body> </html> <script> $(".singlepage img").each(function(){ var src = $(this).attr("src"); //获取图片地址 var str=new RegExp("http"); var result=str.test(src); if(result==false){ var url = "https://www.cdcxhl.com"+src; //绝对路径 $(this).attr("src",url); } }); window.onload=function(){ document.oncontextmenu=function(){ return false; } } </script>