我正在使用Jsoup来解析亚马逊链接。我正在试图找到一种方法来搜索特定元素的代码;如果找到了,就完成了行动;如果没有,请寻找不同的特定元素并完成行动等。
这是我的代码:
protected Void doInBackground(Void... params) {
//String url = "http://www.amazon.com/gp/product/B00FRDUZXM/ref=s9_simh_gw_p14_d0_i2?pf_rd_m=ATVPDKIKX0DER&pf_rd_s=desktop-4&pf_rd_r=0ZV79CBKAVKNW0RD843D&pf_rd_t=36701&pf_rd_p=1970566762&pf_rd_i=desktop";
String url = "http://www.amazon.com/VeggieTales-Very-Merry-Larry-Christmas/dp/B00H2T37SO/ref=pd_rhf_gw_p_img_3";
//String url = "http://www.amazon.com/Sony-A7-A7R-Snapshots-Great/dp/0321968603/ref=pd_rhf_gw_p_img_4";
try {
// Connect to the web site
Document document = Jsoup.connect(url).get();
// Get the html document title
Elements trs = document.select("table.a-lineitem");
if(trs != null)
{
Elements tds = trs.select("span");
Element price1 = tds.first();
System.out.println(price1);
String str1 = price1.text();
System.out.println(str1);
String str2 = str1.replaceAll( "[$,]", "" );
double aInt = Double.parseDouble(str2);
System.out.println("Price: " + aInt);
}
else
{
Elements prices = document.select("table.product b.priceLarge");
if(prices != null)
{
String priceWithCurrency = prices.text();
System.out.println(priceWithCurrency);
System.out.println(priceWithCurrency);
String priceAsText = priceWithCurrency.replaceAll( "[$,]", "" );
double priceAsNumber = Double.parseDouble(priceAsText);
System.out.println("Price: " + priceAsNumber);
}
else
{
Elements prices2 = document.select("div.a-row span.a-size-medium a-color-price");
String priceWithCurrency2 = prices2.text();
System.out.println(priceWithCurrency2);
String priceAsText2 = priceWithCurrency2.replaceAll( "[$,]", "" );
double priceAsNumber2 = Double.parseDouble(priceAsText2);
System.out.println("Price: " + priceAsNumber2);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
我很肯定我正在寻找页面上正确的元素(例如, table.a-lineitem ,其他两个选中的元素不是问题,所以我必须假设它与我的if / else语句有关。有什么想法吗?
这是错误:
12-17 23:10:05.145 10907-11060/nathanieljones.amazonpricechecker E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #3
Process: nathanieljones.amazonpricechecker, PID: 10907
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.jsoup.nodes.Element.text()' on a null object reference
at nathanieljones.amazonpricechecker.MainActivity$Title.doInBackground(MainActivity.java:102)
at nathanieljones.amazonpricechecker.MainActivity$Title.doInBackground(MainActivity.java:73)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
12-17 23:10:05.834 10907-10907/nathanieljones.amazonpricechecker E/WindowManager﹕ android.view.WindowLeaked: Activity nathanieljones.amazonpricechecker.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{12e2885 V.E..... R......D 0,0-959,322} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:363)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:261)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:298)
at nathanieljones.amazonpricechecker.MainActivity$Title.onPreExecute(MainActivity.java:83)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
at android.os.AsyncTask.execute(AsyncTask.java:535)
at nathanieljones.amazonpricechecker.MainActivity$1.onClick(MainActivity.java:57)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
答案 0 :(得分:0)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.jsoup.nodes.Element.text()' on a null object reference
似乎其中一个元素为null,这导致调用.text()
Element price1 = tds.first();
或
Elements prices2 = document.select("div.a-row span.a-size-medium a-color-price");
在致电.text()
之前保持空检查,就像您为Element prices
if(price1 != null){
//get String using .text()
}
和
if(prices2 != null){
//get String using .text()
}
希望这有帮助。