Grab jobs from Freelancer
This task use java and jsoup to grab jobs from Freelancer
Grab jobs from Freelancer
- Create Freelancer class as following
- Call Freelancer.grab() method as following
Call Freelancer.grab() method
List<Freelancer.Job> jobs = Freelancer.grab(); for (int i = 0; i < jobs.size(); i++) { logger.info(jobs.get(i).toString()); }Freelancer class
public class Freelancer { private static Logger logger = Logger.getLogger(Freelancer.class); public static List<Job> grab() { List<Job> jobs = new ArrayList<Job>(); try { String link = "http://api.freelancer.com/Project/Search.xml?order=submitdate&order_dir=desc"; Document doc = Jsoup.parse(new URL(link), 60000); Elements elements = doc.select("item"); Element element = null; for (int i = 0; i < elements.size(); i++) { element = elements.get(i); Job job = new Job(); if (element.select("id").first() == null) continue; job.id = element.select("id").first().text(); job.url = element.select("url").first().text(); job.title = element.select("name").first().text(); job.shortDesc = element.select("short_descr").first().text(); job.data.put("Start date:", element.select("start_date").first().text()); job.data.put("End date:", element.select("end_date").first().text()); job.data.put("Buyer url:", element.select("buyer url").first().text()); job.data.put("Buyer id:", element.select("buyer id").first().text()); job.data.put("Buyer username:", element.select("buyer username").first().text()); job.data.put("Featured:", element.select("options featured").first().text()); job.data.put("Non-public:", element.select("options nonpublic").first().text()); job.data.put("Trial:", element.select("options trial").first().text()); job.data.put("For gold members:", element.select("options for_gold_members").first().text()); job.data.put("Hidden bids:", element.select("options hidden_bids").first().text()); job.data.put("Min budget:", element.select("budget min").first().text()); job.data.put("Max budget:", element.select("budget max").first().text()); Elements eles = element.select("jobs item"); String vals = ""; for (int j = 0; j < eles.size(); j++) { if (vals.length() > 0) vals += ", "; vals += eles.get(j).text(); } job.data.put("Jobs:", vals); jobs.add(job); } } catch (Exception e) { logger.error("", e); } return jobs; } public static class Job { public String id = ""; public String title = ""; public String shortDesc = ""; public String url = ""; public Map<String, String> data = new HashMap<String, String>(); public String toString() { String tag = "\r\n"; tag += "Id: " + id + "\r\n"; tag += "Title: " + title + "\r\n"; tag += "Url: " + url + "\r\n"; tag += "Short Desc: " + shortDesc + "\r\n"; for (String key : data.keySet()) { tag += key + " " + data.get(key) + "\r\n"; } return tag; } } }