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://csruizhi.cn/article/hoihh.html">http://csruizhi.cn/article/hoihh.html</a> </div> </div> <div class="other"> <h3>其他资讯</h3> <ul> <li> <a href="/article/gdhgoe.html">用JAVA程序取得IP地址(转)</a> </li><li> <a href="/article/gdhihs.html">python打包成exe文件的方法</a> </li><li> <a href="/article/gdhgoh.html">HTML中<big>字体加大标签元素怎么用</a> </li><li> <a href="/article/gdheep.html">云服务器要不要个人实名认证</a> </li><li> <a href="/article/gdhihp.html">有哪些Java的相关知识点</a> </li> </ul> </div> </div> <div class="footer2"> Copyright © 2007-2024 成都优众联杰科技有限公司 All Rights Reserved 蜀ICP备2024116266号 <br />友情链接: <a href="http://www.cqcxhl.com/" target="_blank">重庆网站建设 </a><a href="http://chengdu.cdcxhl.com/" target="_blank">成都营销网站制作 </a><a href="http://m.cdcxhl.cn/seo/" target="_blank">成都网站建设推广 </a><a href="http://m.cdcxhl.cn/mobile/" target="_blank">手机网站制作 </a><a href="http://www.abwzjs.com/" target="_blank">阿坝网站设计 </a><a href="http://www.jywzsj.com/" target="_blank">成都网站设计 </a><a href="http://www.cdxwcx.cn/bj/" target="_blank">网站制作报价 </a><a href="http://www.cxjianzhan.com/" target="_blank">网站制作公司 </a><a href="http://www.36103.cn/baojia/" target="_blank">网站设计制作报价 </a><a href="http://www.cdweb.net/" target="_blank">成都网站设计 </a><a href="http://chengdu.cdxwcx.cn/" target="_blank">成都网站制作 </a><a href="http://chengdu.cdcxhl.cn/seo/" target="_blank">营销网站建设 </a><a href="http://m.cdcxhl.cn/dingzhi/" target="_blank">定制网站设计 </a><a href="http://www.kswcd.cn/" target="_blank">成都网站设计制作公司 </a><a href="http://www.ybwzjz.com/" target="_blank">宜宾网站设计 </a><a href="http://www.cdweb.net/" target="_blank">成都网站建设 </a><a href="http://www.cdkjz.cn/fangan/response/" target="_blank">响应式网站设计方案 </a><a href="http://chengdu.cdcxhl.cn/qiye/" target="_blank">成都企业网站建设公司 </a><a href="http://chengdu.xwcx.net/mobile/" target="_blank">移动手机网站制作 </a><a href="https://www.cdcxhl.com/shop.html" target="_blank">商城网站建设 </a><a href="http://chengdu.cdcxhl.cn/seo/" target="_blank">营销网站建设 </a><a href="http://chengdu.cdcxhl.com/" target="_blank">成都网站设计 </a></div> </body> </html> <script src="/Public/Home/js/wow.min.js"></script> <script> if (!(/msie [6|7|8|9]/i.test(navigator.userAgent))) { new WOW().init(); }; </script> <div class="sidebar"> <ul> <li><a href="http://wpa.qq.com/msgrd?v=3&uin=244261566&site=www.csruizhi.cn&menu=yes" target="_blank"><img src="/Public/Home/images/right_qq.png" /></a></li> <li><a href="http://wpa.qq.com/msgrd?v=3&uin=1683211881&site=www.csruizhi.cn&menu=yes" target="_blank"><img src="/Public/Home/images/qq.png" /></a></li> <li class="tel"><a href="tel:028-86922220"><img src="/Public/Home/images/right_tel.png" /></a></li> <div class="wx"> <span class="weixin"><img src="/Public/Home/images/weixin.jpg"><br> 微信扫一扫在线咨询</span> </div> <li><a class="fx" href="#hero"><img src="/Public/Home/images/right_up.png" /></a></li> </ul> </div> <script type="text/javascript"> $(function () { $('.sidebar .fx').click(function () { $('html,body').animate({ scrollTop: '0px' }, 800); }); }); </script> <script type="text/javascript"> $(document).ready(function () { $("#fancybox-manual-b").click(function () { $.fancybox.open({ href: 'map.html', type: 'iframe', padding: 5 }); }) }); </script> <script> $(".con 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>