Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Jsoup从网站抓取文本时出现问题_Java_Android_Html_Parsing_Jsoup - Fatal编程技术网

Java 使用Jsoup从网站抓取文本时出现问题

Java 使用Jsoup从网站抓取文本时出现问题,java,android,html,parsing,jsoup,Java,Android,Html,Parsing,Jsoup,我正试图从亚马逊网站上获取一个价格 以下是我关注的html: <div class="buying" id="priceBlock"> <table class="product"> <tbody> <tr id="actualPriceRow"> <td class="priceBlockLabelPrice" id="actualPriceLabel">

我正试图从亚马逊网站上获取一个价格

以下是我关注的html:

<div class="buying" id="priceBlock">
    <table class="product">
        <tbody>
            <tr id="actualPriceRow">
                <td class="priceBlockLabelPrice" id="actualPriceLabel">Price:</td>
                <td id="actualPriceContent">
                    <span id="actualPriceValue">
                        <b class="priceLarge">
                                $1.99
                        </b>
                    </span>

                </td>
            </tr>
        </tbody>
    </table>
</div>                

为什么这个代码不起作用?

你必须使用
用户代理
,这样网站就不会拒绝你作为用户。您还应该添加一些超时限制,以覆盖默认的超时限制,这可能对您来说太短了。三秒是个不错的选择,但可以随意改变<代码>超时(0)将等待服务器的响应时间。如果你不想限制,就用这个。还有一些奇怪的
DOM
解析,这会导致
NullPointerException
。试试这个

String url = "http://www.amazon.com/dp/B00H2T37SO/?tag=stackoverfl08-20";
Document doc = Jsoup
                .connect(url)
                .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36")
                .timeout(3000)
                .get();

Elements prices = doc.select("table.product b.priceLarge");
for (Element pr : prices)
{
    String priceWithCurrency = pr.text();
    System.out.println(priceWithCurrency);
    String priceAsText = priceWithCurrency.replaceAll( "[$,]", "" );
    double priceAsNumber = Double.parseDouble(priceAsText);
    System.out.println("Price: " + priceAsNumber);
}   

您应该能够使用它们的选择器语法来实现这一点:。在你的例子中,在
priceLarge
类上搜索。有趣的是,它很有效!非常感谢!我花了好几个小时试图找出问题出在哪里,因为它使用的是不同的html位置的不同链接。你真的节省了一些压力!没问题。玩得高兴
String url = "http://www.amazon.com/dp/B00H2T37SO/?tag=stackoverfl08-20";
Document doc = Jsoup
                .connect(url)
                .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36")
                .timeout(3000)
                .get();

Elements prices = doc.select("table.product b.priceLarge");
for (Element pr : prices)
{
    String priceWithCurrency = pr.text();
    System.out.println(priceWithCurrency);
    String priceAsText = priceWithCurrency.replaceAll( "[$,]", "" );
    double priceAsNumber = Double.parseDouble(priceAsText);
    System.out.println("Price: " + priceAsNumber);
}