【问题标题】:How to web scrape date from a website and store that date in database using php and mysql?如何从网站上抓取日期并使用 php 和 mysql 将该日期存储在数据库中?
【发布时间】:2018-03-24 20:52:40
【问题描述】:

我一直在互联网上四处寻找获取事件日期并将该日期存储到数据库中的方法,但找不到太多。

我能够从网站上获取日期,但我不知道如何存储它。

我只想从网站获取日期,然后以 Y-m-d 的格式存储它。如果您知道任何方法,请告诉我。

链接:https://www.brent.gov.uk/events-and-whats-on-calendar/?eventCat=Wembley+Stadium+events

<?php

$curl = curl_init(); 
$all_data = array();

$url = "https://www.brent.gov.uk/events-and-whats-on-calendar/?eventCat=Wembley+Stadium+events";

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($curl);

$event = array();

preg_match_all('/<h3 style="margin:0px!important;">(.*?)<\/h3>/si',$result,$match);
$event['title'] = $match[1];

print_r($event['title']);

echo $all_data;
?>

【问题讨论】:

  • 这里没有 db 的东西。
  • 建议使用 python 脚本而不是 PHP。 BeautifulSoup 可以很容易地用来写一个又快又脏的刮板。 Scrapers 应该不那么频繁地使用(也许一天一次),所以用 php 编写它太费力了。
  • @udiboy1209 那些懂 PHP 而不懂 Python 的人呢?
  • @FunkFortyNiner 这就是问题所在,我只是不知道如何将该数组数据存储为一种格式,然后我可以将其保存到数据库中
  • @udiboy1209 我真的不知道python,但是我正在使用爬虫来获取原始网站每天添加的事件日期,我不知道每天爬一次

标签: php mysql database curl web-scraping


【解决方案1】:

don't use regex to parse html, use a proper HTML parser, 例如 DOMDocument。

对该站点的快速检查显示所有日期都在页面上唯一article 元素的h3 子元素中,您可以使用它来识别它们。提取日期后,可以使用 strtotime() 将其转换为 unix 时间戳,然后可以使用 date() 将其转换为 Y-m-d 格式,例如

$result = curl_exec($curl);
$domd=@DOMDocument::loadHTML($result);
$dateElements=$domd->getElementsByTagName("article")->item(0)->getElementsByTagName("h3");
foreach($dateElements as $ele){
    var_dump(date("Y-m-d",strtotime($ele->textContent)));
}

至于如何将结果存储在 mysql 数据库中,请尝试在 google 中写入 php mysql tutorial -w3schools,或阅读此处的 PDO 部分:http://www.phptherightway.com/#pdo_extension

【讨论】:

  • 代码用于获取日期,但我得到了一些错误
  • E_DEPRECATED : type 8192 -- 非静态方法 DOMDocument::loadHTML() 不应被静态调用 -- 在第 14 行 string(10) "1969-12-31" string(10) " 2018-03-27" 字符串(10) "2018-04-08" 字符串(10) "2018-04-14" 字符串(10) "2018-04-21" 字符串(10) "2018-04-22"字符串(10)“2018-04-30”字符串(10)“2018-05-05”字符串(10)“2018-05-12”字符串(10)“2018-05-13”字符串(10)“2018 -05-19" 字符串(10) "2018-05-20"
  • @PruthviDiu 要么您在编写代码时忘记了 @,要么您在 php.ini 中有 xdebug.scream=1,很可能是前者。
  • brent.gov.uk/events-and-whats-on-calendar/…"; curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($curl); libxml_use_internal_errors(true); $domd=@DOMDocument::loadHTML($result); $dateElements=$domd->getElementsByTagName("article")->item(0)->getElementsByTagName("h3"); foreach($dateElements as $ele){ $data= (date("Y-m-d" , strtotime($ele->textContent )));回显$数据; } ?>
  • 这是我正在使用的代码,我正在 phpfiddle.org 上尝试这个
【解决方案2】:
        <?php

        $db_host = "localhost"; 
        $db_username = "username"; 
        $db_pass = "password"; 
        $db_name = "name"; 

        // Run the actual connection here 
        $con = mysqli_connect($db_host, $db_username, $db_pass, $db_name);
        if ($con->connect_errno) {
            die("Failed to connect to MySQL: (" . $con->connect_errno . ") " . $con->connect_error);
        }

        $curl = curl_init();

    //The Website you want to get data from
        $url = "https://www.brent.gov.uk/events-and-whats-on-calendar/?eventCat=Wembley+Stadium+events";

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        $result = curl_exec($curl);
        libxml_use_internal_errors(true);

        $domd=@DOMDocument::loadHTML($result);

        //Getting the date from the site

        $dateElements=$domd->getElementsByTagName("article")->item(0)->getElementsByTagName("h3");
        foreach($dateElements as $ele){
            $data = (date("Y-m-d",strtotime($ele->textContent)));

        // echo "<br>".$data;

//checking if the date match with database date
           $sql = "SELECT * FROM event_table WHERE date = '$data'";
            $result = $con->query($sql);

        if ($result->num_rows > 0) {

            // output data of each row, if date match echo "Data is there";
            while($row = $result->fetch_assoc()) {
                  echo  "Data is there";
            }
        } 
//if date is not there then inster it into the database
        else {
           $results = mysqli_query($con, "INSERT INTO event_table (id, date) VALUES ('',' $data')");
            echo "data uploaded";
        }

        }
        ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 2014-05-04
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多