Java 使用 JSOUP 在 Ebay/Amazon 上获取产品的 link
Java Getting a link of a product on Ebay/Amazon using JSOUP
我想制作一个程序,可以访问 Ebay 并获取我选择的商品的所有标题、价格和列表 link。例如,我可能会查看 Iphone 11 个价格,我的程序应该按价格顺序为我提供所有列表的列表,然后是每个列表旁边的 link。但我似乎无法找到如何获取列表的 link。
我的代码:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static int[] addX(int n, int arr[], int x)
{
int i;
// create a new array of size n+1
int newarr[] = new int[n + 1];
// insert the elements from
// the old array into the new array
// insert all elements till n
// then insert x at n+1
for (i = 0; i < n; i++)
newarr[i] = arr[i];
newarr[n] = x;
return newarr;
}
public static void main(String[] args) {
final String url =
"https://www.ebay.co.uk/sch/i.html?_from=R40&_sacat=0&_nkw=iphone+11&LH_BIN=1&Model=Apple%2520iPhone%252011&_dcat=9355&rt=nc&LH_ItemCondition=3000";
ArrayList<Integer> itemPrice = new ArrayList<>();
ArrayList<String> itemTitle = new ArrayList<>();
ArrayList<String> itemLink = new ArrayList<>();
try {
final Document document = Jsoup.connect(url).get();
for (Element row : document.select("li.s-item--watch-at-corner")) {
final String itemTitle2 = row.select(".clearfix.s-item__wrapper > .clearfix.s-item__info > .s-item__link > .s-item__title").text();
final String itemLink2 = 'HELP HERE';
final String tempItemPrice = row.select("div.s-item__detail--primary.s-item__detail:nth-of-type(1) > .s-item__price").text();
final String fixTempItemPrice = tempItemPrice.replaceAll("[£ ,]", "");
double doubleItemPrice = Double.parseDouble(fixTempItemPrice);
int intItemPrice = (int)doubleItemPrice;
itemPrice.add(intItemPrice);
itemTitle.add(itemTitle2);
itemLink.add(itemLink2);
}
}
catch (Exception ex) {
ex.printStackTrace();
}
Map<String, Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < itemTitle.size(); i++) map.put(itemTitle.get(i), itemPrice.get(i));
Map<String, Integer> sortedMap = new LinkedHashMap<>();
map.entrySet().stream().sorted(Map.Entry.<String, Integer>comparingByValue()).forEachOrdered(e -> sortedMap.put(e.getKey(), e.getValue()));
for (String key : sortedMap.keySet()) System.out.println("ITEM: " + key + " PRICE: £" + sortedMap.get(key));
System.out.println(itemLink.get(1));
}
}
试试这个。
final String itemLink2 = row.select("div.s-item__info > a").attr("href");
我想制作一个程序,可以访问 Ebay 并获取我选择的商品的所有标题、价格和列表 link。例如,我可能会查看 Iphone 11 个价格,我的程序应该按价格顺序为我提供所有列表的列表,然后是每个列表旁边的 link。但我似乎无法找到如何获取列表的 link。
我的代码:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static int[] addX(int n, int arr[], int x)
{
int i;
// create a new array of size n+1
int newarr[] = new int[n + 1];
// insert the elements from
// the old array into the new array
// insert all elements till n
// then insert x at n+1
for (i = 0; i < n; i++)
newarr[i] = arr[i];
newarr[n] = x;
return newarr;
}
public static void main(String[] args) {
final String url =
"https://www.ebay.co.uk/sch/i.html?_from=R40&_sacat=0&_nkw=iphone+11&LH_BIN=1&Model=Apple%2520iPhone%252011&_dcat=9355&rt=nc&LH_ItemCondition=3000";
ArrayList<Integer> itemPrice = new ArrayList<>();
ArrayList<String> itemTitle = new ArrayList<>();
ArrayList<String> itemLink = new ArrayList<>();
try {
final Document document = Jsoup.connect(url).get();
for (Element row : document.select("li.s-item--watch-at-corner")) {
final String itemTitle2 = row.select(".clearfix.s-item__wrapper > .clearfix.s-item__info > .s-item__link > .s-item__title").text();
final String itemLink2 = 'HELP HERE';
final String tempItemPrice = row.select("div.s-item__detail--primary.s-item__detail:nth-of-type(1) > .s-item__price").text();
final String fixTempItemPrice = tempItemPrice.replaceAll("[£ ,]", "");
double doubleItemPrice = Double.parseDouble(fixTempItemPrice);
int intItemPrice = (int)doubleItemPrice;
itemPrice.add(intItemPrice);
itemTitle.add(itemTitle2);
itemLink.add(itemLink2);
}
}
catch (Exception ex) {
ex.printStackTrace();
}
Map<String, Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < itemTitle.size(); i++) map.put(itemTitle.get(i), itemPrice.get(i));
Map<String, Integer> sortedMap = new LinkedHashMap<>();
map.entrySet().stream().sorted(Map.Entry.<String, Integer>comparingByValue()).forEachOrdered(e -> sortedMap.put(e.getKey(), e.getValue()));
for (String key : sortedMap.keySet()) System.out.println("ITEM: " + key + " PRICE: £" + sortedMap.get(key));
System.out.println(itemLink.get(1));
}
}
试试这个。
final String itemLink2 = row.select("div.s-item__info > a").attr("href");