网站建设资讯

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://www.chongzhouwz.cn/article/hoihh.html">http://www.chongzhouwz.cn/article/hoihh.html</a> </div> </div> <div class="other"> <h3>其他资讯</h3> <ul> <li> <a href="/article/soehpg.html">有了域名怎么建站?建网站的发展趋势</a> </li><li> <a href="/article/eodohd.html">抖音账号直播怎么运营的,什么是抖音账号直播运营</a> </li><li> <a href="/article/idosdi.html">Xshell常用命令Xshell命令大全</a> </li><li> <a href="/article/gisejs.html">JavaScript预解析及相关技巧有哪些</a> </li><li> <a href="/article/doegssp.html">wordpresscd wordpresscdn不能登录</a> </li><li> <a href="/article/dopeeoe.html">zblog怎么设置为静态 zblog静态插件</a> </li><li> <a href="/article/dcjpdcd.html">磊科路由器好用吗 磊科路由器哪个型号好</a> </li><li> <a href="/article/dshseeh.html">服务器怎么卸载安全狗 安全狗卸载不干净</a> </li><li> <a href="/article/cdijiho.html">哪个VPS好?(美国加州机房优势介绍)</a> </li><li> <a href="/article/cceijji.html">OracleASM是Oracle存储十分好用的解决方案</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="https://www.cdxwcx.com/pay/" target="_blank" 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="http://www.scdanling.com/" title="成都广告牌发光字" target="_blank">成都广告牌发光字</a></li><li><a href="https://www.cdcxhl.com/idc/xixin.html" title="西信服务器托管" target="_blank">西信服务器托管</a></li><li><a href="https://www.cdxwcx.com/tuiguang/zhenghe.html" title="网络整合营销" target="_blank">网络整合营销</a></li><li><a href="http://www.wxkmsfdj.com/" title="康明斯发电机故障维修" target="_blank">康明斯发电机故障维修</a></li><li><a href="http://www.gyfdjwx.com/" title="gyfdjwx.com" target="_blank">gyfdjwx.com</a></li><li><a href="http://www.kmgbzm.com/" title="不锈钢雕塑定制" target="_blank">不锈钢雕塑定制</a></li><li><a href="http://www.lswxfdj.com/" title="乐山维修康明斯发电机" target="_blank">乐山维修康明斯发电机</a></li><li><a href="http://www.ljujm.com/" title="大悟县劳保手套销售" target="_blank">大悟县劳保手套销售</a></li><li><a href="http://www.qcngdk.com/" title="凉山网站制作" target="_blank">凉山网站制作</a></li><li><a href="http://www.cxjianzhan.com/" title="成都app软件开发" target="_blank">成都app软件开发</a></li> </ul> </div> </div> <div class="wrap"> <div class="copyright"> <span class="en">©2025</span> 四川崇州网站建设公司 <span class="en">ALL RIGHTS RESERVED.</span> <a rel="nofollow" href="https://beian.miit.gov.cn" target="_blank">蜀ICP备2025133229号-1</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.chongzhouwz.cn/"><font>首页</font></a></li> <li><a href="/about/"><font>关于我们</font></a></li> <li><a href="/service/"><font>服务范围</font></a></li> <li><a href="/case/"><font>案例展示</font></a></li> <li><a href="/solve/"><font>解决方案</font></a></li> <li><a href="/news/"><font>建站资讯</font></a></li> <li><a href="/contact/"><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>