我正试图从网站上检索文章价格。问题是,如果你选择网上价格或商店价格,价格会有所不同。选择商店后,网站会创建一个名为:CP_GEODATA的cookie,该cookie具有特定值。我试着用不同的方式发送饼干,但我一直在网上获取价格。
public class Parser {
public static void main(String[] args) throws Exception {
Map<String, String> cookies = new HashMap<String, String>();
cookies.put("CP_COUNTRY ", "%7B%22country%22%3A%22DE%22%7D ");
cookies.put("CP_GEODATA ", "%7B%22location%22%3A-1%2C%22firstlocation%22%3A11%2C%22name%22%3A%22Hamburg%22%7D");
String url = "https://www.cyberport.de/?token=7a2d9b195e32082fec015dca45ba3aa4&sSearchId=565eee12d987b&EVENT=itemsearch&view=liste&query=&filterkategorie=";
Connection.Response res = Jsoup.connect(url).cookies(cookies).data("query", "4B05-525").execute();
Document doc = res.parse();
String tester = doc.select("span[id=articlePrice] > span[class=basis fl]").text();
String tester2 = doc.select("span[id=articlePrice] > span[class=decimal fl]").text();
System.out.println(tester + tester2 + " €");
}
}
我现在得到的价值是2.90欧元,但应该是4.90欧元。我已经尝试了所有的东西,并在互联网上搜索了很多,但我没有找到任何适合我的解决方案。
这是我收到的价格来源:https://www.cyberport.de/micro-usb-2-0-kabel-usb-a-stecker-micro-b-stecker-0-5m--4B05-525_9374.html
我想知道德国汉堡那家商店的价格。
你可以看到我在上面放的饼干。
谢谢你的帮助!
似乎区域信息存储在会话中,当您选择它时,区域代码会在帖子中发送到服务器。
然后你需要做以下步骤:
- 对所需区域执行POST
- 获取会话cookie
- 使用这些食谱进行原始POST
- 希望得到正确的结果
这是代码
public static void main(String[] args) throws Exception {
Connection.Response res;
//11 is for Hamburg
String zoneId = "11";
//Set the zone and get the session cookies
res = Jsoup.connect("https://www.cyberport.de/newajaxpass/catalog/itemlist/0/costinfo/" + zoneId)
.ignoreContentType(true)
.method(Method.POST).execute();
final Map<String, String> cookies = res.cookies();
//print the cookies, we'll see session cookies here
System.out.println(cookies);
//If we use that cookies, your code runs Ok
String url = "https://www.cyberport.de/?token=7a2d9b195e32082fec015dca45ba3aa4&sSearchId=565eee12d987b&EVENT=itemsearch&view=liste&query=&filterkategorie=";
res = Jsoup.connect(url).cookies(cookies).data("query", "4B05-525").execute();
Document doc = res.parse();
String tester = doc.select("span[id=articlePrice] > span[class=basis fl]").text();
String tester2 = doc.select("span[id=articlePrice] > span[class=decimal fl]").text();
System.out.println(tester + tester2 + " €");
//Extra check
System.out.println(doc.select("div.townName").text());
}
你会看到:
{SERVERID=realmN03, SCS=76fe7473007c80ea2cfa059f180c603d, SID=pphdh7otcefvc5apdh2r9g0go2}
4,90 €
Hamburg
我希望这是所期望的结果。