Perl Mechanize identify content between span tag within specific div tag - perl

Perl WWW::Mechanize::Firefox has successfully retrieved the contents of the web page, and stored in the scalar variable $content.
my $url = 'http://finance.yahoo.com/quote/AAPL/financials?p=AAPL';
$mech->get($url);
my $content= $mech->content();
In examining $content, I'm interested in identifying and saving all the information between the span tags inside the table.
There a varies classes that I have no interest in.
Attempt # 1 did not work.
my $tree = HTML::TreeBuilder->new_from_content($txtRawData);
my #list = $mech->find('span');
foreach ( #list ) {
print $_->as_HTML();
}
Attempt # 2 did not work.
foreach my $tag ($tree->look_down(_tag => 'span')) {
my $value = $tag->as_text;
}
The HTML table of interest is:
<div class="Mt(10px)">
<table class="Lh(1.7) W(100%) M(0)">
<tbody>
<tr class="Bdbw(1px) Bdbc($lightGray) Bdbs(s) H(36px)">
<td class="Fw(b) Fz(15px)">
<span>Revenue</span>
</td>
<td class="C($gray) Ta(end)">
<span>9/24/2016</span>
</td>
<td class="C($gray) Ta(end)">
<span>9/26/2015</span>
</td>
<td class="C($gray) Ta(end)">
<span>9/27/2014</span>
</td>
</tr>
<tr class="Bdbw(1px) Bdbc($lightGray) Bdbs(s) H(36px)">
<td class="Fz(s) H(35px) Va(m)">
<span>Total Revenue</span>
</td>
<td class="Fz(s) Ta(end)">
<span>
<span>215,639,000</span>
</span>
</td>
<td class="Fz(s) Ta(end)">
<span>
<span>233,715,000</span>
</span>
</td>
<td class="Fz(s) Ta(end)">
<span>
<span>182,795,000</span>
</span>
</td>
</tr>
<tr class="Bdbw(1px) Bdbc($lightGray) Bdbs(s) H(36px)">
<td class="Fz(s) H(35px) Va(m)">
<span>Cost of Revenue</span>
</td>
<td class="Fz(s) Ta(end)">
<span>
<span>131,376,000</span>
</span>
</td>
<td class="Fz(s) Ta(end)">
<span>
<span>140,089,000</span>
</span>
</td>
<td class="Fz(s) Ta(end)">
<span>
<span>112,258,000</span>
</span>
</td>
</tr>
<tr class="Bdbw(0px)! H(36px)">
<td class="Fw(b) Fz(s) Pb(20px)">
<span>Gross Profit</span>
</td>
<td class="Fw(b) Fz(s) Ta(end) Pb(20px)">
<span>
<span>84,263,000</span>
</span>
</td>
<td class="Fw(b) Fz(s) Ta(end) Pb(20px)">
<span>
<span>93,626,000</span>
</span>
</td>
<td class="Fw(b) Fz(s) Ta(end) Pb(20px)">
<span>
<span>70,537,000</span>
</span>
</td>
</tr>
</tbody>
</table>
</div>
What is the best way to select (set focus upon) one specific table (their could be multiple tables inside the $content variable), and save the text between the span tags to an array (to be passed to the next procedure - to be inserted into a database table)?
I also would like to highlight that:
Sometimes, the text is inside a two (double) sets of span tags.
There is no table header row (or th tags).

Your first attempt works if you actually do it on $tree and not on $mech.
Combined with as_text from your second attempt is pretty nice.
use HTML::TreeBuilder;
my $tree = HTML::TreeBuilder->new_from_content(my #foo = <DATA>);
my #list = $tree->find('span');
foreach ( #list ) {
say $_->as_text();
}
__DATA__
<div class="Mt(10px)">
<table class="Lh(1.7) W(100%) M(0)">
...
This outputs a list of span contents. You should be able to clean those up and work with them.
Revenue
9/24/2016
9/26/2015
9/27/2014
...
Of course as an actual table (array-of-arrays) it would probably make more sense, but for that we'd have to know what it is you are trying to do.

Related

Magento order email missing style

I have a question regarding the magento order emails.
I have created my own order template by loading the default template and modifying it. When i take a look via the 'preview template' button on there, the email appears without order information (of course) but with every styling etc.
However when my customer gets the email it is pure text with no styling at all.
What could i have been doing wrong
Idk if it helps but here is my email template:
{{template config_path="design/email/header"}}
{{inlinecss file="email-inline.css"}}
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td>
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td style='background: #fff' class="email-heading">
<h1 style='color: #68883e;'>Thank you for your order</h1>
<p>Your order is being process right now</p>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td class="order-details">
<h3 style='display: inline; float: left'>Your order <span class="no-link">#{{var order.increment_id}}</span></h3>
<p style='display: inline; float: right'>Placed on {{var order.getCreatedAtFormated('long')}}</p>
</td>
</tr>
<tr class="order-information">
<td>
{{if order.getEmailCustomerNote()}}
<table cellspacing="0" cellpadding="0" class="message-container">
<tr>
<td>{{var order.getEmailCustomerNote()}}</td>
</tr>
</table>
{{/if}}
{{layout handle="sales_email_order_items" order=$order}}
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="address-details">
<h6>Bill to:</h6>
<p><span class="no-link">{{var order.getBillingAddress().format('html')}}</span></p>
</td>
<td class="address-details">
<h6>Client:</h6>
<p><span class="no-link">{{var order.getclientfirstname().format('html')}}</span></p>
</td>
</tr>
<tr>
{{depend order.getIsNotVirtual()}}
<td class="method-info">
<h6>Shipping method:</h6>
<p>{{var order.shipping_description}}</p>
</td>
{{/depend}}
<td class="method-info">
<h6>Payment method:</h6>
{{var payment_html}}
</td>
</tr>
</table>
</td>
</tr>
</table><!-- asd-->
{{layout handle="sales_email_order_items" order=$order}}
{{template config_path="design/email/footer"}}
You shouldn't use the CSS classes to stylize the templates.
Also, you shouldn't use the file with the styles info, that is located anywhere else, as they are not supported by the majority of the email services.
Instead, you can directly use the attribute tags (to implement the stylazation) or use the styles right in the email code (using the tag <style>)
And here is the list of tags you can use:
https://www.campaignmonitor.com/css/

Using TinyMCE with handlebars template

I am trying to use TinyMCE to allow my users to modify a handlebars report template. The template contains several elements that are not valid to TinyMCE and they are being moved around. See the {{#each data}} and {{/each}}
Here is good HTML code for my handlebars template:
<table class="table table-bordered">
<thead>
<tr>
<th><h4>Item</h4></th>
<th><h4 class="text-right">Quantity</h4></th>
<th><h4 class="text-right">Rate/Price</h4></th>
<th><h4 class="text-right">Sub Total</h4></th>
</tr>
</thead>
<tbody>
{{#each Details}}
<tr>
<td>{{Item}}<br><small>{{Description}}</small></td>
<td class="text-right">{{Quantity}}</td>
<td class="text-right">{{Rate}} {{UnitOfMeasure}}</td>
<td class="text-right">{{Amount}}</td>
</tr>
{{/each}}
</tbody>
</table>
After I past the code into TinyMCE, it results to the following:
{{#each Details}}{{/each}}
<table class="table table-bordered">
<thead>
<tr><th>
<h4>Item</h4>
</th><th>
<h4 class="text-right">Quantity</h4>
</th><th>
<h4 class="text-right">Rate/Price</h4>
</th><th>
<h4 class="text-right">Sub Total</h4>
</th></tr>
</thead>
<tbody>
<tr>
<td>{{Item}}<br /><small>{{Description}}</small></td>
<td class="text-right">{{Quantity}}</td>
<td class="text-right">{{Rate}} {{UnitOfMeasure}}</td>
<td class="text-right">{{Amount}}</td>
</tr>
</tbody>
</table>
Has anyone ran across a plugin or something else that may help me?
I just ran into this... I have an order confirmation email that I need to be configurable with a list of order items in a table; same issue.
I did just realize I probably shouldn't be using tables anyway, since they are not responsive, but I ultimately was able to solve the problem with HTML comments, like this:
<tr style="font-weight: bold;">
<td style="width: 145px;">Qty</td>
<td>Item</td>
<td>Unit Price</td>
<td>Total</td>
</tr>
<!--{{#order.line_items}} -->
<tr repeat="">
<td style="width: 145px;">{{quantity}}</td>
<td>{{product.name}}</td>
<td>{{currency unit_price}}</td>
<td>{{currency total}}</td>
</tr>
<!--{{/order.line_items}} -->
<tr>
<td style="width: 145px;"> </td>
<td> </td>
<td><strong>Subtotal:</strong></td>
<td>{{currency order.subtotal}}</td>
</tr>
I was able to use a custom attribute on my Element and use:
<tr repeat="{{#each Details}}">
</tr repeat="{{/each}}">

zurb ink email wrapper doesnt span across full width

I'm having issues with Zurb Ink's email. So it looks fine in desktop, however it there is this gap when it hits the media queries and it seems to be doing it with the containers when its stacked on top of each other. I want it to span the whole width.
http://i.stack.imgur.com/xygNK.png
Here is my code:
<table class="container" id="hero-container">
<!-- hero logo -->
<tr>
<td>
<table class="row">
<tr>
<td class="wrapper last">
<table class="twelve columns">
<tr>
<td id="hero-logo"><img id="logoImage" src="images/hero-logo.png" alt="Hero In The News" /></td>
<td class="expander"></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<!-- /hero logo -->
<!-- hero image -->
<tr>
<td>
<table class="row">
<tr>
<td class="wrapper last">
<table class="twelve columns">
<tr>
<td><img id="headerImage" src="images/hero-img.png" alt="City of San Jose - Capital of Silicon Valley" /></td>
<td class="expander"></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<!-- /hero image -->
<!-- hero main article -->
<tr>
<td>
<table class="row">
<tr>
<td>
<div class="wrapper last">
<table class="twelve columns">
<tr>
<td class="heading1 text-pad">Residents can Pace Energy Use <br/>with New Program</td>
<td class="expander"></td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table class="row">
<tr>
<td>
<div class="wrapper last">
<table class="twelve columns">
<tr>
<td class="text-pad"><p>The city of San Jose has implemented a program to help residents pace their water and energy consumption. </p></td>
<td class="expander"></td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</td>
</tr>
<!-- button -->
<tr>
<td>
<table class="row">
<tr>
<td>
<div class="wrapper last">
<table class="twelve columns">
<tr>
<td class="text-pad link"><p>Read Full Article</p></td>
<td class="expander"></td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</td>
</tr>
<!--/button -->
<!-- /hero main article -->
</table>
<!-- articles row 1 -->
<table class="container">
<tr>
<td>
<table class="row">
<tr>
<td class="wrapper">
<table class="six columns article">
<tr>
<td class="text-pad">
<h2>Energy Efficiency Program Ushers Green Jobs into Kern</h2>
<p>The green economy has arrived in Kern County, and not a moment...</p>
</td>
<td class="expander"></td>
</tr>
</table>
</td>
<td class="wrapper last">
<table class="six columns article">
<tr>
<td class="text-pad">
<h2>Stockton Gets a New “HERO”</h2>
<p>From insulation to more efficient air conditioning to solar panels to...</p>
</td>
<td class="expander"></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
<!-- /articles row 1-->
<!-- articles row 2 -->
<table class="container article-container">
<tr>
<td>
<table class="row">
<tr>
<td class="wrapper article-wrapper">
<table class="six columns article">
<tr>
<td class="text-pad">
<h2>Fresno estimates $56M from HERO program</h2>
<p>Officials with the City of Fresno expect to see more than $56 M...</p>
</td>
<td class="expander"></td>
</tr>
</table>
</td>
<td class="wrapper last article-wrapper-last">
<table class="six columns article">
<tr>
<td class="text-pad">
<h2>Lompoc City Council approves HERO Program</h2>
<p>A new program that helps homeowners make energy and...</p>
</td>
<td class="expander"></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
Caveat: I am new to Ink myself. What "jumps out" at me is that you're using a container on the outside and multiple rows inside. For full width, swap the row and container classes --> so that the table with class row contains a table with class container.

sahi and table operations

For a given table, I'd like to get the below
total number of rows
able to iterate over by row and column
using the Java Driver. I tried the option mentioned here with no luck.
Below is the HTML for table
<div id="hawkMessageCodeTable" class="ui-datatable ui-widget">
<table>
<thead>
<tr>
<th id="hawkMessageCodeTable:j_idt49" class="ui-state-default">
<div class="ui-dt-c">
<span>Code</span>
</div>
</th>
<th id="hawkMessageCodeTable:j_idt51" class="ui-state-default">
<div class="ui-dt-c">
<span>Message</span>
</div>
</th>
</tr>
</thead>
<tbody id="hawkMessageCodeTable_data" class="ui-datatable-data ui-widget-content">
<tr data-ri="0" class="ui-widget-content ui-datatable-even">
<td>
<div class="ui-dt-c">
9005
</div>
</td>
<td>
<div class="ui-dt-c">
Initial Fraud Alert on File
</div>
</td>
</tr>
<tr data-ri="1" class="ui-widget-content ui-datatable-odd">
<td>
<div class="ui-dt-c">
9003
</div>
</td>
<td>
<div class="ui-dt-c">
Security Alert or consumer statement on file relates to true name fraud or credit fraud
</div>
</td>
</tr>
<tr data-ri="2" class="ui-widget-content ui-datatable-even">
<td>
<div class="ui-dt-c">
2501
</div>
</td>
<td>
<div class="ui-dt-c">
Input/File (Current/Previous) Address Has Been Used (#) Times In The Last (30,60,90) Days On Different Inquiries
</div>
</td>
</tr>
<tr data-ri="3" class="ui-widget-content ui-datatable-odd">
<td>
<div class="ui-dt-c">
9004
</div>
</td>
<td>
<div class="ui-dt-c">
Active Duty Alert on File
</div>
</td>
</tr>
</tbody>
</table>
</div>
I have implemented a similar function with sahi for ruby. To answer your questions:
rowLen = table.fetch("rows.length").to_i
Loop through all the cells with:
#browser.cell(table,rowIndex,colIndex).exists?()
You can find the corresponding api for sahi java

create a table in td using zend decorator

I am using zend forms and zend decorators.
I want to create a sub-table in a parent table's td. like this:
<table>
<tr>
<td>Username:</td>
<td>
<table>
<tr>
<td> <input type="text"> </td>
<td> <img/> </td>
</tr>
</table>
</td>
</tr>
</table>
How can i create this table.
can anyone help me plz.
This might not be the best solution, as you maybe able to create your own decorator to render the form (with the sub-tables). But you could always render each form element individually, using the renderViewHelper() function
<table>
<tr>
<td>Username:</td>
<td>
<table>
<tr>
<td><?php echo $this->form->username->renderViewHelper() ?></td>
<td> <img/> </td>
</tr>
</table>
</td>
</tr> <tr>
<td>AnotherElement:</td>
<td>
<table>
<tr>
<td><?php echo $this->form->anotherElement->renderViewHelper() ?></td>
<td> <img/> </td>
</tr>
</table>
</td>
</tr>
</table>