-4

I want to scrape a HTML table and store it into a JSON file. I successfully scraped the html table data but cannot figure out how to store the scraped data into the JSON file.

Here is my code:

import requests  
from bs4 import BeautifulSoup  

r = requests.get('url')

soup = BeautifulSoup(r.text, 'html.parser')
resultset = soup.find_all('table', attrs={'style':'font-weight:bold'})
records = []  
for result in resultset:  
    heading = result.find('tr')
    records.append((heading))
print(heading)

I get the HTML table data in the form:

<table>
    <thead>
        <tr>
            <th>Serial Number</th>
            <th>Name</th>
            <th>number1</th>
            <th>number2</th>
            <th>number3</th>
            <th>number4</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            <td>John</td>
            <td>22</td>
            <td>4</td>
            <td>12</td>
            <td>18</td>
        </tr>
        <tr>
            <td>1</td>
            <td>Chris</td>
            <td>26</td>
            <td>6</td>
            <td>10</td>
            <td>1</td>
        </tr>
    </tbody>
</table>

How can I store this HTML table data in JSON file. The required JSON format is:

[
 {
   "Serial Number": 1,
   "Name": "John",
   "number1": 22,
   "number2": 4,
   "number3": 12,
   "number4": 18
 },
 {
   "Serial Number": 1,
   "Name": "Chris",
   "number1": 26,
   "number2": 6,
   "number3": 10,
   "number4": 1
 }
]
Aman14
  • 1
  • 1
  • Can you clarify what exactly the issue is? Please see [ask], [help/on-topic]. – AMC Mar 23 '20 at 23:50
  • I want to create a JSON file as shown in last part from the HTML table pretty much whats done in this https://stackoverflow.com/questions/18544634/convert-a-html-table-to-json – Aman14 Mar 24 '20 at 00:09
  • That's the goal, which was already stated in your post, I'm asking about the problem. – AMC Mar 24 '20 at 00:12

1 Answers1

1

Look html is a kind of xml, you have xml parsers, try one of them. As a tip i give you this site: https://www.codespeedy.com/how-to-convert-xml-to-json-in-python/