Jade template engine, rendering HTML as text - node.js

I have this piece of code in one of my .jade files:
each item in items
li= item.name + " " + item.inStock + " <a href='/item/"+item.uniqueId+"'>buy now!</a>"
What this renders is:
Of Mice and Men 1000 <a href='/item/1'>buy now!</a>
[...]
As you can see the <a href='/item/1'>buy now!</a> is not rendered as HTML but as plain text. Is there a way to render it as HTML, so that it creates a link?
Thanks!

each item in items
li
| #{item.name} #{item.inStock}
a(href="/item/"+item.uniqueId) buy now!

try the unescaped operator:
li!= ...
it should do the trick.

Related

How do I retrieve text from a text node in Selenium

So, essentially I want to get the text from the site and print it onto console.
This is the HTML snippet:
<div class="inc-vat">
<p class="price">
<span class="smaller currency-symbol">£</span>
1,500.00
<span class="vat-text"> inc. vat</span>
</p>
</div>
Here is an image of the DOM properties:
How would I go abouts retrieving the '1,500.00'? I have tried to use self.browser.find_element_by_xpath('//*[#id="main-content"]/div/div[3]/div[1]/div[1]/text()') but that throws an error which says The result of the xpath expression is: [object Text]. It should be an element. I have also used other methods like .text but they either only print the '£' symbol, print a blank or throw the same error.
You can use below css :
p.price
sample code :-
elem = driver.find_element_by_css_selector("p.price").text.split(' ')[1]
print(elem)

How to get data from a tag if it's present in HTML else Empty String if the tag is not present in web scraping Python

Picture contains HTML code for the situation
case 1:
<li>
<a> some text: </a><strong> 'identifier:''random words' </strong>
</li>
case 2:
<li>
<a> some text: </a>
</li>
I want to scrape values for identifiers if it's present, else I want to put an empty string if there is no identifier in that particular case.
I am using scrapy or you can help me with BeautifulSoup as well and will really appreciate your help
It's a little bit unclear what do you want exactly, because your screenshot is little bit different than your example in your question. I suppose you want to search text "some text:" and then get next value inside <strong> (or empty string if there isn't any):
from bs4 import BeautifulSoup
txt = '''
<li>
<a> some text: </a><strong> 'identifier:''random words' </strong>
</li>
<li>
<a> some text: </a>
</li>
'''
soup = BeautifulSoup(txt, 'html.parser')
for t in soup.find_all(lambda t: t.contents[0].strip() == 'some text:'):
identifier = t.parent.find('strong')
identifier = identifier.get_text(strip=True) if identifier else ''
print('Found:', identifier)
Prints:
Found: 'identifier:''random words'
Found:

Trying to use PugJS to iterate both href attribute and link text

I'm having a hard time taking my json object and using the name and values to make a link. I've tried separate arrays as well, but I've decided it'd be easier if I set the object as {title:url} and when I use this code:
ul
each url, title in news
li= title
li= url
it returns my titles and urls like so
These are the arguments against net neutrality — and why they’re
wrong
https : //techcrunch .
com/2017/05/19/these-are-the-arguments-against-net-neutrality-and-why-theyre-wrong/
The bizarre naming trends that modern startups follow
https: //techcrunch .
com/2017/05/20/the-bizarre-naming-trends-that-modern-startups-follow/
Salesforce marches steadily toward $10B run rate goal
https :// techcrunch .
com/2017/05/19/salesforce-marches-steadily-toward-10b-run-rate-goal/
Uber threatened to fire engineer at center of Waymo trade secret
lawsuit
https :
//techcrunch.com/2017/05/19/uber-waymo-anthony-levandowski-termination-threat/
but when I try to make links with this code
each url, title in news
a(href= url) title
I get this:
titletitletitletitle
the links work, but it won't iterate the title...
any tips with this issue?
I decided to switch to an array and it seems like you need a = after (href= link) like this:
each articles in news
p
a(href = articles.url)= articles.title
li= articles.description
The issue is actually that there must be an equal sign (you were right about that) but there can be no space between the assignee (left side) and that equal sign. For example, this works:
ul
each url, title in {'/a': 'Title A', '/b': 'Title B'}
li
a(href=url)= title
... and will render:
<ul>
<li>
<a href='/a'>Title A</a>
</li>
<li>
<a href='/b'>Title B</a>
</li>
</ul>
If you omit the equals sign or leave a space like this:
ul
each url, title in {'/a': 'Title A', '/b': 'Title B'}
li
a(href=url) = title
... you'll get:
<ul>
<li>
<a href='/a'>title</a>
</li>
<li>
<a href='/b'>title</a>
</li>
</ul>

Jade template: How to create a hyperlink in UL with description to the hyperlink

I'm trying to create a list with hyperlinks. Each list item contains a subject and a simple description to that subject in one line. I want to include a hyperlink for the subject but not the description. What I did is as below.
ul#subjects
li
a(href="#") Subject: Here is the description of the subject
The link works fine but it covers the whole line of text. How do I close the hyperlink so that it becomes something like below in HTML? Note that the ':' is not covered by the hyperlink.
<ul id='subjects'>
<li><a href='#'>Subject</a>: Here is is the description of the subject
</ul>
Thank you. : )
use | to display the HTML text in Jade
e.g.
ul#subjects
li
a(href="#") Subject
| : Here is the description of the subject

Show only li and corresponding elements that contain visible and hidden text matching search terms?

I have this:
<ul id="container">
<li class="sub_container">
<a> + text + </a><span> + text + </span><span> + text + </span>
<span> + text + </span><a> + text + </a><div> + text + </div>
<hr></hr>
</li>
<li class="sub_container">
<a> + text + </a><span> + text + </span><span> + text + </span>
<span> + text + </span><a> + text + </a><div> + text + </div>
<hr></hr>
</li>
</ul>
and I want to be able to search some visible and hidden text (if possible), so where there is a matching term within + text + inside any <li> tags, only those relevant tags and all their contained elements (which also includes their + text +) stay visible while all other <li> and contained elements become hidden. Shown above is a sample set of two <li> and their contained elements but those tags and elements will multiply dynamically - only the content of + text + changes (+ text + parts come from spreadsheet).
I tried this for searching, where #find is a link to activate the search and #filter is the input box to type the search terms:
$jq("#find").click(function(){
$jq("ul li").hide()
.filter(":contains('"+ $jq("#filter").val() +"')").show()
return false;
})
It works but hides only the next 2 items (in this case an <a> and <span>) located immediately after the opening <li> tag. I tried this plugin at http://lomalogue.com/jquery/quicksearch/ but it gave no results. Any simple click function like the above would be appreciated. Thx ahead for guiding me with a working sample.
This parcel of code finally helped:
$jq("span.numbers, div.timestamp, span.title, div.text, hr").hide();
$jq(".text:containsIgnoreCase("+ $jq(".search").val() +")").show("fast");
$jq(".text:containsIgnoreCase("+ $jq(".search").val() +")").prev("span").show("fast");
$jq(".text:containsIgnoreCase("+ $jq(".search").val() +")").prev("span").prev("div").show("fast");
$jq(".text:containsIgnoreCase("+ $jq(".search").val() +")").prev("span").prev("div").prev("span").show("fast");
$jq(".text:containsIgnoreCase("+ $jq(".search").val() +")").next("hr").show("fast");
The above code hides all elements and then shows elements matching search terms, I guess you could do it the other way around. The headers and other items (siblings related to and located above the divs containing the search terms) display accordingly. This works great for dynamically inserted data coming from a spreadsheet, for instance, where #ids cannot be used and each section of data utilizes the same .class elements.

Resources