Home > database >  getting beautiful soup to read 2nd pre table
getting beautiful soup to read 2nd pre table

Time:07-20

I am attempting to scrape a simple website that has that only has one table, but is the second tag. However, i cannot get bs4 to return any data, it's only returning a null value.

Any advice is appreciated [beautiful soup code here]

CodePudding user response:

from bs4 import BeautifulSoup
import requests

headers= {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'}

url = 'https://www.usbr.gov/pn-bin/instant.pl?parameter=CHRO q&syer=2022&smnth=7&sdy=12&eyer=2022&emnth=7&edy=19&format=2'
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.text, 'lxml')
data = soup.select('pre')[1]
print(data.text.strip())

Result:

BEGIN DATA
DATE       TIME ,  CHRO    Q       
07/12/2022 00:00,      10.60 
07/12/2022 00:15,      10.60 
07/12/2022 00:30,      10.60 
07/12/2022 00:45,      10.60 
07/12/2022 01:00,      10.60 
07/12/2022 01:15,      10.60 
07/12/2022 01:30,      10.60 
07/12/2022 01:45,      10.60 
07/12/2022 02:00,      10.60 
07/12/2022 02:15,      10.60 
07/12/2022 02:30,      10.60 
07/12/2022 02:45,      10.60 
07/12/2022 03:00,      10.60 
07/12/2022 03:15,      10.60 
07/12/2022 03:30,      10.60 
07/12/2022 03:45,      10.60 
07/12/2022 04:00,      10.60 
07/12/2022 04:15,      10.60 
07/12/2022 04:30,      10.60 
07/12/2022 04:45,      10.60 
07/12/2022 05:00,      11.00 
07/12/2022 05:15,      10.60 
07/12/2022 05:30,      10.60 
07/12/2022 05:45,      10.60 
07/12/2022 06:00,      11.00 
07/12/2022 06:15,      11.00 
07/12/2022 06:30,      10.60 
07/12/2022 06:45,      10.60 
07/12/2022 07:00,      10.60 
07/12/2022 07:15,      11.00 
07/12/2022 07:30,      11.00 
07/12/2022 07:45,      10.60 
07/12/2022 08:00,      11.00 
07/12/2022 08:15,      11.00 
07/12/2022 08:30,      11.00 
07/12/2022 08:45,      11.00 
07/12/2022 09:00,      11.00 
07/12/2022 09:15,      11.00 
07/12/2022 09:30,      11.00 
07/12/2022 09:45,      11.00 
07/12/2022 10:00,      11.00 
07/12/2022 10:15,      10.60 
07/12/2022 10:30,      11.00 
07/12/2022 10:45,      11.00 
07/12/2022 11:00,      11.00 
07/12/2022 11:15,      11.00 
07/12/2022 11:30,      11.00 
07/12/2022 11:45,      11.00 
07/12/2022 12:00,      11.00 
07/12/2022 12:15,      10.60 
07/12/2022 12:30,      10.60 
07/12/2022 12:45,      10.60 
07/12/2022 13:00,      11.00 
07/12/2022 13:15,      10.60 
07/12/2022 13:30,      10.60 
07/12/2022 13:45,      10.60 
07/12/2022 14:00,      10.60 
07/12/2022 14:15,      10.60 
07/12/2022 14:30,      10.60 
07/12/2022 14:45,      10.60 
07/12/2022 15:00,      10.60 
07/12/2022 15:15,      10.60 
07/12/2022 15:30,      10.60 
07/12/2022 15:45,      10.60 
07/12/2022 16:00,      10.60 
07/12/2022 16:15,      10.60 
07/12/2022 16:30,      10.30 
07/12/2022 16:45,      10.60 
07/12/2022 17:00,      10.30 
07/12/2022 17:15,      10.60 
07/12/2022 17:30,      10.60 
07/12/2022 17:45,      10.30 
07/12/2022 18:00,      10.30 
07/12/2022 18:15,      10.60 
07/12/2022 18:30,      10.30 
07/12/2022 18:45,      10.30 
07/12/2022 19:00,      10.30 
07/12/2022 19:15,      10.30 
07/12/2022 19:30,      10.30 
07/12/2022 19:45,      10.30 
07/12/2022 20:00,      10.30 
07/12/2022 20:15,      10.30 
07/12/2022 20:30,      10.30 
07/12/2022 20:45,      10.30 
07/12/2022 21:00,      10.30 
07/12/2022 21:15,      10.30 
07/12/2022 21:30,      10.30 
07/12/2022 21:45,      10.30 
07/12/2022 22:00,      10.30 
07/12/2022 22:15,      10.30 
07/12/2022 22:30,      10.60 
07/12/2022 22:45,      10.30 
07/12/2022 23:00,      10.30 
07/12/2022 23:15,      10.30 
07/12/2022 23:30,      10.60 
07/12/2022 23:45,      10.30 
07/13/2022 00:00,      10.30 
07/13/2022 00:15,      10.30 
07/13/2022 00:30,      10.30 
07/13/2022 00:45,      10.30 
07/13/2022 01:00,      10.30 
07/13/2022 01:15,      10.60 
07/13/2022 01:30,      10.30 
07/13/2022 01:45,      10.60 
07/13/2022 02:00,      10.30 
07/13/2022 02:15,      10.60 
07/13/2022 02:30,      10.60 
07/13/2022 02:45,      10.30 
07/13/2022 03:00,      10.60 
07/13/2022 03:15,      10.60 
07/13/2022 03:30,      10.60 
07/13/2022 03:45,      10.60 
07/13/2022 04:00,      10.60 
07/13/2022 04:15,      10.60 
07/13/2022 04:30,      10.60 
07/13/2022 04:45,      10.60 
07/13/2022 05:00,      10.60 
07/13/2022 05:15,      10.60 
07/13/2022 05:30,      10.60 
07/13/2022 05:45,      10.60 
07/13/2022 06:00,      10.60 
07/13/2022 06:15,      10.60 
07/13/2022 06:30,      10.60 
07/13/2022 06:45,      10.60 
07/13/2022 07:00,      10.60 
07/13/2022 07:15,      10.60 
07/13/2022 07:30,      10.60 
07/13/2022 07:45,      10.60 
07/13/2022 08:00,      10.60 
07/13/2022 08:15,      10.60 
07/13/2022 08:30,      10.60 
07/13/2022 08:45,      10.60 
07/13/2022 09:00,      10.60 
07/13/2022 09:15,      10.60 
07/13/2022 09:30,      10.60 
07/13/2022 09:45,      10.60 
07/13/2022 10:00,      10.60 
07/13/2022 10:15,      10.60 
07/13/2022 10:30,      10.60 
07/13/2022 10:45,      11.00 
07/13/2022 11:00,      10.60 
07/13/2022 11:15,      10.60 
07/13/2022 11:30,      10.60 
07/13/2022 11:45,      10.60 
07/13/2022 12:00,      10.60 
07/13/2022 12:15,      10.60 
07/13/2022 12:30,      10.60 
07/13/2022 12:45,      10.60 
07/13/2022 13:00,      10.60 
07/13/2022 13:15,      10.60 
07/13/2022 13:30,      10.60 
07/13/2022 13:45,      10.30 
07/13/2022 14:00,      10.30 
07/13/2022 14:15,      10.30 
07/13/2022 14:30,      10.60 
07/13/2022 14:45,      10.30 
07/13/2022 15:00,      10.30 
07/13/2022 15:15,      10.30 
07/13/2022 15:30,      10.60 
07/13/2022 15:45,      10.30 
07/13/2022 16:00,      10.30 
07/13/2022 16:15,      10.30 
07/13/2022 16:30,      10.60 
07/13/2022 16:45,      10.30 
07/13/2022 17:00,      10.30 
07/13/2022 17:15,      10.30 
07/13/2022 17:30,      10.30 
07/13/2022 17:45,      10.30 
07/13/2022 18:00,      10.30 
07/13/2022 18:15,      10.30 
07/13/2022 18:30,      10.30 
07/13/2022 18:45,      10.30 
07/13/2022 19:00,      10.30 
07/13/2022 19:15,      10.30 
07/13/2022 19:30,       9.99 
07/13/2022 19:45,       9.99 
07/13/2022 20:00,       9.99 
07/13/2022 20:15,      10.30 
07/13/2022 20:30,      10.30 
07/13/2022 20:45,      10.30 
07/13/2022 21:00,       9.99 
07/13/2022 21:15,      10.30 
07/13/2022 21:30,      10.30 
07/13/2022 21:45,       9.99 
07/13/2022 22:00,      10.30 
07/13/2022 22:15,       9.99 
07/13/2022 22:30,       9.99 
07/13/2022 22:45,       9.99 
07/13/2022 23:00,      10.30 
07/13/2022 23:15,      10.30 
07/13/2022 23:30,       9.99 
07/13/2022 23:45,      10.30 
07/14/2022 00:00,      10.30 
07/14/2022 00:15,      10.30 
07/14/2022 00:30,      10.30 
07/14/2022 00:45,      10.30 
07/14/2022 01:00,      10.30 
07/14/2022 01:15,      10.30 
07/14/2022 01:30,      10.30 
07/14/2022 01:45,      10.30 
07/14/2022 02:00,      10.30 
07/14/2022 02:15,      10.30 
07/14/2022 02:30,      10.30 
07/14/2022 02:45,      10.30 
07/14/2022 03:00,      10.30 
07/14/2022 03:15,      10.30 
07/14/2022 03:30,      10.30 
07/14/2022 03:45,      10.30 
07/14/2022 04:00,      10.30 
07/14/2022 04:15,      10.30 
07/14/2022 04:30,      10.30 
07/14/2022 04:45,      10.30 
07/14/2022 05:00,      10.30 
07/14/2022 05:15,      10.30 
07/14/2022 05:30,      10.30 
07/14/2022 05:45,      10.30 
07/14/2022 06:00,      10.30 
07/14/2022 06:15,      10.30 
07/14/2022 06:30,      10.30 
07/14/2022 06:45,      10.30 
07/14/2022 07:00,      10.30 
07/14/2022 07:15,      10.30 
07/14/2022 07:30,      10.30 
07/14/2022 07:45,      10.30 
07/14/2022 08:00,      10.30 
07/14/2022 08:15,      10.60 
07/14/2022 08:30,      10.60 
07/14/2022 08:45,      10.60 
07/14/2022 09:00,      10.60 
07/14/2022 09:15,      10.30 
07/14/2022 09:30,      10.30 
07/14/2022 09:45,      10.60 
07/14/2022 10:00,      10.30 
07/14/2022 10:15,      10.30 
07/14/2022 10:30,      10.30 
07/14/2022 10:45,      10.30 
07/14/2022 11:00,      10.60 
07/14/2022 11:15,      10.60 
07/14/2022 11:30,      10.60 
07/14/2022 11:45,      10.30 
07/14/2022 12:00,      10.30 
07/14/2022 12:15,      10.60 
07/14/2022 12:30,      10.60 
07/14/2022 12:45,      10.30 
07/14/2022 13:00,      10.30 
07/14/2022 13:15,      10.30 
07/14/2022 13:30,      10.30 
07/14/2022 13:45,      10.30 
07/14/2022 14:00,      10.30 
07/14/2022 14:15,      10.30 
07/14/2022 14:30,      10.30 
07/14/2022 14:45,      10.30 
07/14/2022 15:00,      10.30 
07/14/2022 15:15,      10.30 
07/14/2022 15:30,      10.30 
07/14/2022 15:45,      10.30 
07/14/2022 16:00,      10.30 
07/14/2022 16:15,       9.99 
07/14/2022 16:30,       9.99 
07/14/2022 16:45,       9.99 
07/14/2022 17:00,       9.99 
07/14/2022 17:15,      10.30 
07/14/2022 17:30,       9.99 
07/14/2022 17:45,       9.99 
07/14/2022 18:00,       9.99 
07/14/2022 18:15,       9.99 
07/14/2022 18:30,       9.99 
07/14/2022 18:45,       9.99 
07/14/2022 19:00,      10.30 
07/14/2022 19:15,       9.99 
07/14/2022 19:30,       9.99 
07/14/2022 19:45,       9.99 
07/14/2022 20:00,       9.99 
07/14/2022 20:15,       9.99 
07/14/2022 20:30,       9.99 
07/14/2022 20:45,       9.99 
07/14/2022 21:00,       9.99 
07/14/2022 21:15,       9.99 
07/14/2022 21:30,       9.99 
07/14/2022 21:45,       9.99 
07/14/2022 22:00,       9.99 
07/14/2022 22:15,       9.99 
07/14/2022 22:30,       9.99 
07/14/2022 22:45,       9.99 
07/14/2022 23:00,       9.99 
07/14/2022 23:15,       9.99 
07/14/2022 23:30,       9.99 
07/14/2022 23:45,       9.99 
07/15/2022 00:00,       9.99 
07/15/2022 00:15,      10.30 
07/15/2022 00:30,       9.99 
07/15/2022 00:45,       9.99 
07/15/2022 01:00,       9.99 
07/15/2022 01:15,       9.99 
07/15/2022 01:30,       9.99 
07/15/2022 01:45,       9.99 
07/15/2022 02:00,      10.30 
07/15/2022 02:15,       9.99 
07/15/2022 02:30,      10.30 
07/15/2022 02:45,      10.30 
07/15/2022 03:00,       9.99 
07/15/2022 03:15,      10.30 
07/15/2022 03:30,      10.30 
07/15/2022 03:45,       9.99 
07/15/2022 04:00,      10.30 
07/15/2022 04:15,       9.99 
07/15/2022 04:30,      10.30 
07/15/2022 04:45,      10.30 
07/15/2022 05:00,      10.30 
07/15/2022 05:15,      10.30 
07/15/2022 05:30,      10.30 
07/15/2022 05:45,      10.30 
07/15/2022 06:00,      10.30 
07/15/2022 06:15,       9.99 
07/15/2022 06:30,      10.30 
07/15/2022 06:45,      10.30 
07/15/2022 07:00,      10.30 
07/15/2022 07:15,      10.30 
07/15/2022 07:30,      10.30 
07/15/2022 07:45,      10.30 
07/15/2022 08:00,      10.30 
07/15/2022 08:15,      10.30 
07/15/2022 08:30,      10.30 
07/15/2022 08:45,      10.30 
07/15/2022 09:00,      10.30 
07/15/2022 09:15,      10.30 
07/15/2022 09:30,      10.30 
07/15/2022 09:45,      10.30 
07/15/2022 10:00,      10.30 
07/15/2022 10:15,      10.30 
07/15/2022 10:30,      10.60 
07/15/2022 10:45,      10.30 
07/15/2022 11:00,      10.30 
07/15/2022 11:15,      10.30 
07/15/2022 11:30,      10.30 
07/15/2022 11:45,      10.30 
07/15/2022 12:00,      10.30 
07/15/2022 12:15,      10.30 
07/15/2022 12:30,      10.30 
07/15/2022 12:45,      10.30 
07/15/2022 13:00,      10.30 
07/15/2022 13:15,      10.30 
07/15/2022 13:30,      10.30 
07/15/2022 13:45,       9.99 
07/15/2022 14:00,      10.30 
07/15/2022 14:15,      10.30 
07/15/2022 14:30,      10.30 
07/15/2022 14:45,       9.99 
07/15/2022 15:00,       9.99 
07/15/2022 15:15,       9.99 
07/15/2022 15:30,       9.99 
07/15/2022 15:45,       9.99 
07/15/2022 16:00,       9.99 
07/15/2022 16:15,       9.99 
07/15/2022 16:30,       9.99 
07/15/2022 16:45,       9.99 
07/15/2022 17:00,       9.99 
07/15/2022 17:15,       9.99 
07/15/2022 17:30,       9.99 
07/15/2022 17:45,       9.99 
07/15/2022 18:00,       9.99 
07/15/2022 18:15,       9.67 
07/15/2022 18:30,       9.67 
07/15/2022 18:45,       9.99 
07/15/2022 19:00,       9.67 
07/15/2022 19:15,       9.67 
07/15/2022 19:30,       9.67 
07/15/2022 19:45,       9.67 
07/15/2022 20:00,       9.67 
07/15/2022 20:15,       9.99 
07/15/2022 20:30,       9.67 
07/15/2022 20:45,       9.99 
07/15/2022 21:00,       9.67 
07/15/2022 21:15,       9.99 
07/15/2022 21:30,       9.67 
07/15/2022 21:45,       9.67 
07/15/2022 22:00,       9.99 
07/15/2022 22:15,       9.99 
07/15/2022 22:30,       9.99 
07/15/2022 22:45,       9.99 
07/15/2022 23:00,       9.67 
07/15/2022 23:15,       9.67 
07/15/2022 23:30,       9.99 
07/15/2022 23:45,       9.99 
07/16/2022 00:00,       9.99 
07/16/2022 00:15,       9.99 
07/16/2022 00:30,       9.99 
07/16/2022 00:45,       9.99 
07/16/2022 01:00,       9.99 
07/16/2022 01:15,       9.99 
07/16/2022 01:30,       9.99 
07/16/2022 01:45,       9.99 
07/16/2022 02:00,       9.99 
07/16/2022 02:15,       9.99 
07/16/2022 02:30,       9.99 
07/16/2022 02:45,       9.99 
07/16/2022 03:00,       9.99 
07/16/2022 03:15,       9.99 
07/16/2022 03:30,       9.99 
07/16/2022 03:45,       9.99 
07/16/2022 04:00,       9.99 
07/16/2022 04:15,       9.99 
07/16/2022 04:30,       9.99 
07/16/2022 04:45,       9.99 
07/16/2022 05:00,       9.99 
07/16/2022 05:15,       9.99 
07/16/2022 05:30,       9.99 
07/16/2022 05:45,       9.99 
07/16/2022 06:00,      10.30 
07/16/2022 06:15,       9.99 
07/16/2022 06:30,       9.99 
07/16/2022 06:45,       9.99 
07/16/2022 07:00,      10.30 
07/16/2022 07:15,      10.30 
07/16/2022 07:30,      10.30 
07/16/2022 07:45,      10.30 
07/16/2022 08:00,      10.30 
07/16/2022 08:15,      10.30 
07/16/2022 08:30,       9.99 
07/16/2022 08:45,       9.99 
07/16/2022 09:00,       9.99 
07/16/2022 09:15,      10.30 
07/16/2022 09:30,      10.30 
07/16/2022 09:45,      10.30 
07/16/2022 10:00,      10.30 
07/16/2022 10:15,      10.30 
07/16/2022 10:30,      10.30 
07/16/2022 10:45,      10.30 
07/16/2022 11:00,      10.30 
07/16/2022 11:15,      10.30 
07/16/2022 11:30,      10.30 
07/16/2022 11:45,      10.30 
07/16/2022 12:00,      10.30 
07/16/2022 12:15,      10.30 
07/16/2022 12:30,      10.30 
07/16/2022 12:45,      10.30 
07/16/2022 13:00,      10.30 
07/16/2022 13:15,       9.99 
07/16/2022 13:30,       9.99 
07/16/2022 13:45,       9.99 
07/16/2022 14:00,       9.99 
07/16/2022 14:15,       9.99 
07/16/2022 14:30,       9.99 
07/16/2022 14:45,       9.99 
07/16/2022 15:00,       9.99 
07/16/2022 15:15,       9.99 
07/16/2022 15:30,       9.99 
07/16/2022 15:45,       9.99 
07/16/2022 16:00,       9.99 
07/16/2022 16:15,       9.99 
07/16/2022 16:30,       9.67 
07/16/2022 16:45,       9.67 
07/16/2022 17:00,       9.67 
07/16/2022 17:15,       9.67 
07/16/2022 17:30,       9.67 
07/16/2022 17:45,       9.99 
07/16/2022 18:00,       9.99 
07/16/2022 18:15,       9.67 
07/16/2022 18:30,       9.67 
07/16/2022 18:45,       9.67 
07/16/2022 19:00,       9.67 
07/16/2022 19:15,       9.67 
07/16/2022 19:30,       9.67 
07/16/2022 19:45,       9.67 
07/16/2022 20:00,       9.67 
07/16/2022 20:15,       9.67 
07/16/2022 20:30,       9.67 
07/16/2022 20:45,       9.67 
07/16/2022 21:00,       9.67 
07/16/2022 21:15,       9.67 
07/16/2022 21:30,       9.67 
07/16/2022 21:45,       9.67 
07/16/2022 22:00,       9.67 
07/16/2022 22:15,       9.67 
07/16/2022 22:30,       9.67 
07/16/2022 22:45,       9.67 
07/16/2022 23:00,       9.67 
07/16/2022 23:15,       9.67 
07/16/2022 23:30,       9.67 
07/16/2022 23:45,       9.67 
07/17/2022 00:00,       9.67 
07/17/2022 00:15,       9.67 
07/17/2022 00:30,       9.67 
07/17/2022 00:45,       9.67 
07/17/2022 01:00,       9.67 
07/17/2022 01:15,       9.67 
07/17/2022 01:30,       9.67 
07/17/2022 01:45,       9.99 
07/17/2022 02:00,       9.99 
07/17/2022 02:15,       9.67 
07/17/2022 02:30,       9.67 
07/17/2022 02:45,       9.99 
07/17/2022 03:00,       9.99 
07/17/2022 03:15,       9.99 
07/17/2022 03:30,       9.99 
07/17/2022 03:45,       9.99 
07/17/2022 04:00,       9.99 
07/17/2022 04:15,       9.99 
07/17/2022 04:30,       9.99 
07/17/2022 04:45,       9.99 
07/17/2022 05:00,       9.99 
07/17/2022 05:15,       9.99 
07/17/2022 05:30,       9.99 
07/17/2022 05:45,       9.99 
07/17/2022 06:00,       9.99 
07/17/2022 06:15,       9.99 
07/17/2022 06:30,       9.99 
07/17/2022 06:45,       9.99 
07/17/2022 07:00,       9.99 
07/17/2022 07:15,       9.99 
07/17/2022 07:30,       9.99 
07/17/2022 07:45,       9.99 
07/17/2022 08:00,       9.99 
07/17/2022 08:15,       9.99 
07/17/2022 08:30,       9.99 
07/17/2022 08:45,       9.99 
07/17/2022 09:00,       9.99 
07/17/2022 09:15,       9.99 
07/17/2022 09:30,       9.99 
07/17/2022 09:45,       9.99 
07/17/2022 10:00,       9.99 
07/17/2022 10:15,      10.30 
07/17/2022 10:30,      10.30 
07/17/2022 10:45,      10.30 
07/17/2022 11:00,       9.99 
07/17/2022 11:15,       9.99 
07/17/2022 11:30,       9.99 
07/17/2022 11:45,       9.99 
07/17/2022 12:00,       9.99 
07/17/2022 12:15,      10.30 
07/17/2022 12:30,       9.99 
07/17/2022 12:45,      10.30 
07/17/2022 13:00,       9.99 
07/17/2022 13:15,       9.99 
07/17/2022 13:30,       9.99 
07/17/2022 13:45,       9.99 
07/17/2022 14:00,       9.99 
07/17/2022 14:15,       9.99 
07/17/2022 14:30,       9.99 
07/17/2022 14:45,       9.99 
07/17/2022 15:00,       9.99 
07/17/2022 15:15,       9.67 
07/17/2022 15:30,       9.67 
07/17/2022 15:45,       9.67 
07/17/2022 16:00,       9.67 
07/17/2022 16:15,       9.67 
07/17/2022 16:30,       9.67 
07/17/2022 16:45,       9.67 
07/17/2022 17:00,       9.67 
07/17/2022 17:15,       9.67 
07/17/2022 17:30,       9.67 
07/17/2022 17:45,       9.67 
07/17/2022 18:00,       9.67 
07/17/2022 18:15,       9.67 
07/17/2022 18:30,       9.67 
07/17/2022 18:45,       9.67 
07/17/2022 19:00,       9.67 
07/17/2022 19:15,       9.67 
07/17/2022 19:30,       9.67 
07/17/2022 19:45,       9.67 
07/17/2022 20:00,       9.67 
07/17/2022 20:15,       9.67 
07/17/2022 20:30,       9.67 
07/17/2022 20:45,       9.67 
07/17/2022 21:00,       9.67 
07/17/2022 21:15,       9.67 
07/17/2022 21:30,       9.36 
07/17/2022 21:45,       9.67 
07/17/2022 22:00,       9.67 
07/17/2022 22:15,       9.67 
07/17/2022 22:30,       9.67 
07/17/2022 22:45,       9.67 
07/17/2022 23:00,       9.67 
07/17/2022 23:15,       9.67 
07/17/2022 23:30,       9.67 
07/17/2022 23:45,       9.67 
07/18/2022 00:00,       9.67 
07/18/2022 00:15,       9.67 
07/18/2022 00:30,       9.67 
07/18/2022 00:45,       9.67 
07/18/2022 01:00,       9.67 
07/18/2022 01:15,       9.67 
07/18/2022 01:30,       9.67 
07/18/2022 01:45,       9.67 
07/18/2022 02:00,       9.67 
07/18/2022 02:15,       9.67 
07/18/2022 02:30,       9.67 
07/18/2022 02:45,       9.67 
07/18/2022 03:00,       9.67 
07/18/2022 03:15,       9.67 
07/18/2022 03:30,       9.99 
07/18/2022 03:45,       9.99 
07/18/2022 04:00,       9.67 
07/18/2022 04:15,       9.67 
07/18/2022 04:30,       9.67 
07/18/2022 04:45,       9.67 
07/18/2022 05:00,       9.67 
07/18/2022 05:15,       9.99 
07/18/2022 05:30,       9.99 
07/18/2022 05:45,       9.67 
07/18/2022 06:00,       9.67 
07/18/2022 06:15,       9.99 
07/18/2022 06:30,       9.99 
07/18/2022 06:45,       9.99 
07/18/2022 07:00,       9.99 
07/18/2022 07:15,       9.99 
07/18/2022 07:30,       9.99 
07/18/2022 07:45,       9.99 
07/18/2022 08:00,       9.99 
07/18/2022 08:15,       9.99 
07/18/2022 08:30,       9.99 
07/18/2022 08:45,       9.99 
07/18/2022 09:00,       9.67 
07/18/2022 09:15,       9.99 
07/18/2022 09:30,       9.99 
07/18/2022 09:45,       9.99 
07/18/2022 10:00,       9.99 
07/18/2022 10:15,       9.99 
07/18/2022 10:30,       9.99 
07/18/2022 10:45,       9.99 
07/18/2022 11:00,       9.99 
07/18/2022 11:15,       9.99 
07/18/2022 11:30,       9.99 
07/18/2022 11:45,       9.99 
07/18/2022 12:00,       9.99 
07/18/2022 12:15,       9.99 
07/18/2022 12:30,       9.99 
07/18/2022 12:45,       9.99 
07/18/2022 13:00,       9.99 
07/18/2022 13:15,       9.67 
07/18/2022 13:30,       9.67 
07/18/2022 13:45,       9.99 
07/18/2022 14:00,       9.67 
07/18/2022 14:15,       9.67 
07/18/2022 14:30,       9.67 
07/18/2022 14:45,       9.67 
07/18/2022 15:00,       9.67 
07/18/2022 15:15,       9.67 
07/18/2022 15:30,       9.67 
07/18/2022 15:45,       9.67 
07/18/2022 16:00,       9.67 
07/18/2022 16:15,       9.67 
07/18/2022 16:30,       9.67 
07/18/2022 16:45,       9.67 
07/18/2022 17:00,       9.67 
07/18/2022 17:15,       9.67 
07/18/2022 17:30,       9.67 
07/18/2022 17:45,       9.36 
07/18/2022 18:00,       9.36 
07/18/2022 18:15,       9.36 
07/18/2022 18:30,       9.36 
07/18/2022 18:45,       9.36 
07/18/2022 19:00,       9.36 
07/18/2022 19:15,       9.36 
07/18/2022 19:30,       9.36 
07/18/2022 19:45,       9.36 
07/18/2022 20:00,       9.67 
07/18/2022 20:15,       9.36 
07/18/2022 20:30,       9.36 
07/18/2022 20:45,       9.36 
07/18/2022 21:00,       9.36 
07/18/2022 21:15,       9.36 
07/18/2022 21:30,       9.36 
07/18/2022 21:45,       9.36 
07/18/2022 22:00,       9.36 
07/18/2022 22:15,       9.36 
07/18/2022 22:30,       9.36 
07/18/2022 22:45,       9.36 
07/18/2022 23:00,       9.36 
07/18/2022 23:15,       9.36 
07/18/2022 23:30,       9.36 
07/18/2022 23:45,       9.36 
07/19/2022 00:00,       9.36 
07/19/2022 00:15,       9.36 
07/19/2022 00:30,       9.67 
07/19/2022 00:45,       9.36 
07/19/2022 01:00,       9.36 
07/19/2022 01:15,       9.67 
07/19/2022 01:30,       9.67 
07/19/2022 01:45,       9.36 
07/19/2022 02:00,       9.67 
07/19/2022 02:15,       9.67 
07/19/2022 02:30,       9.67 
07/19/2022 02:45,       9.67 
07/19/2022 03:00,       9.67 
07/19/2022 03:15,       9.67 
07/19/2022 03:30,       9.67 
07/19/2022 03:45,       9.67 
07/19/2022 04:00,       9.67 
07/19/2022 04:15,       9.67 
07/19/2022 04:30,       9.67 
07/19/2022 04:45,       9.67 
07/19/2022 05:00,       9.67 
07/19/2022 05:15,       9.67 
07/19/2022 05:30,       9.67 
07/19/2022 05:45,       9.67 
07/19/2022 06:00,       9.67 
07/19/2022 06:15,       9.67 
07/19/2022 06:30,       9.99 
07/19/2022 06:45,       9.67 
07/19/2022 07:00,       9.99 
07/19/2022 07:15,       9.67 
07/19/2022 07:30,       9.67 
07/19/2022 07:45,       9.67 
07/19/2022 08:00,       9.67 
07/19/2022 08:15,       9.67 
07/19/2022 08:30,       9.99 
07/19/2022 08:45,       9.99 
07/19/2022 09:00,       9.67 
07/19/2022 09:15,       9.99 
07/19/2022 09:30,       9.67 
07/19/2022 09:45,       9.67 
07/19/2022 10:00,       9.99 
07/19/2022 10:15,       9.67 
07/19/2022 10:30,       9.99 
07/19/2022 10:45,       9.67 
07/19/2022 11:00,       9.99 
07/19/2022 11:15,       9.99 
07/19/2022 11:30,       9.67 
07/19/2022 11:45,       9.67 
07/19/2022 12:00,       9.67 
07/19/2022 12:15,       9.99 
07/19/2022 12:30,       9.99 
07/19/2022 12:45,       9.67 
07/19/2022 13:00,       9.67 
07/19/2022 13:15,       9.67 
07/19/2022 13:30,       9.67 
07/19/2022 13:45,       9.67 
07/19/2022 14:00,       9.67 
07/19/2022 14:15,       9.67 
07/19/2022 14:30,       9.67 
07/19/2022 14:45,       9.36 
07/19/2022 15:00,       9.67 
07/19/2022 15:15,       9.36 
07/19/2022 15:30,       9.36 
07/19/2022 15:45,       9.67 
07/19/2022 16:00,       9.36 
END DATA

CodePudding user response:

You could use nth-of-type to select for second pre tag. Then call splitlines() on that tag's text, along with a little cleaning. Use csv to write out to a csv.

import requests, re, csv
from bs4 import BeautifulSoup as bs

r = requests.get('https://www.usbr.gov/pn-bin/instant.pl?parameter=CHRO q&syer=2022&smnth=7&sdy=12&eyer=2022&emnth=7&edy=19&format=2')
soup = bs(r.content, 'lxml')
results = []

for i in soup.select_one('pre:nth-of-type(2)').text.strip().splitlines():
    if 'DATA' not in i:
        results.append([ re.sub('\s{2,}', ' ', j.strip()) for j in i.split(',')])

with open("data.csv", "w", encoding="utf-8-sig", newline='') as csv_file:
    w = csv.writer(csv_file, delimiter = ",", quoting=csv.QUOTE_MINIMAL)
    w.writerow(results[0])
    for line in results[1:]:
        w.writerow(line)
  • Related