Xpath: How to get values without a element node - python-2.7

I want to extract the values 4, 3 and 0 but don't know how since they do not have an attribute name
<div class="ea_default_border" style="width:230px; margin-left:20px;">
<span class="ea_default_label">Catégorie :</span>
<a href='/index.php?option=com_'>Location</a><br/>
<span class="ea_default_label">Type :</span>
<a href='/index.php?option=com_'>Maison, Villa, Propriété</a><br/>
<strong>Pièces : </strong>4<br/>
<strong>Chambres : </strong>3<br/>
<strong>SDE/Salle de bains : </strong>0
</div>
I want the desired result to be
4
3
0
and
Pièces : 4
Chambres : 3
SDE/Salle de bains : 0
I have tried several guess that isn't giving the desired result. Thanks in advance for your answers
EDIT: I tried the following xpaths
//div[#class="ea_default_border"]/br/text() <== This gets me nothing as the numbers aren't in a tag.
//div[#class="ea_default_border"]/strong/text() <=== This gets me the titles only.

try
//*[#class='ea_default_border']/child::text()
for your second requirement, try this:
concat(//*[#class='ea_default_border']/strong/text(), //*[#class='ea_default_border']/strong/following-sibling::text()[1])

Related

In ZURB Foundation 6, is there a way to have columns flow around a hidden column?

Using ZURB Foundation 6, is there a way to have columns flow around a hidden column?
So for this code:
<ul class="row large-up-3">
<li class="column column-block large-4" style="display:none;">1</li>
<li class="column column-block large-4">2</li>
<li class="column column-block large-4">3</li>
<li class="column column-block large-4">4</li>
<li class="column column-block large-4">5</li>
<li class="column column-block large-4">6</li>
</ul>
It current displays like this:
2 3
4 5 6
But what I'm hoping to achieve is this:
2 3 4
5 6
Is this feasible?
I don't need to use "display: hidden" if there's another way to do this. That's just placeholder CSS code.
If not, is there a way to do this in Bootstrap or writing my own grid CSS? I can bypass Foundation for this particular content if needed.
Thank you for any help!
Figured it out. In Foundation 6, the following CSS code forces each row to start anew:
.large-up-3 > .column:nth-of-type(3n+1), .large-up-3 > .columns:nth-of-type(3n+1) {
clear: both;
}
Note: I'm using a 3-three column row, so your code will be different, depending on how many columns you have per row.
So if you change clear:both to clear:none, you can then hide columns using display:none, and the columns will automatically fill any gaps.
Why does ZURB do this?
Well, if you change this CSS code, you'll find that your columns don't line up vertically anymore, due to varying column heights.
So this CSS code forces everything to look nice.
If you remove the clear:both code and then run into a alignment problems, you might look into ZURB's "equalizer" code to set all your columns to an equal height.
https://get.foundation/sites/docs/equalizer.html
There are various ways to prettify things further if equal column heights end up looking wrong in your theme, such as using CSS overflow options, but that's getting outside the scope of this question.
I hope this helps someone!

Append values in regular expressions

I'm using Xpath and regular expressions to obtain data from a web page
I'm using the following xpath to get the portion I'm interested in.
response.xpath('//*[#id="business-detail"]/div/p').extract()
EDIT:
Which provides the following:
[u'<p><span class="business-phone" itemprop="telephone">(415) 287-4225</span><span class="business-address" itemprop="address" itemscope itemtype="http://schema.org/PostalAddress"><span itemprop="streetAddress">2180 Bryant St. STE 203, </span><span itemprop="addressLocality">San Francisco</span>,\xa0<span itemprop="addressRegion">CA</span>\xa0<span itemprop="postalCode">94110</span></span><span class="business-link">www.klopfarchitecture.com</span> <br><br></p>']
I'm interested in
<span itemprop="streetAddress">2180 Bryant St. STE 203, </span>
<span itemprop="addressLocality">San Francisco</span>
<span itemprop="addressRegion">CA</span>
<span itemprop="postalCode">94110</span>
So I'm using this regex to extract the data
reg = r'"streetAddress">[0-9]+[^<]*'
reg = r'"addressLocality"[^<]*'
reg = r'"addressRegion"[^<]*'
reg = r'"postalCode"[^<]*'
The problem is that are four of them so I get four variables, I need to append the data to have the full address in one variable to assign it to an Item, what would be an efficient way to accomplish it?
EDIT2:
You're right Roshan Jossey, I can use response.xpath('//*[#itemprop="streetAddress"]').extract()
But still are four labels, addressLocality, addressRegion and postal code. how I merge the results?
I looking for this result:
2180 Bryant St. STE 203, San Francisco, CA 94110
And I'm getting this format for each of the four parts
<span itemprop="streetAddress">2180 Bryant St. STE 203, </span>
I'd recommend to use just xpaths to solve this problem
response.xpath('//*[#id="business-detail"]/div/p//span[#itemprop="streetAddress"]/text()').extract()[0]
will provide you the street address. You can extract all other elements in a similar fashion. Then its just a matter of concatenating them.
Regular expressions looks like an overkill when such simple xpath solutions exist.

Sublime Text regex to re-order Facebook message export

I've exported my Facebook message history and found that messages are displayed as below. Some are in blocks, some blocks are out of order, some are together, all in reverse.
John Doe Sunday, 24 August 2014, 01:18
Hello!
Jane Doe Sunday, 24 August 2014, 01:17
Hi!
What I'm trying to do is to use a regex, [a-z]* and $1, etc., to search and replace in Sublime Text to re-layout the data such that the above becomes sortable in Excel (to get everything in correct order) as below (or any date-sortable manner):
2014.08.24 01:17 Jane Doe Hi
2014.08.24 01:18 John Doe Hello!
Is this possible? I've managed to select the name date and time, but cannot get the variable-length messages consistently and nor re-order/move the date/time or message to the example blow. Does this make sense, or am I wasting my time?
Would it be best in a tab/comma separated way, too?
You'd be much better off using your favorite language to parse the HTML in html/messages.htm. Open the file in Sublime, use a code formatter like HTML-CSS-JS Prettify (note: requires Node.js) to format it, then look at the structure. Basically, each conversation is in reverse order, oldest first, and within the conversation each message is sorted most recent first (yay consistency!). Here's an example:
<div class="thread">Fred Smith, Joe Blow
<div class="message">
<div class="message_header">
<span class="user">Fred Smith</span>
<span class="meta">Monday, June 13, 2011 at 3:42pm EDT</span>
</div>
</div>
<p>Not much Joe. How are you?</p>
<div class="message">
<div class="message_header">
<span class="user">Joe Blow</span>
<span class="meta">Monday, June 13, 2011 at 11:00am EDT</span>
</div>
</div>
<p>Hey there Fred, what's up?</p>
</div>

Way to force specific column down to next row in smaller sizes?

Say I have three columns:
Col 1 | Col 2 | Col 3
And I would like, when the screen size changes to "small", the columns to change to this:
Col 1 | Col 3
Col 2 (in it's own full-size row)
Basically, Col 3 would go onto it's own row. Now, I know I can do this with the "visiblity" classes Foundation provides, but then you'd have to duplicate the Col 2 content twice, as far as I understand it. I'd like to avoid this if possible.
Any ideas? Or am I crazy that Foundation doesn't easily allow for this? Or maybe I should just hack it together by making Col 2 positive: absolute or something and call it a day?
You're looking for something called Source Ordering. (It's towards the bottom of that docs page.)
Order your html so that the columns are defined in the order 1,3,2. Then, use the push/pull classes to swap the positions of columns 3 and 2 for when they are all in the same row.
<div class="small-6 medium-4 columns">
... Column 1 Content ...
</div>
<div class="small-6 medium-4 columns medium-push-4">
... Column 3 Content ...
</div>
<div class="small-12 medium-4 columns medium-pull-4">
... Column 2 Content ...
</div>

How to use float filter to show just two digits after decimal point?

I am using Flask/Jinja2 template to show a number using |float filter.
Here is my code
{% set proc_err = nb_err|length / sum * 100 %}
({{proc_err|float}}%)
Output is a bit awkward:
17/189 (8.99470899471%)
I am looking for a way to make the places after dot limited to a number e.g. 2.
Desired output:
17/189 (8.99%)
It turns to be quite simple:
My code:
{% set proc_err = nb_err|length / sum * 100 %}
({{proc_err|float}}%)
Can be changed a bit with:
{% set proc_err = nb_err|length / sum * 100 %}
({{'%0.2f' % proc_err|float}}%)
or using format:
({{'%0.2f'| format(proc_err|float)}}%)
Reference can be found here on jinja2 github issue 70
You can use round to format a float to a given precision.
Extracted from the docs:
round(value, precision=0, method='common')
Round the number to a given precision. The first parameter specifies the precision (default is 0), the second the rounding method:
common rounds either up or down
ceil always rounds up
floor always rounds down
If you don’t specify a method common is used.
{{ 42.55|round }}
-> 43.0
{{ 42.55|round(1, 'floor') }}
-> 42.5
Note that even if rounded to 0 precision, a float is returned. If you need a real integer, pipe it through int:
{{ 42.55|round|int }}
-> 43
Here is one approach ::
{{ "%.2f"|format(result) }}
Tweak it as you like :)
(I'm assuming you use to Ionic v2)
Use this front-end code:
<div *ngIf="device.usage > 0">
{{ Round(device.usage)}} min
</div>
and put the following function into the .ts file:
Round(number):number
{
return parseFloat(Number((number/1000) / 60).toFixed(2));
}