Skip to content
Advertisement

Properly formatting the table after scraped by BeautifulSoup

I am new to Python.

I have been trying to scrape a table from http://www.phc4.org/reports/utilization/inpatient/CountyReport20192C001.htm. The targeted table is titled as “Utilization by Body System”.

I was able to capture the table by using BeautifulSoup; however, the scraped dataframe has been driving me crazy and I could not find a way to address the issue.

My code:

JavaScript

For instance, the scraped the dataframe is printed as:

JavaScript

There are so many newlines in its output as well as the txt file. The ideal txt file should be look like this:

(with no “total cases” in the header)

What should I do to overcome these issues?

Advertisement

Answer

JavaScript

Output:

enter image description here

Advertisement