Thanks in advance for taking the time to try to help. I am using the Jade templating engine in node.js, and want to generate an HTML table with rows grouped into sections (tbody tags).
Let's say I have the following object in memory:
[
{ type: 'Fruit', name: 'Apple' }
, { type: 'Fruit', name: 'Orange' }
, { type: 'Vegetable', name: 'Carrot' }
, { type: 'Vegetable', name: 'Spinach'}
]
(for simplicity, let's assume the array comes pre-ordered by the "type" column).
And I want to generate a table with rows for each object inside a tbody section for each "type" (Fruit vs. Vegetable). So the HTML I'm trying to generate is:
<table>
<thead>
<th>Type</th>
<th>Name</th>
</thead>
<tbody id="Fruit">
<tr>
<td>Fruit</td>
<td>Apple</td>
</tr>
<tr>
<td>Fruit</td>
<td>Orange</td>
</tr>
</tbody>
<tbody id="Vegetable">
<tr>
<td>Vegetable</td>
<td>Carrot</td>
</tr>
<tr>
<td>Vegetable</td>
<td>Spinach</td>
</tr>
</tbody>
</table>
I think I would want my jade to look something like:
table
thead
th Type
th Name
- var lastType;
each o in objs
- if(o.type != lastType)
tbody(id='#{o.type}')
- lastType = o.type;
tr
td #{o.type}
td #{o.name}
But this generates:
<table>
<thead>
<th>Type</th>
<th>Name</th>
</thead>
<tbody id="Fruit" />
<tbody>
<tr>
<td>Fruit</td>
<td>Apple</td>
</tr>
<tr>
<td>Fruit</td>
<td>Orange</td>
</tr>
</tbody>
<tbody id="Vegetable" />
<tbody>
<tr>
<td>Vegetable</td>
<td>Carrot</td>
</tr>
<tr>
<td>Vegetable</td>
<td>Spinach</td>
</tr>
</tbody>
</table>
Any ideas?
This is in Jade kinda hard to guess, because we don't really see how your indentations are really set in your local code, but here(your posted snippet) it seems like your indentations are not right.
You should try this: +Update (added another loop to get all same types under each body)
table
thead
th Type
th Name
- var lastType;
each o in objs
- if (o.type != lastType)
- lastType = o.type;
tbody(id='#{o.type}')
each t in objs
- if(t.type === lastType)
tr
td #{t.type}
td #{t.name}
Just shift the both tr tags into your tbody, like my code example above.
+Update This will produce this HTML code now:
<table>
<thead>
<th>Type</th>
<th>Name</th>
</thead>
<tbody id="Fruit">
<tr>
<td>Fruit </td>
<td>Apple</td>
</tr>
<tr>
<td>Fruit </td>
<td>Orange</td>
</tr>
</tbody>
<tbody id="Vegetable">
<tr>
<td>Vegetable </td>
<td>Carrot</td>
</tr>
<tr>
<td>Vegetable </td>
<td>Spinach</td>
</tr>
</tbody>
</table>
table
thead
th Type
th Name
tbody#Fruit
tbody
tr
td Fruit
td Apple
tr
td Fruit
td Orange
tbody#Vegetable
tbody
tr
td Vegetable
td Carrot
tr
td Vegetable
td Spinach
You should go with pug basic formate to list the following process
Related
HTML:
<tbody>
<tr >
<td> Tim Cook </td>
<td class="wpsTableNrmRow" > Apple CEO
<a href:applicatiodetailaddress> all CEOs </a> // Nor required this node
</td>
</tr>
<tr >
<td> Sundar Pichai </td>
<td class="wpsTableNrmRow" > Google CEO </td>
</tr>
<tr >
<td> NoCompany </td>
<td class="wpsTableNrmRow" > NOT, DEFINED</td>
</tr>
</tbody>
Code:
applicationData = [td.text for td in webBrowser.find_elements_by_xpath('//td[#class="wpsTableNrmRow"]')]
record = {'Designation': applicationData[0],
'Designation': applicationData[1],'Designation': applicationData[2]}
OUTPUT:
Designation: Apple CEO all CEOs // Not required 'all CEOs'
Designation: Google CEO
Designation: Not, DEFINED
I am scraping data from the table and the <a tag is also scraped. I don't want to scrape <a tag.
How can I do this?
I tried [td.get_attribute("textContent").split("\n")[0] for td in webBrowser.find_elements_by_xpath('//td[#class="wpsTableNrmRow" and text()!=" "]')]
OUTPUT:
Designation: Apple CEO
Designation: Google CEO
Designation: // should have value 'NOT, DEFINED'
How to get value?
applicationData = [td.get_attribute("textContent").split("\n")[0] for td in webBrowser.find_elements_by_xpath('//td[#class="wpsTableNrmRow"]')]
record = {'Designation1': applicationData[0], 'Designation2': applicationData[1]}
Try above code , here we use TextCOntent and it returns different text nodes in different lines so you can split it using "\n"
i'd like to get the td tag value '1,227.90' in below html.
How can i get this vaule using beautifulsoup module?
Thanks.
<h3 class="h_exchange"><span>AAAAAA</span></h3>
<table class="tbl_exchange" summary="XXXXXXXXXXXXXXXXXxx">
<caption>XXXXXXXXXXXXXXXXXxx</caption>
<colgroup>
<col>
<col width="120">
</colgroup>
<thead>
<tr>
<th>XXXXXXXXXXXXXXXXXxx</th>
<th>XXXXXXXXXXXXXXXXXxx</th>
</tr>
</thead>
<tbody>
<tr>
<th class="th_ex4"><span>****************</span></th>
<td>1,227.90</td>
</tr>
You could use CSS selector table.tbl_exchange td, which will select all tags <td> under the <table> with class tbl_exchange:
data = '''<h3 class="h_exchange"><span>AAAAAA</span></h3>
<table class="tbl_exchange" summary="XXXXXXXXXXXXXXXXXxx">
<caption>XXXXXXXXXXXXXXXXXxx</caption>
<colgroup>
<col>
<col width="120">
</colgroup>
<thead>
<tr>
<th>XXXXXXXXXXXXXXXXXxx</th>
<th>XXXXXXXXXXXXXXXXXxx</th>
</tr>
</thead>
<tbody>
<tr>
<th class="th_ex4"><span>****************</span></th>
<td>1,227.90</td>
</tr>'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(data, 'html.parser')
for td in soup.select('table.tbl_exchange td'):
print(td.text)
Prints:
1,227.90
I'm trying to create a custom table button using Jodit React Editor using this as reference - https://xdsoft.net/jodit/examples/toolbar/custom_button.html
I'm a little lost on this though.
I need to be able to create a table and have the icon be a text icon that says - "Table"
Right now I've added this to my configurations - extraButtons: ['tableNew'].
I've also added the below code to the render method.
this.jodit.options.controls.tableNew = {
iconURL: '',
exec: function (editor) {
return '<table> <thead> <tr> <th> Sl no </th> <th>Name</th> <th>Age</th> </tr> </thead>'+
' <tbody> <tr> <td>1</td> <td></td> <td></td> </tr> </tbody> </table>';
}
};
I see that a space has been added in the toolbar which on hover says tableNew but on clicking it nothing happens.
I'd really appreciate it if someone could help me out with this.
Instead of the following,
return '<table> <thead> <tr> <th> Sl no </th> <th>Name</th> <th>Age</th> </tr> </thead> <tbody> <tr> <td>1</td> <td></td> <td></td> </tr> </tbody> </table>';
try something like this:
return editor.create.fromHTML('<table> <thead> <tr> <th> Sl no </th> <th>Name</th> <th>Age</th> </tr> </thead> <tbody> <tr> <td>1</td> <td></td> <td></td> </tr> </tbody> </table>');
This works for me (a similiar implementation, not returning table like you, but return some list).
I am using python3.6 with XPath library. Crawling inside the table gives me an empty list. And need to crawl to specific th.
My tr contents are dynamically generated. I need to crawl to tr which has a specific th value. Example In HTML code, the Rank appears in the second tr but it can appear in anywhere in tr. It doesn't have a specific index. Need to get the href from the tr having the Rank th.
My html file:
<tbody>
<tr>
<th class="a-color-secondary a-size-base prodDetSectionEntry">
Product Number
</th>
<td class="a-size-base">
B003NR57BY
</td>
</tr>
<tr>
<th class="a-color-secondary a-size-base prodDetSectionEntry">
Rank
</th>
<td>
<span>
<span>#3 in Computer Mice</span>
<br>
</span>
</td>
</tr>
<tr>
<th class="a-color-secondary a-size-base prodDetSectionEntry">
Created Date
</th>
<td class="a-size-base">
June 7, 2010
</td>
</tr>
</tbody>
</table>
Python code:
listings_details = parser.xpath(XPATH_PRODUCT_DETAILS)
for row in listings_details:
th = row.xpath("./th/text()")
if th[0].strip() == 'Rank':
categories = row.xpath("./td/span/span//text()")
qid_url= row.xpath("./td/span/span//#href")
I expect the output to be
Rank: 3,
url : /gp/bestsellers/pc/11036491/ref=pd_zg_hrsr_pc_1_1_last,
category: Computer Mice
Need to get the href from the tr having the Rank th.
Use:
/table/tbody/tr[normalize-space(th)='Rank']/td//a/#href
Note: this works for your provided fragment (now well-formed). You need to add later a context for selecting the table element.
<table>
<tbody>
<tr>
<th class="a-color-secondary a-size-base prodDetSectionEntry">Product Number</th>
<td class="a-size-base">B003NR57BY</td>
</tr>
<tr>
<th class="a-color-secondary a-size-base prodDetSectionEntry">Rank</th>
<td>
<span>
<span>#3 in
Computer Mice
</span>
<br/>
</span>
</td>
</tr>
<tr>
<th class="a-color-secondary a-size-base prodDetSectionEntry">Created Date</th>
<td class="a-size-base">June 7, 2010</td>
</tr>
</tbody>
</table>
Test in http://www.xpathtester.com/xpath/53808ee94dfbc5b38f12791cf857ffb9
i'm trying to get a complex (ugly) nested table with no id but with a class name = "tableselect" then read all tr's and td's
the only quick and unique way to find the table - it is within a div tag, then a center tag.
this is the html structure.
<div align="center">
<center>
<table border='1' width='100%' cellspacing='0' cellpadding='5' class="tableselect">
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
...
</table>
</center>
</div>
the .getElementsByTagName("center").getElementsByTagName("table") does not work
also
.getElementsByClassName("tableselect").getElementsByTagName("tr") does not work
using excel 2016