Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.7k views
in Technique[技术] by (71.8m points)

java 格式化报错

报错如下

D:javasoftwarejdkinjava.exe "-javaagent:D:loading softwareIntelliJ IDEA 2020.2.3libidea_rt.jar=62089:D:loading softwareIntelliJ IDEA 2020.2.3in" -Dfile.encoding=UTF-8 -classpath D:javasoftwarejdkjrelibcharsets.jar;D:javasoftwarejdkjrelibdeploy.jar;D:javasoftwarejdkjrelibextaccess-bridge-64.jar;D:javasoftwarejdkjrelibextcldrdata.jar;D:javasoftwarejdkjrelibextdnsns.jar;D:javasoftwarejdkjrelibextjaccess.jar;D:javasoftwarejdkjrelibextjfxrt.jar;D:javasoftwarejdkjrelibextlocaledata.jar;D:javasoftwarejdkjrelibext
ashorn.jar;D:javasoftwarejdkjrelibextsunec.jar;D:javasoftwarejdkjrelibextsunjce_provider.jar;D:javasoftwarejdkjrelibextsunmscapi.jar;D:javasoftwarejdkjrelibextsunpkcs11.jar;D:javasoftwarejdkjrelibextzipfs.jar;D:javasoftwarejdkjrelibjavaws.jar;D:javasoftwarejdkjrelibjce.jar;D:javasoftwarejdkjrelibjfr.jar;D:javasoftwarejdkjrelibjfxswt.jar;D:javasoftwarejdkjrelibjsse.jar;D:javasoftwarejdkjrelibmanagement-agent.jar;D:javasoftwarejdkjrelibplugin.jar;D:javasoftwarejdkjrelib
esources.jar;D:javasoftwarejdkjrelib
t.jar;D:javaprojectEPSpiderargetclasses;C:UsersAdministrator.m2
epositoryorgapachehttpcomponentshttpclient4.5.12httpclient-4.5.12.jar;C:UsersAdministrator.m2
epositoryorgapachehttpcomponentshttpcore4.4.13httpcore-4.4.13.jar;C:UsersAdministrator.m2
epositorycommons-loggingcommons-logging1.2commons-logging-1.2.jar;C:UsersAdministrator.m2
epositorycommons-codeccommons-codec1.11commons-codec-1.11.jar;C:UsersAdministrator.m2
epositorycommons-httpclientcommons-httpclient3.1commons-httpclient-3.1.jar;C:UsersAdministrator.m2
epositorydom4jdom4j1.6.1dom4j-1.6.1.jar;C:UsersAdministrator.m2
epositoryxml-apisxml-apis1.0.b2xml-apis-1.0.b2.jar;C:UsersAdministrator.m2
epositoryorgjsoupjsoup1.12.1jsoup-1.12.1.jar main.Main
这是第1页内容
java.util.UnknownFormatConversionException: Conversion = 'F'
    at java.util.Formatter$FormatSpecifier.conversion(Formatter.java:2691)
    at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2720)
    at java.util.Formatter.parse(Formatter.java:2560)
    at java.util.Formatter.format(Formatter.java:2501)
    at java.io.PrintStream.format(PrintStream.java:970)
    at java.io.PrintStream.printf(PrintStream.java:871)
    at com.company.MingLuSpider.GetRequestData(MingLuSpider.java:43)
    at main.Main.main(Main.java:15)
这是第2页内容
java.util.UnknownFormatConversionException: Conversion = 'F'
    at java.util.Formatter$FormatSpecifier.conversion(Formatter.java:2691)
    at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2720)
    at java.util.Formatter.parse(Formatter.java:2560)
    at java.util.Formatter.format(Formatter.java:2501)
    at java.io.PrintStream.format(PrintStream.java:970)
    at java.io.PrintStream.printf(PrintStream.java:871)
    at com.company.MingLuSpider.GetRequestData(MingLuSpider.java:43)
    at main.Main.main(Main.java:21)

这是第3页内容

Process finished with exit code -1
package main;
import com.company.MingLuSpider;
import java.io.IOException;
import java.net.URLDecoder;
public class Main {
    public static void main(String[] args) throws IOException {
        for (int i = 0; i < 11; i++) {
            if (i == 0) {
                System.out.printf("这是第%d页内容",i+1);
                new MingLuSpider().GetRequestData("https://gongshang.mingluji.com/beijing/diqu/%E4%B8%9C%E5%9F%8E");
                System.out.printf("%n");
            } else {
                System.out.printf("这是第%d页内容%n",i+1);
                new MingLuSpider().GetRequestData("https://gongshang.mingluji.com/beijing/diqu/%E4%B8%9C%E5%9F%8E?page=%d" + i);
                System.out.printf("%n");
            }
        }
    }
}
package com.company;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.apache.http.HttpEntity;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class MingLuSpider {
    public void MingLuSpider() {
        MingLuSpider mingspider = new MingLuSpider();
    }
    public void GetRequestData(String url) throws IOException {
        String ResponseBody ;
        String ResponseInsideBody ;
        String ResponseFounderBody;
        try {
            List headers = new ArrayList();
            headers.add(new BasicHeader("User-Agent", "Mozilla/5.0(Windows NT 6.1;Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0"));
            HttpClientBuilder httpClient = HttpClientBuilder.create().setDefaultHeaders(headers);
            HttpGet httpGet = new HttpGet(url);
            CloseableHttpResponse response = httpClient.build().execute(httpGet);
            TimeUnit.SECONDS.sleep((int) (1+Math.random()*(5-1)+1));
            HttpEntity httpEntity = response.getEntity();
            ResponseBody = EntityUtils.toString(httpEntity, "utf-8");
            Document document = Jsoup.parse(ResponseBody);
            Elements getItems = document.select("td[class='views-field views-field-name']");
            for (Element getItem : getItems) {
                String link = "https://gongshang.mingluji.com" + getItem.select("a").attr("href");
                System.out.printf("每个公司链接为:%n" + link);
                HttpGet GetInsideData = new HttpGet(link);
                CloseableHttpResponse ResponseInside = httpClient.build().execute(GetInsideData);
                TimeUnit.SECONDS.sleep((int) (1+Math.random()*(5-1)+1));
                HttpEntity httpinsideEntity = ResponseInside.getEntity();
                ResponseInsideBody = EntityUtils.toString(httpinsideEntity, "utf-8");
                Document DoucumentIns = Jsoup.parse(ResponseInsideBody);
                String CompanyName = DoucumentIns.selectFirst("span[class='field-item'] span[itemprop='name']").text();
                String FounderName=DoucumentIns.selectFirst("span[class='field-item'] span[itemprop='founder']").text();
                String RelatedCompany=DoucumentIns.selectFirst("span[class='field-item'] span[itemprop='founder'] a").attr("href");
                System.out.println(CompanyName);
                System.out.println(FounderName);
                System.out.println("https://gongshang.mingluji.com" +RelatedCompany);
                HttpGet GetFounderLink = new HttpGet("https://gongshang.mingluji.com" +RelatedCompany);
                CloseableHttpResponse ResponseFounderData = httpClient.build().execute(GetFounderLink);
                TimeUnit.SECONDS.sleep((int) (1+Math.random()*(5-1)+1));
                HttpEntity HttpFounderEntity = ResponseFounderData.getEntity();
                ResponseFounderBody = EntityUtils.toString(HttpFounderEntity, "utf-8");
                Document DoucumentFounderComs = Jsoup.parse(ResponseFounderBody);
                System.out.println("这是创始人的相关公司数据");
                Elements GetFounderComs =DoucumentFounderComs.select("div[class='views-field views-field-name'] span[class='field-content']");
                System.out.println(GetFounderComs);
                System.out.println("完结了");
                System.out.printf("%n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void HandlerDate(){
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

TNT的回复是没有问题的哈,我自己都试过了,看你评论说还有问题,我猜想你可能看得比较快,没有注意改全

如果你还是用printf,除了把%n改为%n%s,还有一点,很关键啊,就是要把link作为参数传入printf里啊,所以人家写的也是很清楚辣,中间是逗号,不是加号,不然加%s不就是占位符嘛
image.png

我估计你是写成了

System.out.printf("每个公司链接为:%n%s" + link);

你只用改这么一点就行啦,改完就没毛病啦,数据都打印出来了
image.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...