Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>The Basic Issue</h2> <p>The parser is behaving correctly. You are just using the same expressions to parse different types of elements. </p> <h2>Revised code</h2> <p>Here is a snippet, focusing only on returning scraped lists. Once you have the lists, you can format the text table easily:</p> <pre><code>import BeautifulSoup def get_queue(data): # Args: # data: string, contains the html to be scraped soup = BeautifulSoup.BeautifulSoup(data) stable = soup.find('table') header = stable.findAll('th') headers = [ th.text for th in header ] cells = [ ] rows = stable.findAll('tr') for tr in rows[1:-2]: # Process the body of the table row = [] td = tr.findAll('td') row.append( td[0].find('a').text ) row.append( td[1].find('a').text ) row.extend( [ td.text for td in td[2:] ] ) cells.append( row ) footer = rows[-1].find('td').text return headers, cells, footer </code></pre> <h2>Output</h2> <p><code>headers</code>, <code>cells</code>, and <code>footer</code>, cells can now be fed into a <code>texttable</code> formatting function:</p> <pre><code>import texttable def show_table(headers, cells, footer): retval = '' table = texttable.Texttable() table.header(headers) for cell in cells: table.add_row(cell) retval = table.draw() return retval + '\n' + footer print show_table(headers, cells, footer) </code></pre> <pre class="lang-none prettyprint-override"><code>+----------+----------+----------+----------+----------+----------+----------+ | Artist - | Album | Album | Series | Duration | Type of | Time to | | Title | | Type | | | Play | play | +==========+==========+==========+==========+==========+==========+==========+ | Song 1 | Album 1 | ... | | 5:43 | S.A.M. | ~0:00:00 | +----------+----------+----------+----------+----------+----------+----------+ | Song2 | Album 2 | ... | | 6:16 | S.A.M. | ~0:05:43 | +----------+----------+----------+----------+----------+----------+----------+ | Song 3 | Album 3 | ... | | 4:13 | S.A.M. | ~0:11:59 | +----------+----------+----------+----------+----------+----------+----------+ | Song 4 | Album 4 | ... | | 5:34 | S.A.M. | ~0:16:12 | +----------+----------+----------+----------+----------+----------+----------+ | Song 5 | Album 5 | ... | | 4:23 | S.A.M. | ~0:21:46 | +----------+----------+----------+----------+----------+----------+----------+ There are x songs in the queue with a total length of x:y:z. </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload