我在 JAVA 中使用 cookie 管理器和连接来获取搜索关键字时显示的列出项目的链接。以下是代码:
public class EBAY {
public static int minPrice = 200;
public static int maxPrice = 200;
public static ArrayList<String> keywords = new ArrayList<>();
public static String searchURL = "https://www.ebay.de/sch/i.html";
public static int maxItemPerKeyword = 20;
protected static Integer doInBackground() throws URISyntaxException {
keywords.add("Auto");
URI searchURI = new URI(searchURL);
for(String keyword : keywords){
int page = 1;
int totalDownloadedItemPerKeyword = 0;
block8: while(totalDownloadedItemPerKeyword < maxItemPerKeyword){
System.out.println("[eBay] Searching for " + keyword + " Page: " + page);
final ArrayList<String> foundURLS = new ArrayList<>();
try {
CookieManager msCookieManager = new CookieManager();
CookieHandler.setDefault(msCookieManager);
msCookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
Connection connection;
connection = Jsoup.connect(searchURL).data("_nkw", keyword);
connection.data("_pgn", String.valueOf(page));
connection.data("_ipg", "200");
connection.data("LH_Auction", "1");
connection.data("LH_BIN", "1");
connection.data("_salic", "77");
connection.data("LH_SellerType", "1");
connection.data("_udlo", String.valueOf(minPrice));
connection.data("_udhi", String.valueOf(maxPrice));
connection.data("_forsp", "1");
connection.userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64; rv:77.0) Gecko/20100101 Firefox/77.0");
for(final Element articleElement : connection.get().getElementById("ResultSetItems").child(1).getElementsByTag("h3")){
final String url = articleElement.child(0).attr("href");
foundURLS.add(url);
}
for(String foundURL : foundURLS){
System.out.println("foundURL: " + foundURL);
}
totalDownloadedItemPerKeyword++;
}catch (IOException e) {
e.printStackTrace();
}
++page;
}
}
System.out.println("[eBay] Done !");
return 0;
}}
列出的项目的 URLS 被正确获取!我面临的唯一问题是cookie管理器在获取链接时会给出以下错误:
java.net.CookieManager put SEVERE: Invalid cookie for https://www.ebay.de/sch/i.html?_nkw=Auto&_pgn=1&_ipg=200&LH_Auction=1&LH_BIN=1&_salic=77&LH_SellerType=1&_udlo=200&_udhi=200&_forsp=1:
有谁知道我为什么会收到这个错误?