我一直在编写一些代码来从Java中的某些页面获取一些数据,Jsoup是最好的库.但是,不幸的是我必须将整个代码移植到C/C++.但是我找不到在c ++上使用的任何体面的html解析器.是否有类似Jsoup的C++库或者如何实现类似的结果?
[目前我正在使用Curl获取页面的来源并漫游互联网以查找html解析器]
使用Jsoup我尝试解析给定的html内容.在Jsoup.parse()之后,html输出将html,head和body标记附加到输入.我只是想忽略这些.
样本输入:
<p><b>This <i>is</i></b> <i>my sentence</i> of text.</p>
Run Code Online (Sandbox Code Playgroud)
Java代码:
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class HTMLParse {
public static void main(String args[]) throws IOException {
try{
File input = new File("/ab.html");
String html = FileUtils.readFileToString(input, null);
Document doc = Jsoup.parseBodyFragment(html);
doc.outputSettings().prettyPrint(false);
System.out.println(doc.html());
}
catch(Exception e){
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
实际产量:
<html><head></head><body><p><b>This <i>is</i></b> <i>my sentence</i> of text.</p>
</body></html>
Run Code Online (Sandbox Code Playgroud)
预期产出:
<p><b>This <i>is</i></b> <i>my sentence</i> of text.</p>
Run Code Online (Sandbox Code Playgroud)
请帮忙.
我正在使用jsoup来抓取一些HTML数据,而且效果很好.现在我需要提取一些JSON内容(只有JSON,而不是HTML).我可以使用jsoup轻松完成此操作,还是必须使用其他方法?jsoup执行的解析是对JSON数据进行编码,因此它无法使用Gson正确解析.
谢谢!
在大多数情况下,使用jsoup解析XML没有问题.但是,如果<link>
XML文档中有标记,则jsoup将更<link>some text here</link>
改为<link />some text here
.这使得无法<link>
使用CSS选择器在标记内提取文本.
那么如何防止jsoup"清理" <link>
标签呢?
我正在尝试使用此网站在欢迎页面上收集我的用户名以学习Jsoup和Android.使用以下代码
Connection.Response res = Jsoup.connect("http://www.mikeportnoy.com/forum/login.aspx")
.data("ctl00$ContentPlaceHolder1$ctl00$Login1$UserName", "username", "ctl00$ContentPlaceHolder1$ctl00$Login1$Password", "password")
.method(Method.POST)
.execute();
String sessionId = res.cookie(".ASPXAUTH");
Document doc2 = Jsoup.connect("http://www.mikeportnoy.com/forum/default.aspx")
.cookie(".ASPXAUTH", sessionId)
.get();
Run Code Online (Sandbox Code Playgroud)
我的cookie(.ASPXAUTH)总是以NULL结尾.如果我在webbrowser中删除此cookie,我将失去连接.所以我相信这是正确的cookie.另外,如果我改变代码
.cookie(".ASPXAUTH", "jkaldfjjfasldjf") Using the correct values of course
Run Code Online (Sandbox Code Playgroud)
我可以从这个页面抓取我的登录名.这也让我觉得我有正确的cookie.那么,为什么我的饼干出现了?我的用户名和密码名称字段是否不正确?别的什么?
谢谢.
我正在玩Selenium和PhantomJS.我正在尝试从网页中绘制所有元素.当我检索一些网页然后我尝试获取任何web元素的位置时,当我在我的代码上选择web元素时,我收到此错误:
org.openqa.selenium.WebDriverException: {"errorMessage":"Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: \"script-src assets-cdn.github.com\".\n","request":{"headers":{"Accept-Encoding":"gzip,deflate","Cache-Control":"no-cache","Connection":"Keep-Alive","Host":"localhost:26310","User-Agent":"Apache-HttpClient/4.4.1 (Java/1.8.0_45)"},"httpVersion":"1.1","method":"GET","url":"/location","urlParsed":{"anchor":"","query":"","file":"location","directory":"/","path":"/location","relative":"/location","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/location","queryKey":{},"chunks":["location"]},"urlOriginal":"/session/77ee7e10-1077-11e6-9f8f-1f750417371e/element/%3Awdc%3A1462201609875/location"}}
Run Code Online (Sandbox Code Playgroud)
我的代码如下.我正在使用Jsoup来获取元素,因为使用selenium我经常遇到与前面提到的相同的错误:
WebDriver driver = new PhantomJSDriver();
driver.manage().window().setSize(new Dimension(1366, 768));
driver.get(URL);
Document doc = Jsoup.parse(driver.getPageSource());
Elements e1 = doc.body().getAllElements();
ArrayList<String> tags = new ArrayList<>();
for (Element e : e1) {
if (tags.indexOf(e.tagName()) == -1) {
tags.add(e.tagName());
List<WebElement> query = null;
if (driver.findElements(By.tagName(e.tagName())).size() < 1) {
continue;
}
try {
query = driver.findElements(By.tagName(e.tagName()));
} …
Run Code Online (Sandbox Code Playgroud) 我尝试使用JSoup登录facebook,当使用控制台应用程序或Tomcat服务器webapp进行测试时,它运行良好:
String userAgent = BrowserVersion.FIREFOX_38.getUserAgent();
Connection conn1 = Jsoup.connect("https://m.facebook.com");
Document document = conn1.userAgent(userAgent).execute().parse();
Element form = document.select("form").get(0);
Elements inputs = form.select("input");
Connection conn2 = Jsoup.connect(form.absUrl("action")).userAgent(userAgent);
for (Element input : inputs) {
conn2.data(input.attr("name"), input.val());
}
conn2.data("email", "mail@gmail.com").data("pass", "mypass");
Connection.Response response1 = conn2.method(Connection.Method.POST).execute();
System.out.println(response1.url());
Document doc1 = response1.parse();
Elements h3 = doc1.select("h3");
for (Element element : h3) {
System.out.println(element.text());
}
Run Code Online (Sandbox Code Playgroud)
response1.url()是https://m.facebook.com/home.php?_rdr
但是,当我尝试使用Google App Engine应用程序时,它无法登录,但会显示包含"您必须先登录"的消息的页面(我猜这个请求尝试访问其他网址)
我更改密码使其不正确,它显示错误的密码页面.所以我认为使用正确的密码,请求会转到正确的页面.但为什么使用正确的密码,它会重定向到其他页面?我不认为控制台应用程序和GAE之间有任何不同的东西使请求不同!
我试图通过标签"c2 l n"获取此类的值.
<td class="c2 l n"><span class"generic">0,63</span></td>
Run Code Online (Sandbox Code Playgroud)
在Jsoup我试过这个:
String value="c2 l n";
Elements Stock_Data_Change = doc.getElementsByClass(value);
Run Code Online (Sandbox Code Playgroud)
但它一直空着......它与其他类名如"ju.l"一起正常工作,它似乎错过了空白.有人知道解决方案吗?
我不能用
或.trim()
等删除它!我不明白.
我甚至在Stackoverflow上找到了尝试,.replace(" ", "")
但都没有工作.
我试过这个:
System.out.println( "'"+fields.get(6).text().replace("\\u00a0", "")+"'" ); //'94,00 '
System.out.println( "'"+fields.get(6).text().replace(" ", "")+"'" ); //'94,00 '
System.out.println( "'"+fields.get(6).text().trim()+"'"); //'94,00 '
System.out.println( "'"+fields.get(6).html().replace(" ", "")+"'"); //'94,00' works
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚为什么我无法删除空白区域\\u00a0
.
我发现了几个有类似问题和有价值答案的主题,但我仍然在努力解决这个问题:
我想用Jsoup解析一些html,所以我可以替换,例如,
"changeme"
Run Code Online (Sandbox Code Playgroud)
同
<changed>changeme</changed>
Run Code Online (Sandbox Code Playgroud)
,但只有当它出现在html的文本部分时,否则,如果它是标记的一部分.所以,从这个html开始:
<body>
<p><a href="http://changeme.html">test changeme app</a></p>
</BODY>
</HTML>
Run Code Online (Sandbox Code Playgroud)
我想谈谈这个:
<body>
<p><a href="http://changeme.html">test <changed>changeme</changed> app</a></p>
</BODY>
</HTML>
Run Code Online (Sandbox Code Playgroud)
我尝试了几种方法,这一方法让我更接近预期的结果:
Document doc = null;
try {
doc = Jsoup.parse(new File("tmp1450348256397.txt"), "UTF-8");
} catch (Exception ex) {
}
Elements els = doc.body().getAllElements();
for (Element e : els) {
if (e.text().contains("changeme")) {
e.html(e.html().replaceAll("changeme","<changed>changeme</changed>"));
}
}
html = doc.toString();
System.out.println(html);
Run Code Online (Sandbox Code Playgroud)
但是通过这种方法,我发现了两个问题:
<body>
<p><a href="http://<changed>changeme</changed> .html">test
<changed>
changeme
</changed>
app</a></p>
</BODY>
</HTML>
Run Code Online (Sandbox Code Playgroud)
在我引入的新元素之前和之后插入换行符.这不是一个真正的问题,因为如果我使用#change#来进行替换并且在doc.toString()之后我将它们删除它们,我将它们再次替换为所需的值(使用<>).
真正的问题:href中的URL已被修改,我不希望它发生.
想法?谢谢.