from this html code I have to extract two values, two separate fields: the first in 'name' and the second in 'value'
<table>
<tbody>
<tr>
<td>
<span><strong>CPU & Dissipatore </strong></span>
</td>
<td>
<span>Intel i7-11700K Dissipatore a Liquido 240mm </span>
</td>
</tr>
</table>
Xpath code:
array(
'name' => ".//table//tr//td[1]",
'value' => ".//table//tr//td[2]",
),
So I get nothing. What am I doing wrong?
Like this:
//tr//td/span
Output
CPU & Dissipatore
Intel i7-11700K Dissipatore a Liquido 240mm
Name/value pairs in XPath (3.1) would be maps not arrays so in XPath 3.1 you can use e.g.
map {
'name' : //table//tr//td[1]/string(),
'value' : //table//tr//td[2]/string()
}
Online sample.
I am trying to retrieve tabular data from a html document stored in my local drive.I am stuck # what to do after parsing i.e how to retrieve those nodes where we have data stored specifically.
<thead>
<tr>
<th></th>
<th data-field="position"><a>Rank</a></th>
<th data-field="name"><a>Brand</a></th>
<th data-field="brandValue"><a>Brand Value</a></th>
<th data-field="oneYearValueChange"><a>1-Yr Value Change</a></th>
<th data-field="revenue"><a>Brand Revenue</a></th>
<th data-field="advertising"><a>Company Advertising</a></th>
<th data-field="industry"><a>Industry</a></th>
</tr>
</thead>
This is the first pat of HTML I want to retrieve , this is the header part for my tabular data.
<tbody id="list-table-body">
<tr class="data">
<td class="image"><img src="./Forbes_files/apple_100x100.jpg" alt=""></td>
<td class="rank">#1 </td>
<td class="name">Apple</td>
<td>$145.3 B</td>
<td>17%</td>
<td>$182.3 B</td>
<td>$1.2 B</td>
<td>Technology</td>
</tr>
<tr class="data">
<td class="image"><img src="./Forbes_files/microsoft_100x100.jpg" alt=""></td>
<td class="rank">#2 </td>
<td class="name">Microsoft</td>
<td>$69.3 B</td>
<td>10%</td>
<td>$93.3 B</td>
<td>$2.3 B</td>
<td>Technology</td>
</tr>
<tr class="data">
<td class="image"><img src="./Forbes_files/google_100x100.jpg" alt=""></td>
<td class="rank">#3 </td>
<td class="name">Google</td>
<td>$65.6 B</td>
<td>16%</td>
<td>$61.8 B</td>
<td>$3 B</td>
<td>Technology</td>
</tr>
This portion of HTML contains the data i.e Rank , Name,and the other statistics.
How can I retrieve both Header and the The data I showed in a dataframe ? Is it possible to retrieve images if I want to ?
Edit : So I looked a little harder and retrieved the data using XpathsAppy which contains class = data , I proceeded to remove "\t" and "\n" , which left me with a character array
fb1 <- htmlParse("forbes.html")
fb2 <- xpathSApply (fb1,"//tr[contains(#class,'data')]",xmlValue)
k3 <- gsub('\\t','',fb2)
k3 <- gsub('\\n',',',k3)
Now k3 is a character array with my data
> k3[1:5]
[1] ",#1 ,Apple,$145.3 B,17%,$182.3 B,$1.2 B,Technology,"
[2] ",#2 ,Microsoft,$69.3 B,10%,$93.3 B,$2.3 B,Technology,"
[3] ",#3 ,Google,$65.6 B,16%,$61.8 B,$3 B,Technology,"
[4] ",#4 ,Coca-Cola,$56 B,0%,$23.1 B,$3.5 B,Beverages,"
[5] ",#5 ,IBM,$49.8 B,4%,$92.8 B,$1.3 B,Technology,"
How do I convert it to a Data Frame ?
Also I wanted the header at the top , but for this k3 charater array , header is at the bottom.
> tail(k3)
[1] ",#96 ,Lancome,$6.2 B,-2%,$4.5 B,-,Consumer Packaged Goods,"
[2] ",#97 ,KIA Motors,$6.2 B,-11%,$42.9 B,$992 M,Automotive,"
[3] ",#98 ,Sprite,$6.2 B,2%,$3.7 B,$3.5 B,Beverages,"
[4] ",#99 ,MTV,$6.2 B,6%,$3.4 B,$1 B,Media,"
[5] ",#100 ,Estee Lauder,$6.1 B,4%,$4.5 B,$2.8 B,Consumer Packaged Goods,"
[6] ",[RANK],[NAME],[BRAND_VALUE],[ONEYEARCHANGE],[REVENUE],[ADVERTISING],[INDUSTRY],
The Rank , Nmae part was supposed to be a header.
I would like any suggestions to improve my code or alternatives as well
I am creating an MVC application. When you use scaffolding templates with your Controllers the Index page is constructed using a table and a foreach which creates a row and the appropriate cells for each instance of the Model in the collection you provide like so:
#model IEnumerable<PoliticiOnline.DTO.Question>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.GeneralQuestion)
</th>
<th>
#Html.DisplayNameFor(model => model.Explanation)
</th>
<th>
#Html.DisplayNameFor(model => model.IsTemplate)
</th>
<th>
#Html.DisplayNameFor(model => model.DateSubmitted)
</th>
<th>
#Html.DisplayNameFor(model => model.JudgementDate)
</th>
<th>
#Html.DisplayNameFor(model => model.FbShares)
</th>
<th>
#Html.DisplayNameFor(model => model.FbLikes)
</th>
<th>
#Html.DisplayNameFor(model => model.TwitterShares)
</th>
<th>
#Html.DisplayNameFor(model => model.SiteVotes)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.GeneralQuestion)
</td>
<td>
#Html.DisplayFor(modelItem => item.Explanation)
</td>
<td>
#Html.DisplayFor(modelItem => item.IsTemplate)
</td>
<td>
#Html.DisplayFor(modelItem => item.DateSubmitted)
</td>
<td>
#Html.DisplayFor(modelItem => item.JudgementDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.FbShares)
</td>
<td>
#Html.DisplayFor(modelItem => item.FbLikes)
</td>
<td>
#Html.DisplayFor(modelItem => item.TwitterShares)
</td>
<td>
#Html.DisplayFor(modelItem => item.SiteVotes)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.QuestionId }) |
#Html.ActionLink("Details", "Details", new { id=item.QuestionId }) |
#Html.ActionLink("Delete", "Delete", new { id=item.QuestionId })
</td>
</tr>
}
</table>
I would like to change this view to something like a listview which contains like a quick look-card for each instance. How would I go about creating this using html and CSS? Basically I would like to create my own structure and present the user with a list with a brief view of each model instance (they can of course see the detail on the detail page).
I hope my explanation was clear, it's very difficult to explain what I mean. You could compare it to defining custom controls, like creating an alternate design for each item in a listview. Hope someone can help me!
EDIT
I actually mean something like the homepage of Stackoverflow. I want to achieve the same sort of structure. I'd like a list with short information of each question. I took a look at the source of Stackoverflow and the CSS but can't really find how to do it. How could you define number of votes goes in the left, title in the middle and author info in bottom right?
I think this is a pretty broad html question and you will probably need to look at some html and css tutorial to really get you going. Your code COULD be as simple as this
#model IEnumerable<PoliticiOnline.DTO.Question>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
#foreach (var item in Model) {
<div style="border: 1px solid black;">
#Html.DisplayFor(modelItem => item.GeneralQuestion ) <br />
#Html.DisplayFor(modelItem => item.Explanation )
</div>
}
Which will produce a bunch of ugly boxes with some text in it. You will really need to understand css to make it look nicer than that. I recommend reading the "Learn Html" and "Learn CSS" sections of http://www.w3schools.com/ .
I am using the HTML Table Class of CodeIgniter, and attempting to create a table template. The example given in the documentation is the following:
$tmpl = array (
'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
'heading_row_start' => '<tr>',
'heading_row_end' => '</tr>',
'heading_cell_start' => '<th>',
'heading_cell_end' => '</th>',
'row_start' => '<tr>',
'row_end' => '</tr>',
'cell_start' => '<td>',
'cell_end' => '</td>',
'row_alt_start' => '<tr>',
'row_alt_end' => '</tr>',
'cell_alt_start' => '<td>',
'cell_alt_end' => '</td>',
'table_close' => '</table>'
);
$this->table->set_template($tmpl);
When I apply this template to my query, this is the resulting HTML:
<table data-hide-table="false" data-orientation="vertical" class="chart">
<thead>
<tr>
<th></th><th>Rate per SF</th></tr>
</thead>
<tbody>
<tr>
<td>2008</td><td>48</td></tr>
<tr>
<td>2009</td><td>32</td></tr>
<tr>
<td>2010</td><td>32</td></tr>
<tr>
<td>2011</td><td>32</td></tr>
<tr>
<td>2012</td><td>40</td></tr>
<tr>
<td>2013</td><td>41</td></tr>
</tbody>
</table>
The problem with this example is that each row contains two data elements (<td></td><td></td>), instead of one header element and one data element (<th></th><td></td>).
How can I change the template so that my resulting table will have one header element and one data element (<th></th><td></td>) on each row like this:
<table data-hide-table="false" data-orientation="vertical" class="chart">
<thead>
<tr>
<th></th><th>Rate per SF</th></tr>
</thead>
<tbody>
<tr>
<th>2008</th><td>48</td></tr>
<tr>
<th>2009</th><td>32</td></tr>
<tr>
<th>2010</th><td>32</td></tr>
<tr>
<th>2011</th><td>32</td></tr>
<tr>
<th>2012</th><td>40</td></tr>
<tr>
<th>2013</th><td>41</td></tr>
</tbody>
</table>
Thanks.
The answer is no you can't change template like this but yes you can somewhere achieve this kind of functionality
If you see the http://ellislab.com/codeigniter/user-guide/libraries/table.html there is a option to add a callable function so I suggest you are creating a table make some flag if you are using that table is generating than create a helper function and add that to the table function than before generating table set a flag to determine the first cell may be using cookie. Than from helper for every 1, 3....(2n+1) add some custom html tag or style. Than after generating the table remove all the flags.
I read many questions and many answers but I couldn't find a straight answer to my question. All the answers were either very general or different from what I want to do. I got so far that i need to use HTML::TableExtract or HTML::TreeBuilder::XPath but I couldn't really use them to store the values. I could somehow get table row values and show them with Dumper.
Something like this:
foreach my $ts ($tree->table_states) {
foreach my $row ($ts->rows) {
push (#fir , (Dumper $row));
} }
print #sec;
But this is not really doing what I'm looking for. I will add the structure of the HTML table that I want to store the values:
<table><caption><b>Table 1 </b>bla bla bla</caption>
<tbody>
<tr>
<th ><p>Foo</p>
</th>
<td ><p>Bar</p>
</td>
</tr>
<tr>
<th ><p>Foo-1</p>
</th>
<td ><p>Bar-1</p>
</td>
</tr>
<tr>
<th ><p>Formula</p>
</th>
<td><p>Formula1-1</p>
<p>Formula1-2</p>
<p>Formula1-3</p>
<p>Formula1-4</p>
<p>Formula1-5</p>
</td>
</tr>
<tr>
<th><p>Foo-2</p>
</th>
<td ><p>Bar-2</p>
</td>
</tr>
<tr>
<th ><p>Foo-3</p>
</th>
<td ><p>Bar-3</p>
<p>Bar-3-1</p>
</td>
</tr>
</tbody>
</table>
It would be convenient if I can store the row values as pairs together.
expected output would be something like an array with values of:
(Foo , Bar , Foo-1 , Bar-1 , Formula , Formula-1 Formula-2 Formula-3 Formula-4 Formula-5 , ....)
The important thing for me is to learn how to store the values of each tag and how to move around in the tag tree.
Learn XPath and DOM manipulation.
use strictures;
use HTML::TreeBuilder::XPath qw();
my $dom = HTML::TreeBuilder::XPath->new;
$dom->parse_file('10280979.html');
my %extract;
#extract{$dom->findnodes_as_strings('//th')} =
map {[$_->findvalues('p')]} $dom->findnodes('//td');
__END__
# %extract = (
# Foo => [qw(Bar)],
# 'Foo-1' => [qw(Bar-1)],
# 'Foo-2' => [qw(Bar-2)],
# 'Foo-3' => [qw(Bar-3 Bar-3-1)],
# Formula => [qw(Formula1-1 Formula1-2 Formula1-3 Formula1-4 Formula1-5)],
# )