获取Ebay/Amazon产品链接使用JSOUP的Java代码。

huangapple go评论86阅读模式
英文:

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 &lt; n; i++)
newarr[i] = arr[i];
newarr[n] = x;
return newarr;
}
public static void main(String[] args) {
final String url =
&quot;https://www.ebay.co.uk/sch/i.html?_from=R40&amp;_sacat=0&amp;_nkw=iphone+11&amp;LH_BIN=1&amp;Model=Apple%2520iPhone%252011&amp;_dcat=9355&amp;rt=nc&amp;LH_ItemCondition=3000&quot;;
ArrayList&lt;Integer&gt; itemPrice = new ArrayList&lt;&gt;();
ArrayList&lt;String&gt; itemTitle = new ArrayList&lt;&gt;();
ArrayList&lt;String&gt; itemLink = new ArrayList&lt;&gt;();
try {
final Document document = Jsoup.connect(url).get();
for (Element row : document.select(&quot;li.s-item--watch-at-corner&quot;)) {
final String itemTitle2 = row.select(&quot;.clearfix.s-item__wrapper &gt; .clearfix.s-item__info &gt; .s-item__link &gt; .s-item__title&quot;).text();
final String itemLink2 = &#39;HELP HERE&#39;;
final String tempItemPrice = row.select(&quot;div.s-item__detail--primary.s-item__detail:nth-of-type(1) &gt; .s-item__price&quot;).text();
final String fixTempItemPrice = tempItemPrice.replaceAll(&quot;[&#163; ,]&quot;, &quot;&quot;);
double doubleItemPrice = Double.parseDouble(fixTempItemPrice);
int intItemPrice = (int)doubleItemPrice;
itemPrice.add(intItemPrice);
itemTitle.add(itemTitle2);
itemLink.add(itemLink2);
}
}
catch (Exception ex) {
ex.printStackTrace();
}
Map&lt;String, Integer&gt; map = new HashMap&lt;String, Integer&gt;();
for (int i = 0; i &lt; itemTitle.size(); i++) map.put(itemTitle.get(i), itemPrice.get(i));
Map&lt;String, Integer&gt; sortedMap = new LinkedHashMap&lt;&gt;();
map.entrySet().stream().sorted(Map.Entry.&lt;String, Integer&gt;comparingByValue()).forEachOrdered(e -&gt; sortedMap.put(e.getKey(), e.getValue()));
for (String key : sortedMap.keySet()) System.out.println(&quot;ITEM: &quot; + key + &quot; PRICE: &#163;&quot; + 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(&quot;div.s-item__info &gt; a&quot;).attr(&quot;href&quot;);

huangapple
  • 本文由 发表于 2020年7月23日 07:04:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63044413.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定