php XML Job Scrape
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php XML Job Scrape相关的知识,希望对你有一定的参考价值。
<?php
# set headers
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);
header('Content-Type: text/xml');
# connect to database
include('db.php');
# XML feed
$url = "https://www.linkup.com/xmlFeed.php?access=";
# declare and truncate existing table
$table_name = "jobs";
$sql = "TRUNCATE TABLE $table_name";
if($db->query($sql)) {
// echo "table cleaned<br>";
}
# get xml content
$xmlstr = file_get_contents($url);
$xmlcont = new SimpleXMLElement($xmlstr);
// foreach($xmlcont as $job) {
// echo "{$job->company} - {$job->title} - {$job->city} - {$job->state} \r\n";
// }
# log activity file
date_default_timezone_set("America/New_York");
$file_date = date('Y-m');
$timestamp = date('Y-m-d h:m:s');
$log_file = "logs/{$file_date}_log";
# insert into database
if( !empty($xmlstr) || $xmlstr !== false ):
$job_counter = 0;
foreach($xmlcont as $job):
// METHOD #1: ALL JOBS
$stmt = $db->prepare( "INSERT INTO $table_name (job_id, company, title, city, state, description, country, url, zip, email, facility, recruiter, shift, type, last_updated, posted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
$stmt->execute( array( $job->jobid, $job->company, $job->title, $job->city, $job->state, $job->rawdesc, $job->country, $job->url, $job->zip, $job->email, $job->faciltiy, $job->recruiter, $job->shift, $job->type, $job->lastmodifieddate, $job->pubdate ) );
$job_counter++;
// // METHOD #2: BY UNIQUE IDS
// // run query to get count
// $stmt = $db->prepare("SELECT job_id FROM $table_name WHERE job_id=?");
// $stmt->execute(array($job->jobid));
// $row_count = $stmt->rowCount();
// // if count == 0
// if( $row_count == 0 ):
// $stmt = $db->prepare( "INSERT INTO $table_name (job_id, company, title, city, state, description, country, url, zip, email, facility, recruiter, shift, type, last_updated, posted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
// $stmt->execute( array( $job->jobid, $job->company, $job->title, $job->city, $job->state, $job->rawdesc, $job->country, $job->url, $job->zip, $job->email, $job->faciltiy, $job->recruiter, $job->shift, $job->type, $job->lastmodifieddate, $job->pubdate ) );
// $job_counter++;
// endif;
endforeach;
echo "$job_counter jobs added!<br>";
$log = "$job_counter jobs added on " . date('Y-m-d h:m:s') . "\r\n";
file_put_contents($log_file, $log, FILE_APPEND);
else:
echo "Feed is empty. No jobs added!";
$log = "Feed is empty on " . date('Y-m-d h:m:s') . "\r\n";
file_put_contents($log_file, $log, FILE_APPEND);
endif;
以上是关于php XML Job Scrape的主要内容,如果未能解决你的问题,请参考以下文章