onWebView检查网页中文
问题:要检查网页中的一段文本:
开始我是这样写的:
private final static String SPECIFIED_TEXT = "这个是一段中文"; onWebView().check(webContent(containingTextInNode(SPECIFIED_TEXT )));
然后直接报错了 从adb logcat看到的结果是网页中文显示为乱码,尝试输出了一下每个中文的长度都是3;但是可以看到网页结构和数据 可以看到数据文本数据是在<p></p> <h2></h2> 里面
不死心啊: 把检查代码全部从库里面拷贝出来 改成
onWebView().check(userWebContent(containingTextInNode(SPECIFIED_TEXT , "p")));
/**
* 为了把网页输出出来
* @param xml
*/
public static void logall(String xml) {
if (xml.length() > 4000) {
for (int i = 0; i < xml.length(); i += 4000) {
if (i + 4000 < xml.length())
Log.i(TAG, xml.substring(i, i + 4000));
else
Log.i(TAG, xml.substring(i, xml.length()));
}
} else
Log.i(TAG, xml);
}
/**
* A WebAssertion which asserts that the document is matched by th provided matcher.
*/
public static WebAssertion<Document> userWebContent(final Matcher<Document> domMatcher) {
checkNotNull(domMatcher);
return webMatches(transform(script("return document.documentElement.outerHTML;"),
new TransformingAtom.Transformer<Evaluation, Document>() {
@Override
public Document apply(Evaluation eval) {
if (eval.getValue() instanceof String) {
try {
// Logall( "eval.getValue() " + (String)eval.getValue()); //这个地方能完整输出网页数据-不乱码的
// return TagSoupDocumentParser.newInstance().parse((String) eval.getValue()); //这个方法不能显示中文
org.jsoup.helper.W3CDom w3cDom = new W3CDom();
org.jsoup.nodes.Document doc = Jsoup.parseBodyFragment((String) eval.getValue()); //org.jsoup.nodes.Document无法转换为org.w3c.dom.Document
return w3cDom.fromJsoup(doc);
} catch (Exception se) {
throw new RuntimeException("Parse failed: " + eval.getValue(), se);
}
}
throw new RuntimeException("Value should have been a string: " + eval);
}
}), domMatcher,
new WebViewAssertions.ResultDescriber<Document>() {
@Override
public String apply(Document document) {
try {
DOMSource docSource = new DOMSource(document);
Transformer tf = TransformerFactory.newInstance().newTransformer();
StringWriter writer = new StringWriter();
StreamResult streamer = new StreamResult(writer);
tf.transform(docSource, streamer);
return writer.toString();
} catch (TransformerException e) {
return "Could not transform!!!" + e;
}
}
});
}
/**
* Returns a matcher that matches Documents that have a body containing the given test.
*/
public static Matcher<Document> containingTextInNode(String text, final String nodeNme) {
checkNotNull(text);
return withNodeName(withTextContent(containsString(text)), nodeNme);
} /**
* Returns a matcher that matches {@link Document}s with body that matches the given matcher.
*/
public static Matcher<Document> withNodeName(final Matcher<Element> bodyMatcher, final String nodeNme) {
checkNotNull(bodyMatcher);
return new TypeSafeMatcher<Document>() {
@Override
public void describeTo(Description description) {
description.appendText("with NodeName: ");
bodyMatcher.describeTo(description);
} @Override
public boolean matchesSafely(Document document) {
NodeList nodeList = document.getElementsByTagName(nodeNme);
if (nodeList.getLength() == 0) {
return false;
}
// showNode(nodeList, "");
for (int i = 0; i < nodeList.getLength(); i++) {
if (bodyMatcher.matches(nodeList.item(i))) {
return true;
}
}
return false;
}
};
} /**
* 将节点集放入已排序的集合中时,W3C 将其称为 NodeList;可以按从零开始的索引检索数据。
*
* @param nodeList
* @param path
*/
public static void showNode(NodeList nodeList, String path) {
for (int i = 0; i < nodeList.getLength(); i++) {
Node mobilePhone = nodeList.item(i);
int destination = mobilePhone.getTextContent().length();
NodeList mobileNodeList = mobilePhone.getChildNodes();
if (mobileNodeList.getLength() > 0) {
showNode(mobileNodeList, path + "-" + mobilePhone.getNodeName());
} else {
Log.i(TAG, path + "-" + mobilePhone.getNodeName() + ":" + destination + " " + mobilePhone.getTextContent()); //无子节点了就显示
}
}
}
//上面我们用了jsoup库,gradle里面增加库依赖
//还要注意Document转换
dependencies {
compile 'org.jsoup:jsoup:1.9.2'
androidTestCompile 'org.jsoup:jsoup:1.9.2' //测试用这个
}
至此可以顺利检查到网页中的中文啦,代码比较乱,将就着先用吧
onWebView检查网页中文的更多相关文章
- 解决Ubuntu下Chrome浏览器网页中文字体混乱
在Ubuntu下使用Chrome浏览器时碰到了网页中文字体混乱的现象: 黑体和楷体混杂,看起来非常不美观. 这是由于许多网页并没有指定字体,然后浏览器将调用系统默认字体配置. 首先,安装文泉驿字体: ...
- 【转载】 IE/Firefox每次刷新时自动检查网页更新,无需手动清空缓存的设置方法
[参考了别人的文章]我们做技术,经常在写页面的时候需要多次刷新测试,可是浏览器都有自己的 缓存机制,一般CSS和图片都会被缓存在本地,这样我们修改的CSS就看不到效果 了,每次都去清空缓存,再刷新看效 ...
- IE/Firefox每次刷新时自动检查网页更新,无需手动清空缓存的设置方法
浏览器都有自己的 缓存机制,一般CSS和图片都会被缓存在本地,这样我们修改的CSS就看不到效果 了,每次都去清空缓存,再刷新看效果,这样操作太麻烦了.在IE下我们可以直接 去修改internet选项/ ...
- 使用notepad++学习python爬虫,print网页中文乱码问题
今天学习使用python爬虫的时候发现爬到的网页中文会乱码,一直网上搜索解决办法,一个一个试验过去,发现还是乱码,然后我就开始使用其它方法测试,用python自带的编辑器打开是正常的,发现是notep ...
- 爬虫 Http请求,urllib2获取数据,第三方库requests获取数据,BeautifulSoup处理数据,使用Chrome浏览器开发者工具显示检查网页源代码,json模块的dumps,loads,dump,load方法介绍
爬虫 Http请求,urllib2获取数据,第三方库requests获取数据,BeautifulSoup处理数据,使用Chrome浏览器开发者工具显示检查网页源代码,json模块的dumps,load ...
- [Python] - 使用chardet检查网页编码格式时发现的问题
最近在使用chardet检查网页编码格式时发现如下问题: 用urllib打开网页再检查编码格式和用urllib2打开网页检查编码格式结果不一样,所以urllib2打开可能导致问题,需要关注. 查看了相 ...
- node爬虫之gbk网页中文乱码解决方案
之前在用 node 做爬虫时碰到的中文乱码问题一直没有解决,今天整理下备忘.(PS:网上一些解决方案都已经不行了) 中文乱码具体是指用 node 请求 gbk 编码的网页,无法正确获取网页中的中文(需 ...
- mac下网页中文字体优化
最近某人吐槽某门户网站在mac下chrome字体超丑,然后发现虽然现在mac用户越来越多,但是大家依然无视mac下的字体差异,于是研究了下mac下网页中的中文字体,和大家分享. 看了一遍国内各大门户和 ...
- Font-Spider 一个神奇的网页中文字体工具,就是这么任性
文章摘要: 1>> font-spider 字体神奇 由于活动项目推广的需要,页面需要用到一些漂亮好看的字体,example : 邯郸-韩鹏毛遂体.ttf. 方正喵呜.ttf 我看 ...
随机推荐
- 波动数列 神奇的dp
问题描述 观察这个数列: 1 3 0 2 -1 1 -2 ... 这个数列中后一项总是比前一项增加2或者减少3. 栋栋对这种数列很好奇,他想知道长度为 n 和为 s 而且后一项总是比前一项增加a或者减 ...
- Servlet 中 RequestDispacher 请求与分发
RequestDispacher 请求与分发使用HttpServletRequest的getRequestDispatcher()方法取得 Login.java页面 package control; ...
- 【bzoj3894】文理分科 网络流最小割
原文地址:http://www.cnblogs.com/GXZlegend 题目描述 文理分科是一件很纠结的事情!(虽然看到这个题目的人肯定都没有纠结过) 小P所在的班级要进行文理分科.他的班级可以用 ...
- 【Luogu】P4284概率充电器(概率树形DP)
题目链接 这题好神啊…… 设f[i]为i没电的概率,初始化$f[i]=1-q[i]$ 之后x的电有三个来源: 1.x自己有电 2.x的儿子给它传来了电 3.x的父亲给它传来了电 对于2和3操作分别做一 ...
- VS2013下配置OpenCV 3.0.0 &&& VS2013下配置Opencv2.4.9
最近做图像需要用到Matlab和OpenCV,一些东西真的是要深入的研究进去才会有所发现,但Matlab和C++都不是我擅长的语言,所以要很加油很加油才行啊!! 步入正题. 1. 环境:Win7 6 ...
- CF10D LCIS (动态规划)
题目链接 Solution 动态规划. 令 \(f_{i,j}\) 表示 \(a\) 数组前 \(i\) 个和 \(b\) 数组前 \(j\) 所得的最长的 LCIS . 转移很好想: \(a_i!= ...
- VMware HA 特性
关键特性1.自动检测服务器故障.VMware HA 自动监控物理服务器的可用性.VMware HA 可检测物理服务器故障,并且无需人工干预即可重新启动资源池中其他物理服务器上的新虚拟机.2.自动检测操 ...
- python装饰器(整理版)
Python中函数有一个装饰器的概念,今天,看核心编程中的函数一章的时候接触到了这个概念,炸一看来,讲的说明真实不好明白.于是写下本篇以示说明,提供给迷糊者.希望能对一些人起到一定的帮助 装饰器的语法 ...
- 实现实体类和Xml相互转化
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...
- 水(NOIP模拟赛Round #10)
题目描述: 小Z有一个长度为的数列.他有次令人窒息的操作,每次操作可以使某个数字或.他当然是希望这些数字的乘积尽量小了.为了简化题目,你只需输出操作完成后的数列即可. ———————————————— ...