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
Freelancer class
1 | List<Freelancer.Job> jobs = Freelancer.grab(); |
2 | for (int i = 0; i < jobs.size(); i++) { |
3 | logger.info(jobs.get(i).toString()); |
4 | } |
List<Freelancer.Job> jobs = Freelancer.grab(); for (int i = 0; i < jobs.size(); i++) { logger.info(jobs.get(i).toString()); }
1 | public class Freelancer { |
2 | |
3 | private static Logger logger = Logger.getLogger(Freelancer.class); |
4 | |
5 | public static List<Job> grab() { |
6 | List<Job> jobs = new ArrayList<Job>(); |
7 | try { |
8 | String link = "http://api.freelancer.com/Project/Search.xml?order=submitdate&order_dir=desc"; |
9 | Document doc = Jsoup.parse(new URL(link), 60000); |
10 | Elements elements = doc.select("item"); |
11 | Element element = null; |
12 | for (int i = 0; i < elements.size(); i++) { |
13 | element = elements.get(i); |
14 | Job job = new Job(); |
15 | if (element.select("id").first() == null) continue; |
16 | job.id = element.select("id").first().text(); |
17 | job.url = element.select("url").first().text(); |
18 | job.title = element.select("name").first().text(); |
19 | job.shortDesc = element.select("short_descr").first().text(); |
20 | job.data.put("Start date:", element.select("start_date").first().text()); |
21 | job.data.put("End date:", element.select("end_date").first().text()); |
22 | job.data.put("Buyer url:", element.select("buyer url").first().text()); |
23 | job.data.put("Buyer id:", element.select("buyer id").first().text()); |
24 | job.data.put("Buyer username:", element.select("buyer username").first().text()); |
25 | job.data.put("Featured:", element.select("options featured").first().text()); |
26 | job.data.put("Non-public:", element.select("options nonpublic").first().text()); |
27 | job.data.put("Trial:", element.select("options trial").first().text()); |
28 | job.data.put("For gold members:", element.select("options for_gold_members").first().text()); |
29 | job.data.put("Hidden bids:", element.select("options hidden_bids").first().text()); |
30 | job.data.put("Min budget:", element.select("budget min").first().text()); |
31 | job.data.put("Max budget:", element.select("budget max").first().text()); |
32 | Elements eles = element.select("jobs item"); |
33 | String vals = ""; |
34 | for (int j = 0; j < eles.size(); j++) { |
35 | if (vals.length() > 0) vals += ", "; |
36 | vals += eles.get(j).text(); |
37 | } |
38 | job.data.put("Jobs:", vals); |
39 | jobs.add(job); |
40 | } |
41 | } catch (Exception e) { |
42 | logger.error("", e); |
43 | } |
44 | return jobs; |
45 | } |
46 | |
47 | public static class Job { |
48 | public String id = ""; |
49 | public String title = ""; |
50 | public String shortDesc = ""; |
51 | public String url = ""; |
52 | public Map<String, String> data = new HashMap<String, String>(); |
53 | |
54 | public String toString() { |
55 | String tag = "\r\n"; |
56 | tag += "Id: " + id + "\r\n"; |
57 | tag += "Title: " + title + "\r\n"; |
58 | tag += "Url: " + url + "\r\n"; |
59 | tag += "Short Desc: " + shortDesc + "\r\n"; |
60 | for (String key : data.keySet()) { |
61 | tag += key + " " + data.get(key) + "\r\n"; |
62 | } |
63 | return tag; |
64 | } |
65 | } |
66 | |
67 | } |
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; } } }