今天我开始“玩”了JSoup。我想知道有多强大JSoup,所以我寻找了一个包含很多元素的网页,并试图检索所有元素。我找到了我要找的东西:http : //www.top1000.ie/companies。
这是一个包含许多相似元素 (1000) 的列表(列表中的每个公司)。只需更改其中的文本,以便我尝试检索的是该文本,但我只能获取前 20 个元素,而不能获取其余元素。
这是我的简单代码:
package retrieveInfo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Retrieve {
public static void main(String[] args) throws Exception{
String url = "http://www.top1000.ie/companies";
Document document = Jsoup.connect(url)
.userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0")
.timeout(1000*5)
.get();
Elements companies = document.body().select(".content .name");
for (Element company : companies) {
System.out.println("Company: " + company.text());
}
}
}
Run Code Online (Sandbox Code Playgroud)
我认为可能是页面没有时间加载,所以这就是我.timeout(1000*5)等待 5 秒但我只能获取列表的前 20 个元素的原因。 …
有一堆html元素如下:
<div class="abcdefghijk">
<p>a</p>
<p>b</p>
<p>c</p>
<p>d</p>
<p>e</p>
<p>f</p>
<p>h</p>
<p>i</p>
<p>j</p>
<p>k</p>
</div>
Run Code Online (Sandbox Code Playgroud)
我想选择前 5 个<p>元素。请帮忙!
我正在创建一个应用程序,因为如果用户输入网站 url ,它将自动转换为网站缩略图。我建立了一个 http 连接并得到了html 页面作为响应。其中有一个带有图像/缩略图的元标记。
<meta property="og:image" itemprop="image primaryImageOfPage" content="https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded" />
Run Code Online (Sandbox Code Playgroud)
所以我的问题是如何提取该图像。
我试过Jsoup,我无法从中提取图像。
极客请帮帮我
当尝试解析网站的 html 页面时,它会因错误而崩溃:
java.io.IOException:标记已失效。
我的部分代码:
String xml = xxxxxx;
try {
Document document = Jsoup.connect(xml).maxBodySize(1024*1024*10)
.timeout(0).ignoreContentType(true)
.parser(Parser.xmlParser()).get();
Elements elements = document.body().select("td.hotv_text:eq(0)");
for (Element element : elements) {
Element element1 = element.select("a[href].hotv_text").first();
hashMap.put(element.text(), element1.attr("abs:href"));
}
} catch (HttpStatusException ex) {
Log.i("GyWueInetSvc", "Exception while JSoup connect:" + xml +" cause:"+ ex.getMessage());
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Socket timeout: " + e.getMessage(), e);
}
Run Code Online (Sandbox Code Playgroud)
我要解析的网站大小约为 2MB。当我调试代码时,我看到在 java 包ConstrainableInputStream.java方法中:
public void reset() throws IOException {
super.reset();remaining = maxSize - …Run Code Online (Sandbox Code Playgroud) 我想比较标题中提到的三种实现的性能,我写了一个小 JAVA 程序来帮助我做到这一点。main 方法包含三个测试块,每个块看起来像这样:
nb=0; time=0;
for (int i = 0; i < 7; i++) {
double v = methodX(url);
if(v>0){
nb++;
time+=v;
}
}
if(nb==0) nb=1;
System.out.println("HttpClient : "+(time/ ((double) nb))+". Tries "+nb+"/7");
Run Code Online (Sandbox Code Playgroud)
变量nb用于避免失败的请求。现在方法methodX是以下之一:
private static double testWithNativeHUC(String url){
try {
HttpURLConnection httpURLConnection= (HttpURLConnection) new URL(url).openConnection();
httpURLConnection.addRequestProperty("User-Agent", UA);
long before = System.currentTimeMillis();
BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
while (bufferedReader.readLine()!=null);
return System.currentTimeMillis()-before;
} catch (IOException e) {
e.printStackTrace();
return -1;
}
}
private static double testWithHC(String …Run Code Online (Sandbox Code Playgroud) 我最近使用新的 Jsoup 依赖项版本 1.15.3 更新了 pom.xml,从那时起我收到了“NoClassDefFoundError: org/jsoup/safety/Whitelist”。我可以使用其他版本来解决此问题吗?
我有
元素列表= doc.select("table").get(0);
有时没有桌子,但我仍然这样做,所以我
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
Run Code Online (Sandbox Code Playgroud)
如何摆脱这个例外?没有尺寸,因为这是元素
我喜欢这样的页面:
www.foo1.bar
www.foo2.bar
www.foo3.bar
.
.
www.foo100.bar
Run Code Online (Sandbox Code Playgroud)
我正在使用库jsoup并使用Thread同时连接到每个页面:
Thread matchThread = new Thread(task);
matchThread.start();
Run Code Online (Sandbox Code Playgroud)
每个任务,连接到这样的页面,并解析HTML:
Jsoup.connect("www.fooX.bar").timeout(0).get();
Run Code Online (Sandbox Code Playgroud)
获得大量的这些例外:
java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at sun.net.NetworkClient.doConnect(NetworkClient.java:158)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:388)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:523)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:227)
at sun.net.www.http.HttpClient.New(HttpClient.java:300)
at sun.net.www.http.HttpClient.New(HttpClient.java:317)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:970)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:911)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:836)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:404)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:391)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:157)
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:146)
Run Code Online (Sandbox Code Playgroud)
jsoup是否只允许同时使用1个线程?或者我做错了什么?有关如何更快地连接到我的页面的任何建议,因为逐个进行需要很长时间.
编辑:
所有700个线程都使用这种方法,也许这就是问题所在.这个方法可以处理这个数量的线程还是单例?
private static Document connect(String url) {
Document doc = null;
try {
doc = Jsoup.connect(url).timeout(0).get(); …Run Code Online (Sandbox Code Playgroud) 我试图用Jsoup废弃网页.Jsoup似乎没有<input像Chrome那样捕获元素.
它缺少这些值:
<input type=?"hidden" id=?"fileId" value=?"3168935269">
<input type=?"hidden" id=?"secondsLeft" value=?"20">??
Run Code Online (Sandbox Code Playgroud)
使用Jsoup我提取了这些元素:
<input type="hidden" class="jsItemDirId" value="yRg1N-QP" />
<input type="hidden" class="jsItemFileId" value="i-EbooI0" />
<input type="hidden" id="fbAppId" value="255519317820035" />
<input type="hidden" id="sPrefix" value="http://search.4shared.com" />
<input type="hidden" class="sLink file" value="/q/CCAD/1" />
<input type="hidden" class="sLink video" value="/q/CCQD/1/video" />
<input type="hidden" class="sLink music" value="/q/CCQD/1/music" />
<input type="hidden" class="sLink photo" value="/q/CCQD/1/photo" />
<input type="hidden" class="sLink games" value="/q/CCQD/1/game" />
<input type="hidden" class="sLink book" value="/q/CCQD/1/books_office" />
<input type="hidden" class="sLink featured_videos" value="/q/CCQD/1/video" />
<input type="hidden" id="sBreadcrumbsPhrase" value="Searching" /> …Run Code Online (Sandbox Code Playgroud) 我有:
Document doc = Jsoup.connect("http://example.com").get();
doc.select(".main li").last().remove();
Run Code Online (Sandbox Code Playgroud)
如果.main li存在,这工作正常,但如果它不存在,那么我的应用程序崩溃.
我如何检查是否.main li存在?
jsoup ×10
java ×8
html ×3
parsing ×2
android ×1
callable ×1
html-parsing ×1
httpclient ×1
ioexception ×1
optimization ×1
pom.xml ×1
runnable ×1