0

Here is the code, it produces what I want but not in the way I want to output the result


   import requests
    from bs4 import BeautifulSoup
    url = 'https://en.wikipedia.org/wiki/2020_coronavirus_pandemic_in_Florida'

    fl = requests.get(url)
    fl_soup = BeautifulSoup(fl.text, 'html.parser')
    block = fl_soup.findAll('td', {'class': 'bb-04em'})

    for name in fl_soup.findAll('td', {'class': 'bb-04em'}):
        print(name.text)

output

2020-04-21

27,869(+3.0%)

867

I would like the output like this 2020-04-21 27,869(+3.0%) 867

kevin wholley
  • 85
  • 1
  • 8

3 Answers3

0

The following should do what you want:

import requests
from bs4 import BeautifulSoup
url = 'https://en.wikipedia.org/wiki/2020_coronavirus_pandemic_in_Florida'

fl = requests.get(url)
fl_soup = BeautifulSoup(fl.text, 'html.parser')

div_with_table = fl_soup.find('div', {'class': 'barbox tright'})
table = div_with_table.find('table')

for row in table.findAll('tr'):
    for cell in row.findAll('td', {'class': 'bb-04em'}):
        print(cell.text, end=' ')
    print()  # new line for each row
Jack Moody
  • 1,590
  • 3
  • 21
  • 38
0

Before accesing each <td>, try to get the data by each <tr>, you will get the information of each table row. Then you could search inside <td> or whatever you want.

Juanje
  • 1,235
  • 2
  • 11
  • 28
0

For the last print statement include the end parameter. By default the print statement has end='\n'

print(name.text, end=' ')

This would give you the desired output.