Using a pandas data frame inside of an HTML - html

I'm trying to implement a pandas dataframe in to this HTML code.
So, instead of User in the Dear User row,
I can take a specific value from the last row of my data frame.
And instead of Users daily amount after the Reason: row I will get a specific reason from the last row of a data frame.
Sample Code:
msg.attach(MIMEText(
'''
<html>
<body>
<p><img src="cid:1" width="900" height="100"></p>
<b><h1 style="text-align: left;">Pay Attention!</h1></b>
<h3 style="text-align: left;">Dear User,</h3>
<p>There has been an anomaly in the following meaurement: </p>
<b><p>Users daily amount </p></b>
<h3>Reasons: </h3>
<p> - Increase in the amount of Information Request sent</p>
<p> - Unknown new rule firing </p>
<h3>Last 2 Weeks Activity</h3>
<p><img src="cid:0"></p>
</body>
</html>
''',
'html', 'utf-8'))
Hope I'm clear, Thanks a lot!

It is not entirely clear what you want to achieve, but as far as I understood, you want to pass a value from pandas dataframe to your html code. In this case, you can simply concatenate the strings.
user = df.iloc[-1][0] //index of value
html = '''
<html>
<body>
<p><img src="cid:1" width="900" height="100"></p>
<b><h1 style="text-align: left;">Pay Attention!</h1></b>
<h3 style="text-align: left;">Dear ''' + user + ''',</h3>
<p>There has been an anomaly in the following meaurement: </p>
<b><p>Users daily amount </p></b>
<h3>Reasons: </h3>
<p> - Increase in the amount of Information Request sent</p>
<p> - Unknown new rule firing </p>
<h3>Last 2 Weeks Activity</h3>
<p><img src="cid:0"></p>
</body>
</html>
'''
msg.attach(MIMEText(html, 'html'))

Related

How to get a div or span class from a related span class?

I've found the lowest class: <span class="pill css-1a10nyx e1pqc3131"> of multiple elements of a website but now I want to find the related/linked upper-class so for example the highest <div class="css-1v73czv eh8fd9011" xpath="1">. I've got the soup but can't figure out a way to get from the 'lowest' class to the 'highest' class, any idea?
<div class="css-1v73czv eh8fd9011" xpath="1">
<div class="css-19qortz eh8fd9010">
<header class="css-1idy7oy eh8fd909">
<div class="css-1rkuvma eh8fd908">
<footer class="css-f9q2sp eh8fd907">
<span class="pill css-1a10nyx e1pqc3131">
End result would be:
INPUT- Search on on all elements of a page with class <span class="pill css-1a10nyx e1pqc3131">(lowest)
OUTPUT - Get all related titles or headers of said class.
I've tried it with if-statements but that doesn't work consistently. Something with an if class = (searchable class) then get (desired higher class) should work.
I can add any more details if needed please let me know, thanks in advance!
EDIT: Picture per clarification where the title(highest class) = "Wooferland Festival 2022" and the number(lowest class) = 253
As mentioned, question needs some more information, to give a concret answer.
Assuming you like to scrape the information in the picture based on your example HTML you select your pill and use .find_previous() to locate your elements:
for e in soup.select('span.pill'):
print(e.find_previous('header').text)
print(e.find_previous('div').text)
print(e.text)
Assuming there is a cotainer tag in HTML structure like <a> or other you would select this based on the condition, that it contains a <span> wit class pill:
for e in soup.select('a:has(span.pill)'):
print(e.header.text)
print(e.header.next.text)
print(e.footer.span.text)
Note: Instead of using css classes, that can be highly dynamic, try use more static attributes or the HTML structure.
Example
See both options, for first one the <a> do not matter.
from bs4 import BeautifulSoup
html='''
<a>
<div class="css-1v73czv eh8fd9011" xpath="1">
<div class="css-19qortz eh8fd9010">
<header class="css-1idy7oy eh8fd909">some date information</header>
<div class="css-1rkuvma eh8fd908">some title</div>
<footer class="css-f9q2sp eh8fd907">
<span class="pill css-1a10nyx e1pqc3131">some number</span>
<footer>
</div>
</div>
</a>
'''
soup = BeautifulSoup(html)
for e in soup.select('span.pill'):
print(e.find_previous('header').text)
print(e.find_previous('div').text)
print(e.text)
print('---------')
for e in soup.select('a:has(span.pill)'):
print(e.header.text)
print(e.header.next.text)
print(e.footer.span.text)
Output
some date information
some title
some number
---------
some date information
some date information
some number

Google drive image not displayed in html code of sites.google.com

I am writing an HTML code for the sites.google.com.
I have found the code to wrap the text around the image but I am unable to display the image itself. The image is in google drive(share status is 'viewer for anyone with the link'). Attached below are the code and a snapshot of the error.
<!DOCTYPE html>
<html>
<head>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=default'></script>
</head>
<body>
<h1>Coulomb's Law </h1>
<div><p style="float: right;">
<img src="https://drive.google.com/thumbnail?id=${1rd3VIZ4tjO4GKNZNSFs4nZr-md0z9jy9}&sz=w${width || 400px}-h${height || 400px}></p>
<!--img src="https://drive.google.com/file/d/1rd3VIZ4tjO4GKNZNSFs4nZr-md0z9jy9/view?usp=sharing" width="200px" height="200px" border="1px" allow="autoplay"--></p> </div>
Coulomb's law states that the electrical force between two charged objects is directly proportional to the product of the quantity of charge on the objects and inversely proportional to the square of the separation distance between the two objects.</p>
<p align="justify" style="color:black;font-size:18px;" >
In equation form, Coulomb's law can be stated as</p>
$$ {F = \frac{k* Q_1 * Q_2}{r^2} }$$
<!$$ {J(\theta) =\frac{1}{2m} [\sum^m_{i=1}(h_\theta(x^{(i)}) - y^{(i)})2 + \lambda\sum^n_{j=1}\theta^2_j} $$>
<p align="justify" style="color:black;font-size:18px;" >
where Q<sub>1</sub> represents the quantity of charge on object 1 (in Coulombs), Q<sub>2</sub> represents the quantity of charge on object 2 (in Coulombs), and d represents the distance of separation between the two objects (in meters). The symbol k is a proportionality constant known as the Coulomb's law constant.</p>
</body>
</html>
You have using an invalid HTML (and url).
<img src="https://drive.google.com/thumbnail?id=${1rd3VIZ4tjO4GKNZNSFs4nZr-md0z9jy9}&sz=w${width || 400px}-h${height || 400px}></p>
Use this instead:
<img src="https://drive.google.com/thumbnail?id=1rd3VIZ4tjO4GKNZNSFs4nZr-md0z9jy9&sz=w400px-h400px"></p>

how can i set a time to show something with angular?

i've a search field and when i search for something and nothing is found, i show a message for user, but when has content to show, the message error appear for 3~4 seconds and after this time the message is disappear and the result of search appear...
my html:
<div>
<h2>Search page</h2>
<div class="container clearfix" ng-controller="restaurantsDataCtrl" group-by="category">
<restaurants-gallery ng-show="restaurants.length" category="{{list.category}}" restaurants="{{list.restaurants}}" ng-repeat="list in restaurantsByCategory">
</restaurants-gallery>
<p ng-show="!restaurants.length">Message nothing found.</p>
</div>
</div>
what i need is set a time for this message appear and when this time is ended angular will know if show or not the message.
One thing you could do is create a flag to indicate whether your data request has started:
$scope.completed = false
then in the callback for your data request set that flag as true:
...
$http.get(...).then(function(response) {
$scope.completed = true;
$scope.restaurants = response.data;
}
if you combine that with your conditional to see if there are in fact no restaurants you can be guaranteed the message will only show up after you've tried to get data and nothing came back:
<p ng-show="!restaurants.length && completed">Message nothing found.</p>
Here's a small working example of that idea: http://plnkr.co/edit/IYtSKMHco52rHGWTT55W?p=preview

How to get simple text from HTML page with goquery?

I am new to Go. I am using goquery to extract data from an HTML page.
But the problem is the data I am looking for is not bounded by any HTML tag. It is simple text after a <br> tag. How can I extract it?
Edit : Here is HTML code.
<div class="container">
<div class="row">
<div class="col-lg-8">
<p align="justify"><b>Name</b>Priyaka</p>
<p align="justify"><b>Surname</b>Patil</p>
<p align="justify"><b>Adress</b><br>India,Kolhapur</p>
<p align="justify"><b>Hobbies </b><br>Playing</p>
<p align="justify"><b>Eduction</b><br>12th</p>
<p align="justify"><b>School</b><br>New Highschool</p>
</div>
</div>
</div>
From this I want "Priyanka" and "12th".
The following is what you want:
doc.Find(".container").Find("[align=\"justify\"]").Each(func(_ int, s *goquery.Selection) {
prefix := s.Find("b").Text()
result := strings.TrimPrefix(s.Text(), prefix)
println(result)
})
import strings in front of your code. If you need complete code example, check here.
Try query for and get its siblings
http://godoc.org/github.com/PuerkitoBio/goquery#Selection.Siblings

Getting the proper Xpath for ImportXML OR Xpathonurl functions (Google Sheets or Excel)

I am new at all this and for the life of me I cannot get the text "In Stock Online" or "Out of Stock Online from http://www.walmart.ca/en/ip/large-kick-fetch-ball/6000105718024
I've tried multiple variations with no luck.
Should it not be =importxml("url","//span[#class='msg']") ?
This also doesnt work using Excel's xpathonurl function (an add-in)
Here is the html source code:
<div class="product-avail wgrid-3w6 wgrid-2w4 marg-r-0">
<div class="statuses">
<div class="shipping status">
<span class="icon-status checkmark"></span>
<span class="msg">In stock online</span>** </div>
<div class="shipping-alert status">
<span class="icon-status arrow" style="display: inline;"></span>
<span class="msg"><span class="edd">
See estimated delivery date</span></span> </div> </div> </div>`
Try this:
=importxml("url","//span[#class='msg']/text()")
As per xpath, if you want to retrieve the text value of the given element, you should use text() method.
Update:
The span with class=msg in the given URL has an empty value. So you can handle the #N/A as follows:
=IFERROR(IMPORTXML("http://www.walmart.ca/en/ip/large-kick-fetch-ball/6000105718024", "//span[#class='msg']"), "")