forked from prashantarya12/WebsiteParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getlinked.java
249 lines (188 loc) · 8.14 KB
/
getlinked.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package getlink;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import javax.imageio.ImageIO;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;
public class getlinked
{
static List<productdata> BRAND_LIST = new ArrayList<productdata>();
static Set<productdata> ITEM_LIST = new HashSet<productdata>();
public static void main(String[] args) throws IOException, InterruptedException
{
Capabilities caps = new DesiredCapabilities();
((DesiredCapabilities) caps).setJavascriptEnabled(true);
((DesiredCapabilities) caps).setCapability("takesScreenshot", true);
((DesiredCapabilities) caps).setCapability(
PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
/**Path"\\phantomjs.exe"**/
);
WebDriver driver = new PhantomJSDriver(caps);
ArrayList<productdata> datalist = new ArrayList<productdata>();
for(char alphabet = 'A'; alphabet <= 'Z';alphabet++)
{
driver.get(/**Webpage that has brand listings**/);
List<WebElement> list = driver.findElements(By.cssSelector("a[href*='products.html?brand']")); //Finds elements you are looking for
for(WebElement e : list){
String link = e.getAttribute("title");
String href = e.getAttribute("href");
datalist.add(new productdata(link, null , href, null, null ));
}
}
Set<productdata> gs = new HashSet<>(); //convert to set to remove duplicates
gs.addAll(datalist);
datalist.clear();
datalist.addAll(gs);
datalist.removeAll(Arrays.asList(null,"")); //convert back to list, remove blanks and order alphabetically
Set<String> titles = new HashSet<String>();
for( productdata e : datalist ) {
if( titles.add( e.Name )) BRAND_LIST.add( e );
}
Collections.sort(BRAND_LIST, new Comparator<productdata>() {public int compare(productdata o1, productdata o2) {return o1.Name.compareTo(o2.Name); }});
File brandlog = new File(/**Path**/"");
for(int k = 0; k < BRAND_LIST.size(); k++)
{
try
{
if(brandlog.exists()==false)
{
System.out.println("We had to make a new file.");
brandlog.createNewFile();
}
PrintWriter out = new PrintWriter(new FileWriter(brandlog, true));
out.append(BRAND_LIST.get(k).Name + "\n");
out.close();
}
catch(IOException e)
{
System.out.println("COULD NOT LOG!!");
}
}
//********************************************************************************************************************************////////////////////////////////////////
//********************************************************************************************************************************////////////////////////////////////////
for(int i = 0; i<BRAND_LIST.size();i++)
{
driver.get((BRAND_LIST.get(i).Href)+"&limit=120");
Boolean isnextpage = true;
File log = new File(/**Path**/"");
new File(/**Path**/"").mkdir();
while(isnextpage)
{
if(driver.findElements(By.cssSelector(".next.i-next")).size() > 0)
{
WebElement element = driver.findElement(By.cssSelector(".next.i-next"));
String title = element.getAttribute("title");
String Temphref = element.getAttribute("href");
if(!title.equals("Next")) isnextpage = false;
else if(title.equals("Next"))
{
int result = parser(log, driver, i, BRAND_LIST.get(i).Name);
if(result == -1){System.out.println("The data didnt match at or before page:" + (BRAND_LIST.get(i).Href)+"&limit=120");}
driver.get(Temphref);
}
}
else
{
int result = parser(log, driver, i, BRAND_LIST.get(i).Name);
if(result == -1) System.out.println("The data didnt match at or before page:" + (BRAND_LIST.get(i).Href)+"&limit=120");
break;
}
}
}
//********************************************************************************************************************************////////////////////////////////////////
//********************************************************************************************************************************////////////////////////////////////////
driver.close();
}
static int parser(File log, WebDriver driver, int i, String Brand) throws IOException
{
try{
WebElement mainframe = driver.findElement(By.xpath("//div[contains(@class, 'category-products')]"));
List<WebElement> newlist1 = mainframe.findElements(By.className("product-name"));
List<WebElement> regprice = mainframe.findElements(By.cssSelector(".regular-price,.old-price"));
List<WebElement> images = mainframe.findElements(By.xpath(/**XPath Element**/""));
List<String> ITEM_PRICE = new ArrayList<String>();
List<String> ITEM_NAME = new ArrayList<String>();
List<String> ITEM_HREF = new ArrayList<String>();
List<String> ITEM_IMAGESRC = new ArrayList<String>();
for(WebElement e : regprice)
{
WebElement price = e.findElement(By.className("price"));
String myprice = price.getText();
ITEM_PRICE.add(myprice);
}
for(WebElement e : images)
{
String imgsrc = e.getAttribute("src");
ITEM_IMAGESRC.add(imgsrc);
}
for(WebElement e : newlist1) //creates new object that holds data
{
WebElement atag = e.findElement(By.tagName("a"));
String itemtitle = atag.getAttribute("title");
String itemhref = atag.getAttribute("href");
ITEM_NAME.add(itemtitle);
ITEM_HREF.add(itemhref);
}
System.out.println(ITEM_NAME.size() + " " + ITEM_PRICE.size() + " " + ITEM_HREF.size() +" "+ITEM_IMAGESRC.size());
if(ITEM_NAME.size() != ITEM_PRICE.size() || ITEM_PRICE.size() != ITEM_HREF.size() || ITEM_NAME.size() != ITEM_HREF.size() || ITEM_IMAGESRC.size() != ITEM_HREF.size() )
{
System.out.println("The data didnt match at page with href:" + BRAND_LIST.get(i).Href);
return -1;
}
for(int j = 0; j < ITEM_NAME.size(); j++)
{
System.out.println(ITEM_NAME.get(j) + " " + ITEM_PRICE.get(j) + " " + ITEM_HREF.get(j) + " " + ITEM_IMAGESRC.get(j));
String s = ITEM_IMAGESRC.get(j).replaceAll("/small_image", "/image");
URL smallurl = new URL(ITEM_IMAGESRC.get(j));
URL bigurl = new URL(s.replaceAll("/120x", "/275x"));
String temp = ITEM_NAME.get(j).replaceAll(",", " with ").replaceAll("/", " & ").replaceAll(":", " ").replaceAll("\\*"," ").replaceAll("\""," ");
String imgloc = /**Path**/"";
try{
BufferedImage bufImgOne = ImageIO.read(bigurl);
ImageIO.write(bufImgOne, "png", new File(imgloc));
}
catch(Exception e){
BufferedImage bufImgOne = ImageIO.read(smallurl);
ImageIO.write(bufImgOne, "png", new File(imgloc));
System.out.println("HAD TO USE THUMBNAIL PHOTO");
}
ITEM_LIST.add(new productdata(ITEM_NAME.get(j), ITEM_PRICE.get(j), ITEM_HREF.get(j), ITEM_IMAGESRC.get(j), imgloc));
try
{
if(log.exists()==false)
{
log.createNewFile();
}
PrintWriter out = new PrintWriter(new FileWriter(log, true));
out.append(temp + "," + ITEM_PRICE.get(j) + "," + ITEM_HREF.get(j) + "," + ITEM_IMAGESRC.get(j) + "," + imgloc + "\n");
out.close();
}
catch(IOException e)
{
System.out.println("COULD NOT LOG!!");
}
}
}
catch(Exception e)
{
System.out.println("THIS BRAND HAS NO ITEMS (PLEASE REMOVE EMPTY PHOTO FOLDER LATER)");
}
return 0;
}
}