doute7910 2016-09-21 14:51
浏览 34

在标记和特定属性名称之间抓取文本并存储到PHP数组中

i have a question, if i want to grab ALL the product names in url http://www.tokopedia.com/lbagstore

in the url above will display all products

i see in the View Source menu product name is between tag

<b itemprop="name"> [product name] <b>

i have PHP script like below:

<? 
$html=file_get_contents("https://www.tokopedia.com/lbagstore");
preg_match("'<b itemprop=\"name\">(.*?)</b>'si", $html, $match);
$productname = $match[1];
echo $productname;
?>

but it seems all blank page.

i have difficulty especially to put in array and display them all

does anyone can help me to fix this code? Thanks!

  • 写回答

1条回答 默认 最新

  • douzhanhui5662 2016-09-21 16:08
    关注

    You are "inspecting" the html code of the page instead of "displaying the SOURCE CODE". If you want to extract the data from a website, you need to display its source code, then you can get what you want from it using a regex.

    I checked the code myself and there are no <b itemprop="name"> [product name] <b> within the source code, that's why you don't have any results. The only way to see that piece of code was to inspect the code displayed instead of the source code ;)

    If you change your code to this, you will be able to see what the real code looks like and then you will be able to adapt your regex to grab the names of the products you want.

    $html = "https://www.tokopedia.com/lbagstore";
    var_dump($html);
    preg_match("'<b itemprop=\"name\">(.*?)</b>'si", $html, $match);
    $productname = $match[1];
    echo $productname;
    

    Just add the var_dump to display the text. Also if you can't scrap what you want from the website and you need to do it quick, i may recommand you a free google chrome extension called "grepsr" (https://chrome.google.com/webstore/search/grepsr), I tested it and i could extract the names of the products within 5 minuts.

    edit : Also if you want to grab the name of all the products in the page you will have to use preg_match_all() instead of preg_match.

    I hope this helps ;)

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口