Java 无法使用Jsoup从网站中获取价格
我在大学里有一个项目,我需要构建一个软件,根据用户应该输入的URL(目前仅来自Banggood.com)跟踪商品价格 我刚刚开始学习如何从网站上抓取信息,所以我已经设法做到了这一点,我只是停留在一开始。我设法刮取了物品标题,但在物品价格方面没有成功。我上传我当前的代码 我无法从Jsoup网站或谷歌获得正确的信息Java 无法使用Jsoup从网站中获取价格,java,jsoup,Java,Jsoup,我在大学里有一个项目,我需要构建一个软件,根据用户应该输入的URL(目前仅来自Banggood.com)跟踪商品价格 我刚刚开始学习如何从网站上抓取信息,所以我已经设法做到了这一点,我只是停留在一开始。我设法刮取了物品标题,但在物品价格方面没有成功。我上传我当前的代码 我无法从Jsoup网站或谷歌获得正确的信息 import java.io.IOException; import java.util.Scanner; import org.jsoup.Jsoup; import org.jso
import java.io.IOException;
import java.util.Scanner;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
public class ProjectLearning
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
print("Please enter your item URL: ");
String userUrl = scan.next();
print("Give me a sec..");
Document document;
try {
//Get Document object after parsing the html from given url.
document = Jsoup.connect(userUrl).get();
String title = document.title(); //Get title
print("Item name: " + title); //Print title.
//Get price
Elements price = document.select("div#item_now_price");
for (int i=0; i < price.size(); i++)
{
print(price.get(i).text());
}
} catch (IOException e)
{
e.printStackTrace();
}
print("There you go");
}
public static void print(String string) {
System.out.println(string);
}
}
这是因为您获得的元素具有id
项目\u现在的价格
而不是类
查看您输入的URL,带有price的元素如下所示:
<div class="item_now_price" oriattrmin="0" oriattrmax="0" noworiprice="149.9" oriprice="149.9" oriprice3="146.7" oriprice10="145.16" oriprice30="143.64" oriprice100="142.11">US$149.90</div>
149.90美元
正确的选择器应该是Elements price=document.select(“div.item\u now\u price”)代码>
查看以了解有关选择器的更多信息
更新:
因此,我查看了您的代码,您没有得到作为输出的价格的原因是价格是通过另一个Ajax请求加载的。不幸的是,jSoup
在这里帮不了你
有关更多信息,请查看以下答案:
<div class="item_now_price" oriattrmin="0" oriattrmax="0" noworiprice="149.9" oriprice="149.9" oriprice3="146.7" oriprice10="145.16" oriprice30="143.64" oriprice100="142.11">US$149.90</div>