使用PHP怎么采集抓取淘宝网的单个商品信息-创新互联-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

使用PHP怎么采集抓取淘宝网的单个商品信息-创新互联

使用PHP怎么采集抓取淘宝网的单个商品信息?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

10多年创新互联建站网站建设,由一走到现在,当中离不开团队顽强的创业精神,离不开伴随我们同行的客户与专业的合作伙伴,创力信息一直秉承以“见一个客户,了解一个行业,交一个朋友”的方式为经营理念,提出“让每一个客户成为我们的终身客户”为目标,以为用户提供精细化服务,全面满足用户需求为宗旨,诚信经营,更大限度为用户创造价值。期待迈向下一个更好的10多年。

思路:


file_get_contents(url) 该函数根据 url 如 /tupian/20230522/ 将该网页内容(源码)以字符串形式输出(一个整字符串),然后配合preg_match,preg_replace等这些正则表达式操作就可以实现获取该url特定div,img等信息了。当然前题是淘宝在单个商品页面的结构是固定的,如500图的img中id就是J_ImgBooth!

具体实现方法:(获取500图,名称,价格,属性及商品描述)


复制代码 代码如下:


$text=file_get_contents("/tupian/20230522/item.htm //将url地址上页面内容保存进$text


A.获取500图:


复制代码 代码如下:


preg_match('/]*id="J_ImgBooth"[^r]*rc=\"([^"]*)\"[^>]*>/', $text, $img);
//运用正则抓取img标签中id为J_ImgBooth的img,$img[0]为该500图img标签,$img[1]为500图的图片地址;


B. 获取名称:

复制代码 代码如下:


preg_match('/([^<>]*)<\/title>/', $text, $title);<br/>//因为正文中的商品名称标签没有特殊class或id正则不好抓取,就抓<title>标签中的内容了,一般来说title中内容就是商品名称了(实际有些出入),$title[0]整个title标签 $title[1]标签中内容;<br/>$title=iconv('GBK','UTF-8',$title);<br/>//如果你的网站是utf8编码,那么需要进行一下转码(淘宝是gbk编码)</p><br/><p><strong>C.获取价格:</strong></p><p>复制代码 代码如下:</p><p><br/>preg_match('/<([a-z]+)[^i]*id=\"J_StrPrice\"[^>]*>([^<]*)<\/\\1>/is', $text, $price);<br/>//同理获取id为J_StrPrice的标签内容$price[2], $price[0]是整个标签, $price[1]为strong标签名;<br/>$price=floatval($price);//放入数据库估计还有转一下变量类型</p><br/><p><strong>D.获取属性:</strong></p><p>这之前获取的内容都是在单标签中相对只需一个正则就可搞定,然而如果要获取如</p><p>复制代码 代码如下:</p><p><br/>…<br/> <br/><div id=”xxx”><br/> <br/>…<br/> <br/><ul><br/> <br/>…<br/> <br/></ul><br/> <br/><div>…<br/> <br/><div>…<br/> <br/></div><br/> <br/></div><br/> <br/></div><br/> <br/>…</p><br/><p>这样特定div中有未知n个<>标签,获取该特定div将会非常的困难,搜了下网上,最接近的也只是”/<([a-z]+)[^>]*>([^<>]|(?R))*<\/\\1>/”这样使用递归抓取标签对,但是他不能抓特定标签,所以想要轻松抓取class=”attributes”的div我是没法办到了。但是淘宝网页有其特殊性,就是它的各个标签结构基本是固定的…<div>…</div>标签后面不是</div><div id=”description”>就是</div><div>,所以我们可以采用变通法达到获取属性标签内容的目的。</p><p>复制代码 代码如下:</p><p><br/>preg_match('/<(div)[^c]*class=\"attributes\"[^>]*>.*<\/\\1>/is', $text, $text0);<br/>//这个正则会抓取<div开始到整个页面最后一个</div>标签,当然我们属性标签就在这个的前面部分。<br/> <br/>$text1=preg_replace("/<\/div>[^<]*<(div)[^c]*id=\"description\"[^>]*>.*<\/\\1>/is","",$text0);<br/>//匹配到</div ><div id=”description”>至最后</div>然后用””代替(就是把匹配的删除了),所以如果attributes的div后面紧跟的是description那么我们已经达到目的了。<br/> <br/>$attributes=preg_replace("/<\/div>[^<]*<(div)[^c]*class=\"box J_TBox\"[^>]*>.*<\/\\1>/is","",$text1);<br/>//如果attributes后面紧跟box J_Tbox标签,那么我们还需要使用以上这步来剔除box J_Tbox标签,当然如果attributes的div后面紧跟的是description,这一步将不会匹配到任何即什么都不会做。</p><br/><p><strong>E.获取描述:</strong></p><p>通过上面方法你肯定觉得淘宝页面上任何标签都可以很简单获取了吧(我之前也是这么想的),但是使用这个方法获取描述时得到的内容将会是“描述加载中”,是的,这个描述内容不是在源码中的,它是打开页面加载进一大堆js后,不知道从淘宝的哪个角落中加载进来的。</p><p>好吧,那么我们也可以模仿它放一些js进去。不知道哪些对加载描述有用?没事,全加载进来肯定没错。不知道需要放那些特定div上去有作用?抓一个源码,删掉一些div一步步试试看,你会发现“<div id=”detail”> </div></p><p>复制代码 代码如下:</p><p><br/><div id="description"><br/> <br/><div id="J_DivItemDesc">描述加载中</div><br/> <br/></div></p><br/><p>这几个div是加载描述所必须的,那么下面就是写代码了:</p><p>复制代码 代码如下:</p><p><br/>preg_match_all('/<script[^>]*>[^<]*<\/script>/is', $text, $content);//页面js脚本<br/> $content=$content[0];<br/> $description='<div id="detail"> </div><br/>  <div id="description"><br/>   <div id="J_DivItemDesc">描述加载中</div><br/>  </div>';<br/>foreach ($content as &$v){$description.=iconv('GBK','UTF-8',$v);};</p><br/><br/><p>关于使用PHP怎么采集抓取淘宝网的单个商品信息问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。</p> <br> 当前题目:使用PHP怎么采集抓取淘宝网的单个商品信息-创新互联 <br> 文章路径:<a href="http://kswsj.cn/article/dgpsop.html">http://kswsj.cn/article/dgpsop.html</a> </div> </div> <div class="other"> <h3>其他资讯</h3> <ul> <li> <a href="/article/spoddc.html">深度分销</a> </li><li> <a href="/article/spodci.html">抖音短视频运营是什么部门的</a> </li><li> <a href="/article/spoddi.html">剧情号抖音代运营</a> </li><li> <a href="/article/spodss.html">吉林抖音代运营分润方法</a> </li><li> <a href="/article/spoddo.html">桐乡抖音代运营行情</a> </li> </ul> </div> </div> <div class="line"></div> <!--底部--> <footer id="5"> <div class="foot1 container"> <div class="list"> <div class="item"> <a href="javascript:;"> <span class="ico1"><i class="iconfont"></i><img src="/Public/Home/img/ewm.png" alt=""></span> <strong>关注我们</strong> </a> </div> <div class="item"> <a href="" target="_blank"> <span><i class="iconfont"></i></span> <strong>索要报价</strong> </a> </div> <div class="item"> <a href="" target="_blank"> <span><i class="iconfont"></i></span> <strong>我要咨询</strong> </a> </div> <div class="item"> <a href="" target="_blank"> <span><i class="iconfont"></i></span> <strong>找到我们</strong> </a> </div> <div class="item"> <a href="" target="_blank"> <span><i class="iconfont"></i></span> <strong>投诉建议</strong> </a> </div> </div> <div class="tel"> <dl> <tel><a href="tel:400-028-6601" target="_blank">400-028-6601</a></tel><br> <span>也许您需要专业的服务,欢迎来电咨询</span> </dl> <dl> <tel><a href="tel:18980820575" target="_blank">18980820575</a></tel><br> <span>您的需求,是我们前进的动力</span> </dl> </div> </div> <div class="friend"> <div class="container"> <span class="tit">友情链接:</span> <div class="inner"> <a href="https://www.cdcxhl.com/waimao.html" target="_blank">外贸网站建设</a><a href="https://www.cdcxhl.com/ruanwen/" target="_blank">软文发稿投放平台</a><a href="https://www.cdcxhl.com/pinpai.html" target="_blank">品牌网站建设</a><a href="https://www.cdxwcx.com/jifang/xiyun.html" target="_blank">成都西云机房</a><a href="https://www.cdcxhl.com/link/" target="_blank">友情链接出售</a><a href="https://www.cdcxhl.com/ruanwen/" target="_blank">软文营销推广</a><a href="https://www.cdcxhl.com/ruanwen/" target="_blank">软文发布</a><a href="https://www.cdcxhl.com/gaofang/" target="_blank">高防服务器租用</a><a href="https://www.cdcxhl.com/ruanwen/" target="_blank">软文发稿</a><a href="https://www.cdcxhl.com/douyin/" target="_blank">抖音运营公司</a><a href="https://www.cdcxhl.com/link/" target="_blank">买卖链接</a><a href="https://www.cdcxhl.com/xiyun.html" target="_blank">成都移动机房</a><a href="https://www.cdcxhl.com/ddos/" target="_blank">DDOS防护</a><a href="https://www.cdcxhl.com/xiyun.html" target="_blank">移动服务器托管</a><a href="https://www.cdcxhl.com/" target="_blank">做网站</a><a href="https://www.cdcxhl.com/xiaochengx.html" target="_blank">成都微信小程序开发</a><a href="https://www.cdcxhl.com/app.html" target="_blank">app软件开发公司</a><a href="https://www.cdcxhl.com/xiyun.html" target="_blank">移动主机托管</a> </div> </div> </div> <div class="foot"> <div class="container"> <div class="footNav"> <h3>网站建设</h3> <a href="https://www.cdxwcx.com/city/dujiangyan/" target="_blank">都江堰网站建设</a><a href="https://www.cdxwcx.com/city/suining/" target="_blank">遂宁网站建设</a><a href="http://www.scyingshan.cn/" target="_blank">营山网站建设</a> </div> <div class="footNav"> <h3>服务器托管</h3> <a href="https://www.cdcxhl.com/jigui/" target="_blank">服务器托管机柜</a><a href="https://www.cdcxhl.com/idc/yidong.html" target="_blank">移动服务器托管</a><a href="https://www.cdcxhl.com/idc/cqstsanx.html" target="_blank">重庆水土三线托管</a> </div> <div class="footNav"> <h3>网站制作</h3> <a href="http://m.xwcx.net/" target="_blank">H5网站制作</a><a href="http://m.cdcxhl.cn/dingzhi/" target="_blank">定制网站制作</a><a href="https://www.cdxwcx.com/wangzhan/mbqiye.html" target="_blank">成都企业网站制作</a> </div> <div class="footNav"> <h3>企业服务</h3> <a href="https://www.cdcxhl.com/ruanwen/" target="_blank">软文发布</a><a href="https://www.cdcxhl.com/shoulu/" target="_blank">分类目录网站</a><a href="https://www.cdcxhl.com/service/ypfwzgz.html" target="_blank">互联网药品信息服务资格证</a> </div> <div class="fr ecode"> <div class="fl"> <img src="/Public/Home/img/ewm.jpg"> <p>关注企业微信</p> </div> <div class="fr slogan"> <p class="icon"> <a class="ph" href=""><i class="iconfont"></i></a> <a class="qq" href="tencent://message/?uin=1683211881&Site=&Menu=yes"><i class="iconfont"></i></a> </p> <p> <i>想要找 </i> <a href="">小程序开发</a>、<a href="">APP开发</a>、 <a href="">营销型网站建设</a>、<a href="">网站建设</a>、 <i><a href="">网站定制开发</a></i> ,就选<a href="">创新互联</a> </p> </div> </div> </div> <div class="bottom container"> <p class="fl"> 版权所有:成都创新互联科技有限公司 备案号:<a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">蜀ICP备19037934号</a> 服务热线:028-86922220 </p> <p class="fr"> <a href="https://www.cdxwcx.com/" target="_blank">成都网站建设</a>: <a href="https://www.cdcxhl.com/" target="_blank">创新互联</a> </p> </div> </div> </footer> <!--在线咨询--> <div class="fot"> <ul> <li> <a href="https://p.qiao.baidu.com/cps/mobileChat?siteId=11284691&userId=6256368&type=1&reqParam=%20{%22from%22:0,%22sessionid%22:%22%22,%22siteId%22:%2211284691%22,%22tid%22:%22-1%22,%22userId%22:%226256368%22,%22ttype%22:1,%22siteConfig%22:%20{%22eid%22:%226256368%22,%22queuing%22:%22%22,%22siteToken%22:%226ce441ff9e2d6bedbdfc2a4138de449e%22,%22userId%22:%226256368%22,%22isGray%22:%22false%22,%22wsUrl%22:%22wss://p.qiao.baidu.com/cps3/websocket%22,%22likeVersion%22:%22generic%22,%22siteId%22:%2211284691%22,%22online%22:%22true%22,%22webRoot%22:%22//p.qiao.baidu.com/cps3/%22,%22bid%22:%22160142915792139572%22,%22isSmallFlow%22:0,%22isPreonline%22:0,%22invited%22:0%20},%22config%22:%20{%22themeColor%22:%224d74fa%22%20}%20}&appId=&referer=&iswechat=0&expectWaiter=-1&openid=null&otherParam=null&telephone=null&speedLogId=null&eid=null&siteToken=6ce441ff9e2d6bedbdfc2a4138de449e" target="_blank"> <img src="/Public/Home/img/fot1.png" alt=""> <p>在线咨询</p> </a> </li> <li> <a href="tel:18980820575" target="_blank"> <img src="/Public/Home/img/fot2.png" alt=""> <p>拨打电话</p> </a> </li> </ul> </div> </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>