dtvgo28624 2018-09-24 17:43
浏览 120

从其他网站获取信息php

I have a quick question; I want to take some information (the price) from another website and introduce it in my database to see how the price fluctuates. I am having some troubles getting the information.. I dont fully understand oop. There are 2 tries, i tried to make.. The first try, kinda dose the work i want to, but is kinda messy. I asked a friend if he knows another way, and told me about DOM, i did some research about it but i dont quite get it. If anybody knows how to expand my ideea on no2 as i have no ideea how to continue. This is just for me, to develop my skills in php and so on. Again i dont fully understand oop, so please dont be harsh on me.

Try no1

<form action="" method="POST">
    <input type="radio" name="g1" value="x1">x1
    <input type="radio" name="g2" value="x2">x2
    <input type="radio" name="s1" value="x3">x3
    <input type="radio" name="s2" value="x4">x4
    <input type="submit" name="submit" value="click">

    <?php
    if (isset($_POST['g1'])) {
        $r1 = $_POST['g1'];
        name1($r1);
    } elseif (isset($_POST['g2'])) {
        $r2 = $_POST['g2'];
        name2($r2);
    } elseif (isset($_POST['s1'])) {
        $r3 = $_POST['s1'];
        name3($r3);
    } elseif (isset($_POST['s2'])) {
        $r4 = $_POST['s2'];
        name4($r4);
    }
    ?>
</form>
<?php

function name1($end) {
    echo '<br>' . $end . '<br>';
    $data_scrap = file_get_contents(url);
//    echo $data_scrap;
    $string1 = $data_scrap;
    $delimiter1 = '<div class="table-cell col-xs-5 col-sm-2 col-md-3 col-lg-2">';
    $dele_s = explode($delimiter1, $string1);
//    echo '<br>'.$dele_s[1].'<br>';

    $string2 = $dele_s[1];
    $delimiter2 = '<p class="product-new-price">';
    $dele_j = explode($delimiter2, $string2);
    echo'<br>' . $dele_j[1] . '<br>';
// kinda works ?
}

function name2($end) {
    echo $end;
//same thing here as on 1
}

function name3($end) {
    echo $end;
//same thing here as on 1
}

function name4($end) {
    echo $end;
//same thing here as on 1
}
?>

Try no2 How i said, i kinda have no ideea what shall i do here, but if anybody knows how to make it work, or atleast show me another way and explain it to me i will be very gratefull

<?php
$url = '';
//curl on url
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
//get content from url
$data_scrap = file_get_contents('$url');
//create DomDoc on HTML url
$dom = new DOMDocument('1.0', 'utf-8');
$element = $dom->createElement($url);
//create DOMXpath on $DomDoc
$ddom = new DOMXPath($dom);

// find price element ?
// get text ?
// analizer?
// $price (as float)
  • 写回答

1条回答 默认 最新

  • doqpm82240 2018-09-24 18:03
    关注

    You can use DOMDocument, but that's going to get tedious fast. I'd suggest using simple_html_dom as it'll allow you to use selector syntax. In your case:

    $html = file_get_html($url);
    $price_element = $html->find('p.product-new-price', 0); // Assuming there's just one element
    $price = floatval($price_element->plaintext); // Add other parsing if necessary
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?