本文整理汇总了Java中org.jsoup.nodes.Document.getElementsByAttribute方法的典型用法代码示例。如果您正苦于以下问题:Java Document.getElementsByAttribute方法的具体用法?Java Document.getElementsByAttribute怎么用?Java Document.getElementsByAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.jsoup.nodes.Document
的用法示例。
在下文中一共展示了Document.getElementsByAttribute方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: process
import org.jsoup.nodes.Document; //导入方法依赖的package包/类
@Override
public void process(final ExecutionContext executionContext, final Document document)
throws ProcessException {
try {
Elements xkSections = document.getElementsByAttribute(XK_SECTION_ATTR);
if (xkSections != null) {
for (Element xkSectionTag : xkSections) {
Element xkSectionStylesTag = xkSectionTag.getElementsByAttribute(XK_SECTION_STYLES_ATTR).first();
if (xkSectionStylesTag != null) {
String xkSectionStyles = xkSectionStylesTag.attr(XK_SECTION_STYLES_ATTR);
xkSectionTag.addClass(xkSectionStyles.trim());
xkSectionStylesTag.remove();
}
}
}
} catch (Exception e) {
throw new ProcessException(e);
}
}
示例2: call
import org.jsoup.nodes.Document; //导入方法依赖的package包/类
public List<HouseInfo> call() {
lstHouseInfo = new ArrayList<HouseInfo>();
CloseableHttpClient client = HttpClients.createDefault();
Lock lock = new ReentrantLock();
HttpGet get = new HttpGet(url);
//设置请求头
get.setHeader("Accept", "text/html, application/xhtml+xml, image/jxr, */*");
get.setHeader("Accept-Language", "zh-Hans-CN, zh-Hans; q=0.8, en-US; q=0.5, en; q=0.3");
get.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
try{
//获取响应
CloseableHttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
String html = EntityUtils.toString(entity, "UTF-8");
//解析html
Document doc = Jsoup.parse(html);
Elements lists = doc.getElementsByAttribute("logr");
lock.lock();
try{
for (Element list : lists) {
//将关键信息提取出来
HouseInfo houseInfo = new HouseInfo();
String[] houseInfoArray = list.getElementsByTag("h2").first().text().split(" ");
houseInfo.setHouseTitle(list.getElementsByTag("h2").first().text());
houseInfo.setHouseURL("http://" + "bj" + ".58.com" + list.getElementsByTag("a").first().attributes().get("href"));
houseInfo.setMoney(list.getElementsByClass("money").tagName("b").text());
houseInfo.setHouseLocation(houseInfoArray[1]);
lstHouseInfo.add(houseInfo);
}
}finally {
lock.unlock();
}
}catch (IOException e){
System.out.println( e.getMessage());
}
return lstHouseInfo;
}