我有以下HTML
<html>
<head>
<title>test</title>
</head>
<body>
<table>
<caption>table title and/or explanatory text</caption>
<thead>
<tr>
<th>header</th>
</tr>
</thead>
<tbody>
<tr>
<td id=\"test\" width=\"272\"></td>
</tr>
</tbody>
</table>
<a href=\"http://www.google.fi\" style=\"color:black\">Test link</a>
<a href=\"http://www.google.fi\"><img src=\"http://www.google.se/images/nav_logo95.png\" /></a>"
</body>
</html>;
Run Code Online (Sandbox Code Playgroud)
我想找到jsoup的第一个链接,并用文本替换它
Element elem = page.select("a[href=" + link.getUrl() + "]:contains(" + link.getName() + ")").first();
Run Code Online (Sandbox Code Playgroud)
我只能用elem.html("foo")或替换内部HTML 或打印outerHtmlelem.outerHtml()
有谁知道我怎么能做到这一点?
我正在尝试创建一个应用程序来从网站上的多个页面中删除内容.我正在使用JSoup进行连接.这是我的代码:
for (String locale : langList){
sitemapPath = sitemapDomain+"/"+locale+"/"+sitemapName;
try {
Document doc = Jsoup.connect(sitemapPath)
.userAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21")
.timeout(10000)
.get();
Elements element = doc.select("loc");
for (Element urls : element) {
System.out.println(urls.text());
}
} catch (IOException e) {
System.out.println(e);
}
}
Run Code Online (Sandbox Code Playgroud)
一切都在大部分时间都很完美.但是,我希望能够做一些事情.
首先,有时404状态将返回或500状态可能是301.使用下面的代码,它将只打印错误并移动到下一个URL.我希望能够做的是尝试返回所有链接的url状态.如果页面连接打印200,如果不打印相关的状态代码.
其次我有时会发现这个错误"java.net.SocketTimeoutException:read timed out"我可以增加我的超时但是我宁愿尝试连接3次,在第3次失败时我想将URL添加到"失败"数组所以我可以在将来重试失败的连接.
知识比我更多的人可以帮助我吗?
我需要将jsoup元素映射回源HTML中的特定字符偏移量.换句话说,如果我有这样的HTML:
Hello <br/> World
Run Code Online (Sandbox Code Playgroud)
我需要知道"Hello"从偏移量0开始,长度为6个字符,<br/>从偏移量6开始,长度为5个字符等.
我在Element javadoc中找不到返回此信息的getter.可以检索吗?
我遇到了使用jsoup的问题.我无法匹配<div id="shout_132684">那些正在改变的数字.我应该如何匹配?
Elements content = doc.select("div:matches(id=\"shout_.+?\")");
Run Code Online (Sandbox Code Playgroud)
不行.
我经常要在我工作的网站上运行测试用例.大多数时候我只需要检查网站上是否存在元素,或者我必须抓取网站的一些数据.到目前为止,我一直在使用Jsoup来完成这项工作.
我最近被介绍给Selenium Webdriver.我一直在阅读它,但我只想弄清楚何时最好使用它.在像我这样的情况下,检查页面上是否存在元素或者抓取数据我认为我仍然会更好地使用Jsoup?Selenium最适合填写表单并点击网站上的按钮?
我正在使用Jsoup从网站获取HTML.我正在使用
String url="http://www.example.com";
Document doc=Jsoup.connect(url).get();
Run Code Online (Sandbox Code Playgroud)
这个代码来获取HTML.但是当我在这样的链接中使用一些土耳其字母时;
String url="http://www.example.com/?q=Türkçe";
Document doc=Jsoup.connect(url).get();
Run Code Online (Sandbox Code Playgroud)
Jsoup发送这样的请求: "http://www.example.com/?q=Trke"
所以我无法得到正确的结果.我怎么解决这个问题?
我有一个学校的项目来解析Web代码并像数据库一样使用它.当我试图从(https://www.marathonbet.com/en/betting/Football/)中删除数据时,我没有得到所有这些?
这是我的代码:
Document doc = Jsoup.connect("https://www.marathonbet.com/en/betting/Football/").get();
Elements newsHeadlines = doc.select("div#container_EVENTS");
for (Element e: newsHeadlines.select("[id^=container_]")) {
System.out.println(e.select("[class^=block-events-head]").first().text());
System.out.println(e.select("[class^=foot-market]").select("[class^=event]").text());
}
Run Code Online (Sandbox Code Playgroud)
得到的结果(这是显示的联赛的最后一个):
Football. Friendlies. Internationals All bets Main bets
1. USA 2. Mexico 16 Apr 01:30 +124 7/5 23/10 111/50 +124
Run Code Online (Sandbox Code Playgroud)
在她之上显示所有联赛.
为什么我没有获得完整数据?感谢您的时间!
我正在寻找一些网络抓取/抓取,我做了一些研究并发现了Jsoup.我遇到的唯一问题是进口.我看过的视频和我见过的例子都有我的匹配代码,但无论出于何种原因,他们的进口都有效,而我的进口却没有.我的所有四个都给出错误:导入org.jsoup无法解决.请帮忙.
package com.stackoverflow.q2835505;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class test {
public static void main(String[] args) throws Exception {
String url = "http://stackoverflow.com/questions/2835505";
Document document = Jsoup.connect(url).get();
String question = document.select("#question .post-text").text();
System.out.println("Question: " + question);
Elements answerers = document.select("#answers .user-details a");
for (Element answerer : answerers) {
System.out.println("Answerer: " + answerer.text());
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在做一些用Selenium制作的网页抓取(所以使用这个不是问题).当我必须识别一个元素(即:获取src属性)时,我应该使用Selenium内部选择引擎还是应该使用Jsoup(这更容易).所以问题是:使用Jsoup是如此表现相当可观?我应该尽可能多地使用硒吗?谢谢
我有一个这样的列表jsoup:
Elements tbody = new Elements();
Run Code Online (Sandbox Code Playgroud)
tbody可能看起来像这样(----分隔tbody列表中的元素):
<td>
<div data-emission="56b2140adb6da7bf3cbf6228" class="mainCell">
<a href="/tv/weather-country-12457/"> <span class="left">16:00</span>
<div>
<p>Weather - country</p>
</div> </a>
</div>
<div data-emission="56b2140adb6da7bf3cbf6237" class="mainCell shows pending">
<a href="/shows/that's-70-show-550347/epi1201/"> <span class="left">16:10</span>
<div>
<p>That's 70 show</p>
<span class="info">epi.?1201, Show</span>
</div> <p class="onAir"> <span>Pending</span> <u></u> <u style="width: 5%"></u> </p> </a>
</div> </td>
---------------------------------------------------------------------------
<td>
<div data-emission="56b23876db6da7bf3cbf6588" class="mainCell pending">
<a href="/tv/weather-563806/"> <span class="left">16:10</span>
<div>
<p>Weather</p>
</div> <p class="onAir"> <span>Pending</span> <u></u> <u style="width: 51%"></u> </p> </a>
</div> …Run Code Online (Sandbox Code Playgroud) jsoup ×10
java ×9
selenium ×2
connection ×1
import ×1
performance ×1
turkish ×1
web-scraping ×1