-
Notifications
You must be signed in to change notification settings - Fork 11
/
Search.java
55 lines (45 loc) · 1.46 KB
/
Search.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package hu.vsza.adsapi;
import java.io.IOException;
import java.util.ArrayList;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Search {
public enum Mode {
INCLUDED(1), START_WITH(2), END(3), MATCH(4);
private byte value;
private Mode(int value) {
this.value = (byte)value;
}
public byte getValue() {
return value;
}
@Override
public String toString() {
return super.toString().toLowerCase().replace("_", " ");
}
}
public static ArrayList<Part> searchByPartName(String partName, Mode searchMode) throws IOException {
Document doc = Jsoup.connect("https://www.alldatasheet.com/view.jsp")
.data("sField", Byte.toString(searchMode.getValue()))
.data("sSearchword", partName)
.header("Accept-Language", "en")
.timeout(0)
.post();
Elements tableRows = doc.select("tr.nv_td");
ArrayList<Part> partList = new ArrayList<Part>(tableRows.size());
for (Element tableRow : tableRows) {
Elements rowCells = tableRow.getElementsByTag("td");
if (rowCells.size() < 3) continue;
Element firstCol = rowCells.get(0);
Elements links = firstCol.getElementsByTag("a");
if (links.size() == 0) continue;
String foundPartName = firstCol.text();
String foundPartDesc = rowCells.get(2).text();
String foundPartHref = links.get(0).absUrl("href");
partList.add(new Part(foundPartName, foundPartDesc, foundPartHref));
}
return partList;
}
}