使用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);
//因为正文中的商品名称标签没有特殊class或id正则不好抓取,就抓标签中的内容了,一般来说title中内容就是商品名称了(实际有些出入),$title[0]整个title标签 $title[1]标签中内容;
$title=iconv('GBK','UTF-8',$title);
//如果你的网站是utf8编码,那么需要进行一下转码(淘宝是gbk编码)
C.获取价格:
复制代码 代码如下:
preg_match('/<([a-z]+)[^i]*id=\"J_StrPrice\"[^>]*>([^<]*)<\/\\1>/is', $text, $price);
//同理获取id为J_StrPrice的标签内容$price[2], $price[0]是整个标签, $price[1]为strong标签名;
$price=floatval($price);//放入数据库估计还有转一下变量类型
D.获取属性:
这之前获取的内容都是在单标签中相对只需一个正则就可搞定,然而如果要获取如
复制代码 代码如下:
…
…
这样特定div中有未知n个<>标签,获取该特定div将会非常的困难,搜了下网上,最接近的也只是”/<([a-z]+)[^>]*>([^<>]|(?R))*<\/\\1>/”这样使用递归抓取标签对,但是他不能抓特定标签,所以想要轻松抓取class=”attributes”的div我是没法办到了。但是淘宝网页有其特殊性,就是它的各个标签结构基本是固定的…
…
标签后面不是 就是
,所以我们可以采用变通法达到获取属性标签内容的目的。
复制代码 代码如下:
preg_match('/<(div)[^c]*class=\"attributes\"[^>]*>.*<\/\\1>/is', $text, $text0);
//这个正则会抓取
标签,当然我们属性标签就在这个的前面部分。
$text1=preg_replace("/<\/div>[^<]*<(div)[^c]*id=\"description\"[^>]*>.*<\/\\1>/is","",$text0);
//匹配到
至最后
然后用””代替(就是把匹配的删除了),所以如果attributes的div后面紧跟的是description那么我们已经达到目的了。
$attributes=preg_replace("/<\/div>[^<]*<(div)[^c]*class=\"box J_TBox\"[^>]*>.*<\/\\1>/is","",$text1);
//如果attributes后面紧跟box J_Tbox标签,那么我们还需要使用以上这步来剔除box J_Tbox标签,当然如果attributes的div后面紧跟的是description,这一步将不会匹配到任何即什么都不会做。
E.获取描述:
通过上面方法你肯定觉得淘宝页面上任何标签都可以很简单获取了吧(我之前也是这么想的),但是使用这个方法获取描述时得到的内容将会是“描述加载中”,是的,这个描述内容不是在源码中的,它是打开页面加载进一大堆js后,不知道从淘宝的哪个角落中加载进来的。
好吧,那么我们也可以模仿它放一些js进去。不知道哪些对加载描述有用?没事,全加载进来肯定没错。不知道需要放那些特定div上去有作用?抓一个源码,删掉一些div一步步试试看,你会发现“
复制代码 代码如下:
这几个div是加载描述所必须的,那么下面就是写代码了:
复制代码 代码如下:
preg_match_all('/
基本
文件
流程
错误
SQL
调试
- 请求信息 : 2026-05-24 06:16:41 HTTP/1.1 GET : /article/dgpsop.html
- 运行时间 : 1.6382s ( Load:0.0066s Init:0.9878s Exec:0.6345s Template:0.0093s )
- 吞吐率 : 0.61req/s
- 内存开销 : 2,233.55 kb
- 查询信息 : 12 queries 5 writes
- 文件加载 : 36
- 缓存信息 : 0 gets 0 writes
- 配置加载 : 130
- 会话信息 : SESSION_ID=anilhpik06fobnorpchfrplqu7
- /www/wwwroot/tsicrk.com/index.php ( 1.09 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/ThinkPHP.php ( 4.61 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Think.class.php ( 12.26 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Storage.class.php ( 1.37 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Storage/Driver/File.class.php ( 3.52 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Mode/common.php ( 2.82 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Common/functions.php ( 53.56 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Hook.class.php ( 4.01 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/App.class.php ( 13.49 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Dispatcher.class.php ( 14.79 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Route.class.php ( 13.36 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Controller.class.php ( 11.23 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/View.class.php ( 7.59 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/BuildLiteBehavior.class.php ( 3.68 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ParseTemplateBehavior.class.php ( 3.88 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ContentReplaceBehavior.class.php ( 1.91 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Conf/convention.php ( 11.15 KB )
- /www/wwwroot/tsicrk.com/App/Common/Conf/config.php ( 2.14 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Lang/zh-cn.php ( 2.55 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Conf/debug.php ( 1.49 KB )
- /www/wwwroot/tsicrk.com/App/Home/Conf/config.php ( 0.31 KB )
- /www/wwwroot/tsicrk.com/App/Home/Common/function.php ( 3.33 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ReadHtmlCacheBehavior.class.php ( 5.62 KB )
- /www/wwwroot/tsicrk.com/App/Home/Controller/ArticleController.class.php ( 6.02 KB )
- /www/wwwroot/tsicrk.com/App/Home/Controller/CommController.class.php ( 1.60 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Model.class.php ( 60.11 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php ( 32.43 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db/Driver/Pdo.class.php ( 16.74 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Cache.class.php ( 3.83 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Cache/Driver/File.class.php ( 5.87 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Template.class.php ( 28.16 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Template/TagLib/Cx.class.php ( 22.40 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Template/TagLib.class.php ( 9.16 KB )
- /www/wwwroot/tsicrk.com/App/Runtime/Cache/Home/7540f392f42b28b481b30614275e4e55.php ( 17.71 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/WriteHtmlCacheBehavior.class.php ( 0.97 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ShowPageTraceBehavior.class.php ( 5.24 KB )
- [ app_init ] --START--
- Run Behavior\BuildLiteBehavior [ RunTime:0.000005s ]
- [ app_init ] --END-- [ RunTime:0.000029s ]
- [ app_begin ] --START--
- Run Behavior\ReadHtmlCacheBehavior [ RunTime:0.000289s ]
- [ app_begin ] --END-- [ RunTime:0.000313s ]
- [ view_parse ] --START--
- [ template_filter ] --START--
- Run Behavior\ContentReplaceBehavior [ RunTime:0.000056s ]
- [ template_filter ] --END-- [ RunTime:0.000080s ]
- Run Behavior\ParseTemplateBehavior [ RunTime:0.006407s ]
- [ view_parse ] --END-- [ RunTime:0.006436s ]
- [ view_filter ] --START--
- Run Behavior\WriteHtmlCacheBehavior [ RunTime:0.000157s ]
- [ view_filter ] --END-- [ RunTime:0.000172s ]
- [ app_end ] --START--
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`pid`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( pid= )
- [8] Undefined index: pid /www/wwwroot/tsicrk.com/App/Home/Controller/ArticleController.class.php 第 47 行.
- [8] Undefined index: db_host /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php 第 120 行.
- [8] Undefined index: db_port /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php 第 121 行.
- [8] Undefined index: db_name /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php 第 122 行.

1.6382s
