我在网页URL中有多个div,我必须解析它们具有相同的类名但不同的名称没有id.
例如.
<div class="answer" style="display: block;" name="yyy" oldblock="block" jQuery1317140119108="11">
Run Code Online (Sandbox Code Playgroud)
和
<div class="answer" style="display: block;" name="xxx" oldblock="block" jQuery1317140119108="11">
Run Code Online (Sandbox Code Playgroud)
我想从只有一个div中选择数据并解析,即(name ="yyy")(div中的内容是<href>每个类不同的链接.
我在Jsoup网页中查找了选择器语法,但无法找到解决它的方法.你可以帮我解决这个问题,或者让我知道我是否遗漏了什么?
我有以下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限制下载的页面/链接的大小,给出类似下面的内容(Scala代码):
val document = Jsoup.connect(theURL).get();
我只想获得给定页面的前几KB,并停止尝试下载.如果有一个非常大的页面(或者theURL是一个不是html的链接,并且是一个大文件),我不想花时间下载其余的.
我的用例是IRC机器人的页面标题snarfer.
奖金问题:
有什么理由Jsoup.connect(theURL).timeout(3000).get();不在大文件上超时吗?如果某人粘贴了一个永无止境的音频流或大型ISO(可以通过在不同的线程中获取URL标题(或使用Scala actor并在那里计时)来解决它,最终会导致机器人ping通),但是当我认为timeout()应该完成相同的最终结果时,对于一个非常简单的机器人来说似乎有些过分了.
我有这个代码,它使用Java中的Jsoup来完成我需要它做的事情
Elements htmlTree = doc.body().select("*");
Elements menuElements = new Elements();
for(Element element : htmlTree) {
if(element.hasClass("header"))
menuElements.add(element);
if(element.hasClass("name"))
menuElements.add(element);
if(element.hasClass("quantity"))
menuElements.add(element);
}
Run Code Online (Sandbox Code Playgroud)
我想做同样的事情,但在Python中使用BeautifulSoup.我试图抓取的HTML示例树如下:
<div class="header"> content </div>
<div class="name"> content </div>
<div class="quantity"> content </div>
<div class="name"> content </div>
<div class="quantity"> content </div>
<div class="header"> content2 </div>
<div class="name"> content2 </div>
<div class="quantity"> content2 </div>
<div class="name"> content2 </div>
<div class="quantity"> content2 </div>
Run Code Online (Sandbox Code Playgroud)
等等
基本上我希望输出保留每个元素的相对位置.我将如何使用Python和BeautifulSoup做到这一点?
编辑:
这是我的python代码(它非常天真),但也许它可以帮助?
output = []
for e in soup :
if e["class"] == "pickmenucolmenucat" : …Run Code Online (Sandbox Code Playgroud) 我经常要在我工作的网站上运行测试用例.大多数时候我只需要检查网站上是否存在元素,或者我必须抓取网站的一些数据.到目前为止,我一直在使用Jsoup来完成这项工作.
我最近被介绍给Selenium Webdriver.我一直在阅读它,但我只想弄清楚何时最好使用它.在像我这样的情况下,检查页面上是否存在元素或者抓取数据我认为我仍然会更好地使用Jsoup?Selenium最适合填写表单并点击网站上的按钮?
我希望在Groovy中开发一个Web爬虫(使用Grails框架和MongoDB数据库),它能够抓取网站,创建站点URL列表及其资源类型,内容,响应时间和涉及的重定向数量.
我正在讨论JSoup vs Crawler4j.我已经阅读了他们基本上做了什么,但我无法理解两者之间的区别.任何人都可以建议哪个更适合上述功能?或者比较两者完全不正确?
谢谢.
我是新与Jsoup,但我不明白为什么我试图获得一个页面时,即使页面是从浏览器访问收到404错误,我不使用任何proxys.我尝试过以下代码:
private static Document connect() {
String url = "http://www.transfermarkt.co.uk/real-madrid/startseite/verein/418";
Document doc = null;
try {
doc = Jsoup.connect(url).get();
} catch (NullPointerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (HttpStatusException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return doc;
}
Run Code Online (Sandbox Code Playgroud)
我收到了异常消息:
org.jsoup.HttpStatusException: HTTP error fetching URL. Status=404, URL=http://www.transfermarkt.co.uk/real-madrid/startseite/verein/418
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:449)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:424)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:178)
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:167)
at ro.pago.ucl2015.UCLWebParser.connect(UCLWebParser.java:27)
at ro.pago.ucl2015.UCLWebParser.main(UCLWebParser.java:16)
Run Code Online (Sandbox Code Playgroud) 我想选择class=""喜欢的元素
<li class="" > </li>
Run Code Online (Sandbox Code Playgroud)
我用了
Elements topProductSecNav = topNavWrapper.select("li[class=]");
Run Code Online (Sandbox Code Playgroud)
但我得到了java.lang.IllegalArgumentException: String must not be empty例外.
我有以下html,使用Jsoup我试图提取p部分中没有任何属性的文本(文本"Some text 2"而不是"Some text 1").
<div id="intro">
<h1 class="some class">
<p id="some_id">
Some text 1
</p>
<p>
Some text 2
</p>
</div>
Run Code Online (Sandbox Code Playgroud)
我尝试使用以下Jsoup表达式:
div[id=intro] > p:not(:has(@*))
Run Code Online (Sandbox Code Playgroud)
但它不起作用.谢谢你的帮助.
我正在使用下面的代码来获取HTML,但我没有获取纯HTML,它包含非转义字符。我正在使用无法解析此HTML的JSOUP解析器。
webview.evaluateJavascript(
"(function() { return ('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>'); })();",
new ValueCallback<String>() {
@Override
public void onReceiveValue(String html) {
}
});
Run Code Online (Sandbox Code Playgroud)
我从上面的代码中获取此html字符串。
"\u003Chtml>\u003Chead>\n \u003Cmeta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n \u003Cmeta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n \u003Clink rel=\"shortcut icon\" href=\"https://www.xyx.com/favicon.ico\" type=\"image/x-icon\">\n \u003Clink rel=\"icon\" href=\"https://www.xyx.com/favicon.ico\" type=\"image/x-icon\">\n \n \u003Ctitle>Page Not Found! : BJSBuzz\u003C/title>\n\n \u003C!-- \n\tOpen Source Social Network (Ossn)/script>\u003C/body>\u003C/html>"
Run Code Online (Sandbox Code Playgroud) jsoup ×10
java ×6
html ×2
html-parsing ×2
web-scraping ×2
android ×1
connection ×1
crawler4j ×1
python ×1
selenium ×1
web-crawler ×1