Parsing HTML In iOS Using TFHpple? - iphone

Iam New To iOS .
i Wanna Parse HTML Page Like This :
<div class="main_menu">
<ul class="sf-menu">
<li class="current">Holiday Tours
<ul>
<li>About Us</li>
<li>Mohammed El Decken</li>
<li>Yehia El Decken</li>
</ul>
</li>
</li>
<li>Vacation Packages
<ul>
<li>Slov2Spain</li>
<li>Swiss Alps</li>
<li>Fall Hike</li>
<li>Nile Valley</li>
<li>Sahlala</li>
<li>Safar El Layaly</li>
<li>Cruising Sinai</li>
<li>Siwa Siwa</li>
<li>Silk-Road</li>
<li>French Alps</li>
</ul>
</li>
-> <li>Weekend Trips<ul>
-> <li>Hiking</li>
-> <li>Sandboarding</li>
-> <li>Horse Riding</li>
-> <li>GoCycle</li>
-> <li>Islamic Tour</li>
</ul>
</li>
<li>Reservation </li>
</ul>
</div>
</div>
i wanna Access Weekend trips Where The Arrows And I Cannot Fine XPathQuery To Get Them ?
Any Help Please ?

NSArray *data = [xpath searchWithXPathQuery:#"//li//a//child::text()"];
to search data
for (int i = 0; i < data.count; i++) {
TFHppleElement *element = [data objectAtIndex:i];
NSLog(#"%#", [element content]);
}
to look data inside and you can check how to get you want.
And here is a xPath syntax tutorial page:
http://www.w3schools.com/xpath/xpath_syntax.asp

Related

How can I get list elements with web scraping?

I need to get list elements with web scraping. I can't reach elements one by one, I can get all elements in one string. How can I get list elements using SwiftSoup or any other option?
Here my function:
self.webView.evaluateJavaScript("document.getElementsByTagName('html')[0].innerHTML") { (value, error) in
if error != nil {
print("Err: \(error)")
}else{
//print(value!)
self.innerDetail = value as! String
do {
let html = self.innerDetail
let doc: Document = try SwiftSoup.parse(html)
// BURADA IMAGE URL LERINI ALIRIZ DETAY SAYFALARI ICIN...
let imageLink = try doc.getElementsByClass("img-container")
let src: Elements = try imageLink.select("img[src]")
let imageUrlStringArray: [String?] = src.array().map { try? $0.attr("src").description }
print(imageUrlStringArray) // BUNDA BUTUN DETAY IMAGE URL LERI SAKLANIR..
// BURADA ARABANIN MARKASI MODELI YILI KM VE YAKIT OLARAK CEKILMESI GEREKMEKTEDIR..
// ONCE FIYATI TABIKI..
let priceMainClass = try doc.getElementsByClass("price")
print(try priceMainClass.text()) // BU FIYATTIR..
// BURDA COK FAZLA DATA GELIYOR VE LISTE SEKLINDELER..
let detailClass = try doc.getElementsByClass("classified-info-list").first()
print(try detailClass?.html())
print(try detailClass?.text())
let detailFeatures = try detailClass?.text()
//print(detailFeatures)
//self.detailFeaturesArr = detailFeatures?.components(separatedBy: " ") as! [String]
} catch {
print("err")}
}
In detailClass?.text() I can get data but it is one string. In detailClass?.html() have list which I want to get data from there.
Here list data detailClass?.html():
Optional("<li> <strong>Fiyat</strong> <span class=\"price\"> 77.500 TL<input id=\"priceHistoryFlag\" type=\"hidden\" value=\"\" autocomplete=\"off\"> \n <!-- ngIf: hasPriceHistory --> \n <!-- ngIf: hasPriceHistory --> </span> </li> \n<li> <strong> İlan Tarihi</strong> <span> 01 Ekim 2020</span> </li> \n<li> <strong>İlan No</strong> <span class=\"classifiedId\" id=\"classifiedId\">865620915</span> </li> \n<li> <strong>Marka</strong> <span>Volvo </span> </li> \n<li> <strong>Seri</strong> <span>S40 </span> </li> \n<li> <strong>Model</strong> <span>2.0 T </span> </li> \n<li> <strong>Yıl</strong> <span class=\"\"> 1999</span> </li> \n<li> <strong>Yakıt</strong> <span class=\"\"> Benzin & LPG</span> </li> \n<li> <strong>Vites</strong> <span class=\"\"> Otomatik</span> </li> \n<li> <strong>KM</strong> <span class=\"\"> 178.000</span> </li> \n<li> <strong>Kasa Tipi</strong> <span class=\"\"> Sedan</span> </li> \n<li> <strong>Motor Gücü</strong> <span class=\"\"> 160 hp</span> </li> \n<li> <strong>Motor Hacmi</strong> <span class=\"\"> 1948 cc</span> </li> \n<li> <strong>Çekiş</strong> <span class=\"\"> Önden Çekiş</span> </li> \n<li> <strong>Renk</strong> <span class=\"\"> Gümüş Gri</span> </li> \n<li> <strong>Garanti</strong> <span class=\"\"> Hayır</span> </li> \n<li> <strong>Plaka / Uyruk</strong> <span class=\"\"> Türkiye (TR) Plakalı</span> </li> \n<li> <strong>Kimden</strong> <span class=\"fromOwner\"> Sahibinden</span> </li> \n<li> <strong>Görüntülü Arama İle Görülebilir</strong> <span class=\"\"> Evet</span> </li> \n<li> <strong>Takas</strong> <span> Hayır </span> </li> \n<li> <strong>Durumu</strong> <span> İkinci El </span> </li> \n<li class=\"hiddenAttributes\"> <input type=\"hidden\" autocomplete=\"off\" class=\"classifiedAttr\" id=\"attrClassifiedId\" value=\"865620915\"> <input type=\"hidden\" autocomplete=\"off\" class=\"classifiedAttr\" id=\"attrIsShipping\" value=\"false\"> </li>")
Sorry about my english. I hope it will be understandable.
I solve problem added code below. I found answer at python question here: How to get a list of the <li> elements in an <ul> with Selenium using Python?
Here my code:
// BURDA COK FAZLA DATA GELIYOR VE LISTE SEKLINDELER..
let detailClass = try doc.getElementsByClass("classified-info-list").first()
let listItems = try detailClass?.getElementsByTag("li")
for j in try listItems!{
let text = try j.text()
print(text)
}

Group and display data

Here is my data
data = [
{ category : "Cat1"},
{ category : "Cat2"},
{ category : "Cat3"},
{ category : "Cat4"},
{ category : "Cat5"},
{ category : "Cat6"}]
Let suppose i have it in a collection named myData
What i want is to group and display my data in group of 2.
Then i display it in a navbar (in a dropdown in fact) like this
<ul>
{{#each group}}
<li class="col-md-2">
<ul>
{{#each categories}}
<li>{{category}}</li>
{{/each}}
</ul>
{{/each}}
<ul>
What i am asking is how to group the data in my helpers or in mongodb so that i could get this result.
I'm not 100% clear what you mean by "group", but assuming you are using Boostrap navbar dropdowns, you could group them with separators:
{{#each categories}}
<li>{{category}}</li>
{{#if doSeparator #index}}
<li role="separator" class="divider"></li>
{{/if}
{{/each}}
and the doSeparator helper goes in your .js file:
doSeparator( index ) {
return (index % 2);
}
If on the other hand you want submenus for each group you will need to reorganize your array in two levels.
Another approach could be:
<ul>
{{#each groups}}
<li>
<ul>
{{#each this}}
<li>{{category}}</li>
{{/each}}
</ul>
</li>
{{/each}}
</ul>
Then in your Template helper:
import { Template } from 'meteor/templating';
import chunk from 'lodash/chunk';
import { myData } from '/imports/api/mydata/collection';
import './main.html';
Template.someTemplate.helpers({
groups() {
return chunk(myData.find().fetch(), 2);
},
});
This uses lodash's chunk function to split the returned array into groupings of 2 items (so you'll want to meteor npm install --save lodash if you haven't already).
The above will give you output like:
<ul>
<li>
<ul>
<li>1</li>
<li>2</li>
</ul>
</li>
<li>
<ul>
<li>3</li>
<li>4</li>
</ul>
</li>
<li>
<ul>
<li>5</li>
<li>6</li>
</ul>
</li>
</ul>
You can break it up into groups of 2 inside a helper using underscore's map and compact
Template.hello.helpers({
groups() {
// var data = myData.find().fetch();
var data = [
{ category : "Cat1"},
{ category : "Cat2"},
{ category : "Cat3"},
{ category : "Cat4"},
{ category : "Cat5"},
{ category : "Cat6"}];
return _.chain(data).map(function(item, index){
return (index % 2) ? false : data.slice(index, index + 2);
}).compact().value();
},
});
Then, in your template you can use a nested #each in to loop through groups
<template name="hello">
<ul>
{{#each group in groups}}
<li class="col-md-2">
<ul>
{{#each category in group}}
<li>{{category.category}}</li>
{{/each}}
</ul>
</li>
{{/each}}
</ul>
</template>

How to find sibling element with behat/mink?

HTML:
<div id="my-id">
<li class="list_element">
<div class="my_class"></div>
</li>
<li class="list_element">
<div class="another_class"></div>
</li>
<li class="list_element">
<div class="class3"></div>
</li>
</div>
What I want to do with behat/mink:
$page = $this->getSession()->getPage();
$selector = $page->find('css', "#my-id .my_class"); //here I need anchor element located near to .my_class div.
I don't know in which one .list_element .my_class div is. I know only anchor is next to .my_class element. Which selector should I use in the find() function?
Try one of these:
#my-id .my_class ~ a
#my-id .my_class + p
#my-id .list_element a
This is too basic question.Please see more here w3schools

executing java/scala code in scala.html templates

Can I execute that line of code
nav = request().path().toString()
inside of scala template like index.scala.html
I would like to have that code to check on witch side is user and to mark it on menu
using code like this in main.scala.html:
<li class="#("active".when(nav == "contact"))">
Contacts
</li>
I would recommend you different approach, create tag - resuable template, which takes Integer as an argument,
it will render menu and mark as an active different menuitem depends on value.
#(menuItem: Int)
<ul >
<li #if(menuItem==1){ class="active" } >
////
</li>
<li #if(menuItem==2){ class="active" }>
</li>
<li #if(menuItem==3){ class="active" }>
///
</li>
</ul>
from your contact page and any other page, call this tag with corresponding value, #views.html.tags.menu(1)
You can define variables like that if that is your question. If it is not your question than please try to explain your problem in more detail.
#nav = { #request().path().toString() }

jquery parent/children selector for counting <li>

I have this piece of HTML
<div id="fileTreeInviati">
<ul class="php-file-tree">
<li class="pft-directory">
A006 - SOMETEXT (<span name="contaNew"></span>)
<img src="./moduli/home/images/info.png" title="Informazioni Azienda" class="imgInfo"/>
<ul style="display: none;">
<li class="pft-file ext-png">
cut.png
</li>
<li class="pft-file ext-dll">
Safari.dll
</li>
</ul>
</li>
<li class="pft-directory">
A012 - SOMETEXT (<span name="contaNew"></span>)
<img src="./moduli/home/images/info.png" title="Informazioni Azienda" class="imgInfo"/>
<ul style="display: none;">
<li class="pft-file ext-jpg">
04.jpg
</li>
<li class="pft-file ext-dll">
Safari.dll
</li>
</ul>
</li>
<li class="pft-directory">
A014 - SOMETEXT (<span name="contaNew"></span>)
<img src="./moduli/home/images/info.png" title="Informazioni Azienda" class="imgInfo"/>
<ul style="display: none;">
<li class="pft-file ext-txt">
acu.txt
</li>
<li class="pft-file ext-dll">
Safari.dll
</li>
</ul>
</li>
</ul>
I'm working on a js snippet that cycle through all "a" of the "li" and checks if it has the class "new" if yes increment a counter by one. This counter now has to be printed on the relative "li" "span" 3 level before.
So I have the number of the element with the "new" class.
The js snippet is this
$("#fileTreeInviati .php-file-tree .pft-directory li").each(function(){
$(this).children("a").each(function(i,e){
if ($(e).hasClass("new")){
cont++;
console.log($(e).text());
$(this).parent().parent().parent().children("a").children("span").text(cont);
}
})
cont = 0;
});
I think I'm almost there but the counter is always 1. I think there is something mess with .children, maybe it can handle only the first occurrence?
Thanks for help
Why not just use .length instead?
$('#fileTreeInviati .php-file-tree .pft-directory li a.new').length;
Update: If you want to count every li element separately, use this:
$('#fileTreeInviati .php-file-tree .pft-directory li').each(function() {
alert($('a.new', this).length);
})
OK, I figure out how to do the magic:)
Here it is:
cont = 0;
$('#fileTreeInviati .php-file-tree .pft-directory').each(function() {
$(this).children("ul").children("li").children("a.new").each(function(i,e){
cont++;
$(e).parent().parent().parent().children("a").children("span").text(cont);
});
cont=0;
});
Now all works perfect. If you think this can do in a better way, lemme know.
Bye