Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do i extract my required data from HTML file?
    primarykey
    data
    text
    <p>This is the HTML I have:</p> <pre><code>p_tags = '''&lt;p class="foo-body"&gt; &lt;font class="test-proof"&gt;Full name&lt;/font&gt; Foobar&lt;br /&gt; &lt;font class="test-proof"&gt;Born&lt;/font&gt; July 7, 1923, foo, bar&lt;br /&gt; &lt;font class="test-proof"&gt;Current age&lt;/font&gt; 27 years 226 days&lt;br /&gt; &lt;font class="test-proof"&gt;Major teams&lt;/font&gt; &lt;span style="white-space: nowrap"&gt;Japan,&lt;/span&gt; &lt;span style="white-space: nowrap"&gt;Jakarta,&lt;/span&gt; &lt;span style="white-space: nowrap"&gt;bazz,&lt;/span&gt; &lt;span style="white-space: nowrap"&gt;foo,&lt;/span&gt; &lt;span style="white-space: nowrap"&gt;foobazz&lt;/span&gt;&lt;br /&gt; &lt;font class="test-proof"&gt;Also&lt;/font&gt; bar&lt;br /&gt; &lt;font class="test-proof"&gt;foo style&lt;/font&gt; hand &lt;br /&gt; &lt;font class="test-proof"&gt;bar style&lt;/font&gt; ball&lt;br /&gt; &lt;font class="test-proof"&gt;foo position&lt;/font&gt; bak&lt;br /&gt; &lt;br class="bar" /&gt; &lt;/p&gt;''' </code></pre> <p>This is my Python code, using Beautiful Soup:</p> <pre><code>def get_info(p_tags): """Returns brief information.""" head_list = [] detail_list = [] # This works fine for head in p_tags.findAll('font', 'test-proof'): head_list.append(head.contents[0]) # Some problem with this? for index in xrange(2, 30, 4): detail_list.append(p_tags.contents[index]) return dict([(l, detail_list[head_list.index(l)]) for l in head_list]) </code></pre> <p>I get the proper <code>head_list</code> from the HTML but the <code>detail_list</code> is not working.</p> <pre> head_list = [u'Full name', u'Born', u'Current age', u'Major teams', u'Also', u'foo style', u'bar style', u'foo position'] </pre> <p>I wanted something like this</p> <pre> { 'Full name': 'Foobar', 'Born': 'July 7, 1923, foo, bar', 'Current age': '78 years 226 days', 'Major teams': 'Japan, Jakarta, bazz, foo, foobazz', 'Also': 'bar', 'foo style': 'hand', 'bar style': 'ball', 'foo position': 'bak' } </pre> <p>Any help would be appreciable. Thanks in advance.</p>
    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. 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