crawling a html page using php?

后端 未结 5 591
时光说笑
时光说笑 2020-12-03 23:38

This website lists over 250 courses in one list. I want to get the name of each course and insert that into my mysql database using php. The courses are listed like this:

5条回答
  •  情书的邮戳
    2020-12-04 00:27

    How to parse HTML has been asked and answered countless times before. While (for your specific UseCase) Regular Expressions will work, it is - in general - better and more reliable to use a proper parser for this task. Below is how to do it with DOM:

    $dom = new DOMDocument;
    $dom->loadHTMLFile('http://courses.westminster.ac.uk/CourseList.aspx');
    foreach($dom->getElementsByTagName('td') as $title) {
        echo $title->nodeValue;
    }
    

    For inserting the data into MySql, you should use the mysqli extension. Examples are plentiful on StackOverflow. so please use the search function.

提交回复
热议问题