java 格式化报错
报错如下
D:\javasoftware\jdk\bin\java.exe "-javaagent:D:\loading software\IntelliJ IDEA 2020.2.3\lib\idea_rt.jar=62089:D:\loading software\IntelliJ IDEA 2020.2.3\bin" -Dfile.encoding=UTF-8 -classpath D:\javasoftware\jdk\jre\lib\charsets.jar;D:\javasoftware\jdk\jre\lib\deploy.jar;D:\javasoftware\jdk\jre\lib\ext\access-bridge-64.jar;D:\javasoftware\jdk\jre\lib\ext\cldrdata.jar;D:\javasoftware\jdk\jre\lib\ext\dnsns.jar;D:\javasoftware\jdk\jre\lib\ext\jaccess.jar;D:\javasoftware\jdk\jre\lib\ext\jfxrt.jar;D:\javasoftware\jdk\jre\lib\ext\localedata.jar;D:\javasoftware\jdk\jre\lib\ext\nashorn.jar;D:\javasoftware\jdk\jre\lib\ext\sunec.jar;D:\javasoftware\jdk\jre\lib\ext\sunjce_provider.jar;D:\javasoftware\jdk\jre\lib\ext\sunmscapi.jar;D:\javasoftware\jdk\jre\lib\ext\sunpkcs11.jar;D:\javasoftware\jdk\jre\lib\ext\zipfs.jar;D:\javasoftware\jdk\jre\lib\javaws.jar;D:\javasoftware\jdk\jre\lib\jce.jar;D:\javasoftware\jdk\jre\lib\jfr.jar;D:\javasoftware\jdk\jre\lib\jfxswt.jar;D:\javasoftware\jdk\jre\lib\jsse.jar;D:\javasoftware\jdk\jre\lib\management-agent.jar;D:\javasoftware\jdk\jre\lib\plugin.jar;D:\javasoftware\jdk\jre\lib\resources.jar;D:\javasoftware\jdk\jre\lib\rt.jar;D:\javaproject\EPSpider\target\classes;C:\Users\Administrator\.m2\repository\org\apache\httpcomponents\httpclient\4.5.12\httpclient-4.5.12.jar;C:\Users\Administrator\.m2\repository\org\apache\httpcomponents\httpcore\4.4.13\httpcore-4.4.13.jar;C:\Users\Administrator\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\Administrator\.m2\repository\commons-codec\commons-codec\1.11\commons-codec-1.11.jar;C:\Users\Administrator\.m2\repository\commons-httpclient\commons-httpclient\3.1\commons-httpclient-3.1.jar;C:\Users\Administrator\.m2\repository\dom4j\dom4j\1.6.1\dom4j-1.6.1.jar;C:\Users\Administrator\.m2\repository\xml-apis\xml-apis\1.0.b2\xml-apis-1.0.b2.jar;C:\Users\Administrator\.m2\repository\org\jsoup\jsoup\1.12.1\jsoup-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(){
}
}
回答
这一行的问题:
System.out.printf("每个公司链接为:%n" + link);
报错原因是link里有一个%F
,不能正常解析format
修正方式是改成这样,正确使用format:
System.out.printf("每个公司链接为:%n%s", link);
或者直接println
System.out.println("每个公司链接为:n" + link);
TNT的回复是没有问题的哈,我自己都试过了,看你评论说还有问题,我猜想你可能看得比较快,没有注意改全
如果你还是用printf
,除了把%n
改为%n%s
,还有一点,很关键啊,就是要把link
作为参数传入printf
里啊,所以人家写的也是很清楚辣,中间是逗号,不是加号,不然加%s
不就是占位符嘛
我估计你是写成了
System.out.printf("每个公司链接为:%n%s" + link);
你只用改这么一点就行啦,改完就没毛病啦,数据都打印出来了
以上是 java 格式化报错 的全部内容, 来源链接: utcz.com/a/65124.html