Java使用JSOUP在Ebay/Amazon上获取产品链接
我想做一个程序,去易趣,并获得所有的标题,价格和我选择的物品清单链接。例如,我可以查看iPhone11的价格,我的程序应该给我一个按价格顺序排列的所有列表,然后是每个列表旁边的链接。但我似乎无法找到如何获取列表的链接 我的代码:Java使用JSOUP在Ebay/Amazon上获取产品链接,java,jsoup,Java,Jsoup,我想做一个程序,去易趣,并获得所有的标题,价格和我选择的物品清单链接。例如,我可以查看iPhone11的价格,我的程序应该给我一个按价格顺序排列的所有列表,然后是每个列表旁边的链接。但我似乎无法找到如何获取列表的链接 我的代码: import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import java.util.*; import java.util.stream.C
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));
}
}
import org.jsoup.jsoup;
导入org.jsoup.nodes.Document;
导入org.jsoup.nodes.Element;
导入java.util.*;
导入java.util.stream.collector;
公共班机{
公共静态int[]addX(int n,int arr[],int x)
{
int i;
//创建大小为n+1的新数组
int newarr[]=新int[n+1];
//从中插入元素
//将旧数组转换为新数组
//插入所有元素,直到n
//然后在n+1处插入x
对于(i=0;i.clearfix.s-item\uuuu信息>.s-item\uuu链接>.s-item\uuu标题”)。text();
最后一个字符串itemLink2='HELP HERE';
最终字符串tempItemPrice=row.select(“div.s-item_uuudetail--primary.s-item_uuudetail:nth类型(1)>.s-item_uuuprice”).text();
最终字符串fixTempItemPrice=tempItemPrice.replaceAll(“[£,]”,“”);
double doubleItemPrice=double.parseDouble(FixtTempItemPrice);
int inttemprice=(int)doubleItemPrice;
项目价格。添加(价格);
itemTitle.add(itemTitle2);
itemLink.add(itemLink2);
}
}
捕获(例外情况除外){
例如printStackTrace();
}
Map Map=newhashmap();
对于(inti=0;isortedMap.put(e.getKey(),e.getValue());
对于(字符串键:sortedMap.keySet())System.out.println(“项目:“+key+”价格:“+sortedMap.get(键));
System.out.println(itemLink.get(1));
}
}
试试这个
final String itemLink2 = row.select("div.s-item__info > a").attr("href");
如果这个答案对你有帮助,请考虑点击它旁边的V符号来接受它。