英文:
Java Getting a link of a product on Ebay/Amazon using JSOUP
问题
我想制作一个程序,可以访问 eBay 并获取我选择的物品的所有标题、价格和链接的列表。例如,我可能查看 iPhone 11 的价格,我的程序应该给我一个按价格排序的所有列表,然后是每个列表旁边的链接。但我似乎找不到如何获取列表的链接。
我的代码:
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;
// 创建一个新数组,大小为 n+1
int newarr[] = new int[n + 1];
// 将元素从旧数组插入新数组
// 插入所有元素直到 n
// 然后在 n+1 处插入 x
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 = '在此处添加链接获取代码';
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<>();
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));
}
}
英文:
I want to make a program that goes to Ebay and get all the titles, prices and link of listings of an item I choose. For example I might look at Iphone 11 prices and my program should give me a list of all the listings in price order and then the link next to each listing. But I can't seem to find out how to get the link of the listing.
My code:
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));
}
}
答案1
得分: 1
尝试这个。
final String itemLink2 = row.select("div.s-item__info > a").attr("href");
英文:
Try this.
final String itemLink2 = row.select("div.s-item__info > a").attr("href");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论