How to select the url from this code
<div class="main_news_lp_img3"
onclick="location.href='?news_view=77f1f3883c4ceac3'"
style="background-image: url('uploads/resize2/61b96f91b599c754461eca5891a87951.JPG');">
</div>
I want to select url()
content - this part
uploads/resize2/61b96f91b599c754461eca5891a87951.JPG
It is something like this (pseudo) :
Edit :
htmlDocument = Jsoup.connect(HtmlUrl).get();
Elements articles = htmlDocument.select(DIV);
String url = null;
for (Element article : articles) {
Element element = article.select(DIV).first();
if (element.attr(style) != null) {
url = element.attr(style);
}
}
With Jsoup you will not be able to select specific "elements" of a style
attribute. You have to read the whole attribute and then parse the contents yourself:
Document doc = Jsoup.connect("your-url").get()
// select all "div" elements with a class name "main_news_lp_img3"
for (Element el : doc.select("div.main_news_lp_img3")) {
// get the "style" attribute value
String style = el.attr("style");
// parse the url from the attribute
String url = StringUtils.substringBetween(style, "background-image: url('", "')");
// do something with url...
}
Here I use StringUtils.substringBetween
from Apache commons-lang , but you could also use a regular-expression or implement your own substringBetween
method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.