CSS Selectors Explanation - html

I'm really confused as to how I can style this list WITHOUT changing the following HTML or wrapping it in class names. How do I go about this?
For example, if I want to put Ocean Fish in bold but NOT have Pacific or Atlantic affected, how do I target that div without adding a class name?
Another example, I want to have "12 salmon" and "3 cod" in green text, and "46 halibut" and "13 pollock" in blue text? in I know there's a trick using very specific selectors, but I don't know how.
<ul>
<li>
<div>Ocean Fish</div>
<ul>
<li>
<div>Pacific</div>
<ul>
<li>12 salmon</li>
<li>3 cod</li>
</ul>
</li>
</ul>
<ul>
<li>
<div>Atlantic</div>
<ul>
<li>46 halibut</li>
<li>13 pollock</li>
</ul>
</li>
</ul>
</li>
</ul>```

This will handle all your cases if you want to do it without classes.
body > ul > li > div{font-weight:bold;}
body > ul > li > ul:nth-child(2) > li > ul{color:green;}
body > ul > li > ul:nth-child(3) > li > ul{color:blue;}

Similar method to previous answer, but more direct:
let divSel = document.querySelectorAll('div'); // alert(divSel.length);
divSel[0].style.fontWeight = 'bolder';
let liSel = document.querySelectorAll('li');
liSel[2].style.color = 'green';
liSel[3].style.color = 'green';
liSel[5].style.color = 'blue';
liSel[6].style.color = 'blue';
Still a pain to modify if entry order is changed.

Related

how to write css selector for scrapy?

I have the following web page:
<div id="childcategorylist" class="link-list-container links__listed" data-reactid="7">
<div data-reactid="8">
<strong data-reactid="9">Categories</strong>
</div>
<div data-reactid="10">
<ul id="categoryLink" aria-label="shop by category" data-reactid="11">
<li data-reactid="12">
Contact Lenses
</li>
<li data-reactid="14">
Beauty
</li>
<li data-reactid="16">
Personal Care
</li>
I want to have css selector of href tags under li tag, i.e. for contact lens, beauty and personal-care. How to write it?
I am writing it in the following way:
#childcategorylist li
gives me following output:
['<li class="titleitem" data-reactid="16"><strong data-reactid="17">Categories</strong></li>']
Please help!
I am not a expert in scrapy, but usually html elements should have a .text object.
If not, you might want to use regexp to extract the text between > and < like:
import re
txt = someArraycontainingStrings[0]
x = re.search(">[a-zA-Z]*</", txt)
Maybe that gives you proper results

how to give a class to an <li> element that inserted in my code automatically by moodle cashe?

I am creating a new moodle block and I want to edit the code of CSS for this block. Currently there are images that I want to make inline but moodle makes these images in ul and li by default that take the full width "display: block". I can't give any class to these li and I can't select the li because moodle create the li tags for the titles and everything in the block, so if I edit the li it will be for every element like h1 and so on not only for imgs, can I give a class for it?
<ul class="unlist">
<li class="r0"><div class="column c1"><h3><div class="text_to_html">User grade</div></h3></div></li>
<li class="r1"><div class="column c1">first grade middle school </div></li>
<li class="r0"><div class="column c1"><h3><div class="text_to_html">user type</div></h3></div></li>
<li class="r1"><div class="column c1"> Student</div></li>
<li class="r0"><div class="column c1"><h3><div class="text_to_html">General Certificates</div></h3></div></li>
<li class="r1"><div class="column c1"><a download="download" class="inline" href="http://81.10.36.53/pluginfile.php/711/profilefield_file/files_2/0/1900 $10000 Gold Certificate both sides.jpg"><img class="cvpic" src="http://81.10.36.53/pluginfile.php/711/profilefield_file/files_2/0/1900%20%2410000%20Gold%20Certificate%20both%20sides.jpg"></a></div></li>
<li class="r0"><div class="column c1"><a download="download" class="inline" href="http://81.10.36.53/pluginfile.php/711/profilefield_file/files_2/0/Aamir Javed Certificate.jpg"><img class="cvpic" src="http://81.10.36.53/pluginfile.php/711/profilefield_file/files_2/0/Aamir%20Javed%20Certificate.jpg"></a></div></li>
<li class="r1"><div class="column c1"><a download="download" class="inline" href="http://81.10.36.53/pluginfile.php/711/profilefield_file/files_2/0/US $20 1905 Gold Certificate.jpg"><img class="cvpic" src="http://81.10.36.53/pluginfile.php/711/profilefield_file/files_2/0/US%20%2420%201905%20Gold%20Certificate.jpg"></a></div></li>
If you can use jQuery, you can do something like:
$(document).ready(function() {
$("ul.unlist li div img").css("display", "inline");
});
What does it do:
Wait for the page to completely load
Select all <img> tags inside your <ul> that have the unlist class, that also have a <div> as a parent element, inside a <li> element
So, basically, it selects the <img> in the last 3 rows of the few lines you provided.
That way, you only select the <li> that have a <img> tag in it and not the other <li> with the <h3> tag, for example.
If you can't use jQuery, here is the same code, but in pure JavaScript:
(function() {
document.querySelector("ul.unlist li div img").style.display = "inline";
})();
Edit
By re-reading your question, you want to add a class, so here it is:
jQuery
$("ul.unlist li div img").addClass("your-class");
JavaScript
document.querySelector("ul.unlist li div img").className += " your-class";
Don't forget the space in the beginning of the JavaScript one, otherwise your <img> classes are gonna be like that : cvpicyour-class instead of cvpic your-class

get text with jsoup

I have this HTML
<ul id="items"><li>
<p><strong><span class="style4"><strong>Lifts open today include Agassiz to the top, Sunset, Hart Prairie, Little and Big Spruce from <br />
9 a.m. - 4 p.m.</strong></span></strong></p>
</li>
</ul>
<h3> </h3>
<h3>Trails Open<br />
</h3>
<ul id="items">
<li class="style4">
<p><strong><span class="style4">100% of trails open with 30 groomed runs. </span></strong></p>
</li>
</ul>
I want the text "Lifts open today....."
This is my code. Nothing is show. There is no error in the logcat
Document doc = Jsoup.connect(url).get();
Elements div = doc.select("div.right");
for (Element liftope : div){
Elements p =liftope.select("#items > li > p");
liftoper = p.text();
}
What is wrong???
If you want only that text "Lifts open today include: Agassiz to the top, Sunset, Hart Prairie, Aspen and Little Spruce Conveyor!" this (i try) work:
Element div = doc.getElementById("contentinterior");
Elements uls = div.getElementsByTag("ul");
Element ul = uls.get(2);
String result = ul.text();

Creating table of contents in html

Is it possible to create an ordered list like the following?
I like this for a table of contents I'm creating.
Into
Section1
2.1 SubSection1
2.2 SubSection2
Section2
.....
I have the following but each subsection restarts from 1.
<ol>
<li>
</li>
<li>
</li>
<ol>
<li>
</li>
<li>
</li>
</ol>
</ol>
Thanks
This can indeed be done with pure CSS:
ol {
counter-reset: item;
}
li {
display: block;
}
li:before {
content: counters(item, ".")" ";
counter-increment: item;
}
Same example as a fiddle.
There's quite a number of jQuery plugins to generate a table of contents.
Look at this one for starters
Another one here, with ordered lists
Have you seen this post:
Number nested ordered lists in HTML
I don't think it can be done without using JS.
This code leads to the desired output for me:
<ol>
<li>
foo
</li>
<li>
bar
<ol>
<li>
baz
</li>
<li>
qux
</li>
</ol>
</li>
<li>
alpha
<ol>
<li>
beta
</li>
<li>
gamma
</li>
</ol>
</li>
</ol>
CSS:
ol {
counter-reset: item;
}
li {
display: block;
}
li::before {
content: counters(item, ".")". ";
counter-increment: item;
}
Fiddle: http://jsfiddle.net/Lepetere/evm8wyj5/1/
For myself I was not happy with existing solutions. So I created a solution with Python3 and BeautifulSoup.
The function take HTML source as string and looks for header tags (e.g. h1). In the next steps an id= is created for the header and also corresponding toc entry.
def generate_toc(html_out):
"""Create a table of content based on the header tags.
The header tags are used to create and link the toc.
The toc as place on top of the html output.
Args:
html_out(string): A string containing the html source.
Returns:
(string): The new string.
"""
from bs4 import BeautifulSoup
# the parser
soup = BeautifulSoup(html_out, 'html.parser')
# create and place the div element containing the toc
toc_container = soup.new_tag('div', id='toc_container')
first_body_child = soup.body.find_all(recursive=False)[0]
first_body_child.insert_before(toc_container)
# toc headline
t = soup.new_tag('p', attrs={'class': 'toc_title'})
t.string = 'Inhalt'
toc_container.append(t)
def _sub_create_anchor(h_tag):
"""Create a toc entry based on a header-tag.
The result is a li-tag containing an a-tag.
"""
# Create anchor
anchor = uuid.uuid4()
h_tag.attrs['id'] = anchor # anchor to headline
# toc entry for that anchor
a = soup.new_tag('a', href=f'#{anchor}')
a.string = h_tag.string
# add to toc
li = soup.new_tag('li')
li.append(a)
return li
# main ul-tag for the first level of the toc
ul_tag = soup.new_tag('ul', attrs={'class': 'toc_list'})
toc_container.append(ul_tag)
# helper variables
curr_level = 1
ul_parents = [ul_tag]
# header tags to look for
h_tags_to_find = [f'h{i}' for i in range(1, 7)] # 'h1' - 'h6'
for header in soup.find_all(h_tags_to_find):
next_level = int(header.name[1:])
if curr_level < next_level: # going downstairs
# create sub ul-tag
sub_ul_tag = soup.new_tag('ul', attrs={'class': 'toc_list'})
# connect it with parent ul-tag
ul_parents[-1].append(sub_ul_tag)
# remember the sub-ul-tag
ul_parents.append(sub_ul_tag)
elif curr_level > next_level: # going upstairs
# go back to parent ul-tag
ul_parents = ul_parents[:-1]
curr_level = next_level
# toc-entry as li-a-tag
li_tag = _sub_create_anchor(header)
# add to last ul-tag
ul_parents[-1].append(li_tag)
return soup.prettify(formatter='html5')
This is maybe not elegant in all of your use cases. Myself I use to put TOC's on top of HTML reports generated by data sciences routines (e.g. pandas).
<h5>Table of contents:</h5> <ol> <li>Types of home fruit drying machines</li> <li>What fruits can you dry with an electric fruit drying machine?</li> < li>Zagores Machine Electric Fruit Dryer</li> <li>Buy Electric Fruit Dryer</ li> <li>Gas fruit drying machine</li> <li>Electric fruit drying machine price< /li> <li>FAQ</li> </ol>
.
see this in: enter link description here

CSS select next two elements

I have the following HTML:
...
<li>...</li>
<li>...</li>
<li class="last">...</li>
<li class="selected">...</li> # <- this
<li>...</li> # <- this
<li class="last">...</li> # <- this
<li>...</li>
<li>...</li>
<li class="last">...</li>
....
So, basically groups of three lis, the last one with a specific class and in one of the groups the first li with a special class.
Is there a way with pure CSS to select the li with the selected class and the next two ones, marked in the above code?
I've managed to select the next one using li.selected + li, but this doesn't work for the one after that.
Simply add another + li to reach it:
li.selected, li.selected + li, li.selected + li + li