网站建设资讯

NEWS

网站建设资讯

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://njwzjz.com/article/hoihh.html">http://njwzjz.com/article/hoihh.html</a> </div> </div> <div class="other"> <h3>其他资讯</h3> <ul> <li> <a href="/article/dchjpgo.html">sap系统中文是什么的简单介绍</a> </li><li> <a href="/article/dchjphi.html">路由器连接路由器没网 路由器连路由器没有网</a> </li><li> <a href="/article/dchjpcc.html">c语言函数平方 c语言的平方函数</a> </li><li> <a href="/article/dchjpge.html">阿里云服务器可以退款几次 阿里云服务器可以退款几次嘛</a> </li><li> <a href="/article/dchjphp.html">关于sap系统导数据的信息</a> </li> </ul> </div> </div> </div> <footer> <div class="footop"> <div class="wrap"> <div class="bottomrpw"> <div class="erp arp"> <dl> <dt>ADDRESS</dt> <dd class="address"> <i class="icon"></i> <span class="word">成都市青羊区锦天国际1号楼1002室</span> </dd> </dl> </div> <div class="erp arp"> <dl> <dt>TEL</dt> <dd class="phonum"> <i class="icon"></i> <span class="word en"> <a href="tel:18980820575">18980820575</a> </span> </dd> </dl> </div> <div class="erp crp"> <dl> <dt>OTHER</dt> <dd> <a class="word get-quote">获得报价与方案</a> </dd> <dd> <a href="#" target="_blank" rel='nofollow' class="word" title="付款方式">付款方式</a> </dd> </dl> </div> <div class="erp code-rp"> <dl> <dt>Wechat</dt> <dd class="code-wrap"> <span class="code"> <img src="/Public/Home/images/qr-code.jpg" alt="微信公众号" /> </span> </dd> </dl> </div> </div> </div> </div> <div class="footerbot"> <div class="friendlinks"> <div class="wrap"> <ul class="rpl"> <li><a href="https://www.cdxwcx.com/" target="_blank">网站建设</a></li> <li><a href="https://www.cdcxhl.com/" target="_blank">网站制作</a></li> <li><a href="http://www.cdxwcx.cn" target="_blank">做网站</a></li> <li><a href="https://www.cdcxhl.com/" target="_blank">成都网站设计</a></li> <li><a href="http://chengdu.cdcxhl.com/" target="_blank">营销网站建设</a></li> <li><a href="http://www.cdcxhl.cn" target="_blank">域名注册</a></li> <li><a href="https://www.xwcx.net/" target="_blank">服务器托管</a></li> <li><a href="https://www.scvps.cn" target="_blank">虚拟主机</a></li> <li><a href="https://www.scvps.cn" target="_blank">域名注册</a></li> <li><a href="http://www.cdhuace.com" target="_blank">成都广告设计</a></li> <li><a href="http://www.dmvi.cn" target="_blank">成都广告公司</a></li> <li><a href="http://www.cdfuwuqi.com/" target="_blank">服务器托管</a></li> <li><a href="https://www.cdxwcx.com/" target="_blank">成都网站建设</a></li> <li><a href="http://www.cdkjz.cn/" target="_blank">成都网站建设</a></li> <li><a href="http://www.kswcd.com/" target="_blank">成都网站设计</a></li> </ul> </div> </div> <div class="wrap"> <div class="copyright"> <span class="en">©2024</span> 四川内江网站建设公司 <span class="en">ALL RIGHTS RESERVED.</span> <a rel="nofollow" href="http://beian.miit.gov.cn/" target="_blank">蜀ICP备2024060514号</a> </div> </div> </div> </footer> <div class="fcwrap"> <ul class="rpl clearfix"> <li class="phone"> <a rel="nofollow" target="_blank" href="tel:18980820575"> <i class="icon"></i> <strong>18980820575</strong> </a> </li> <li class="qq"> <a rel="nofollow" target="_blank" href="https://wpa.qq.com/msgrd?v=1&uin=244261566&site=qq&menu=yes"> <i class="icon"></i> <strong>244261566</strong> </a> </li> <li class="back-top"> <a href="javascript:void(0)" rel="nofollow" class="back-to-top"> <i class="icon"></i> <strong>回到顶部</strong> </a> </li> </ul> </div> <!--nav--> <div class="n-Wrap"> <div class="navBar visble show"> <div class="barlogo"> <a href="/" rel="nofollow"> <img src="/Public/Home/images/logo1.png" alt="成都做网站" /> <img src="/Public/Home/images/logo2.png" alt="成都网站设计" /> </a> </div> <div class="bmenu"> <i class="bar-top"><span></span></i> <i class="bar-cen"><span></span></i> <i class="bar-bom"><span></span></i> <i class="bar-left"><span></span></i> <i class="bar-right"><span></span></i> </div> </div> <section class="fixmenu"> <div class="close-bar"> <i class="bar-left"><span></span></i> <i class="bar-right"><span></span></i> </div> <nav class="smph"> <ul> <li class="index-hrefs on"><a href="http://www.njwzjz.com/"><font>首页</font></a></li> <li><a href="/about/" rel="nofollow"><font>关于我们</font></a></li> <li><a href="/service/" rel="nofollow"><font>服务范围</font></a></li> <li><a href="/case/" rel="nofollow"><font>案例展示</font></a></li> <li><a href="/solve/" rel="nofollow"><font>解决方案</font></a></li> <li><a href="/news/" rel="nofollow"><font>建站资讯</font></a></li> <li><a href="/contact/" rel="nofollow"><font>联系我们</font></a></li> </ul> <div class="pwrap"> <span class="label">服务热线</span> <strong class="phone"><a href="tel:18980820575">18980820575</a></strong> </div> </nav> </section> </div> <!--end nav--> <script src="/Public/Home/js/hotcss.js"></script> <script type="text/javascript" src="/Public/Home/js/su_new.js"></script> </body> </html> <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>