I am trying to scrape the rain chance and the temperature/wind speed for each baseball game from rotowire.com. Once I scrape the data, I will then convert it to three columns - rain, temperature, and wind. Thanks to another user, I was able to get close to getting the data but cannot quite get all the way there. I've tried two approaches.
The first approach:
from bs4 import BeautifulSoup
import requests
import pandas as pd
url = 'https://www.rotowire.com/baseball/daily-lineups.php'
r = requests.get(url)
soup = BeautifulSoup(r.text, "html.parser")
weather = []
for i in soup.select(".lineup__bottom"):
forecast = i.select_one('.lineup__weather-text').text
weather.append(forecast)
This returns:
['\n100% Rain\r\n 66°\xa0\xa0Wind 8 mph In ', '\n0% Rain\r\n 64°\xa0\xa0Wind 4 mph L-R ', '\n0% Rain\r\n 69°\xa0\xa0Wind 7 mph In ', '\nDome\r\n In Domed Stadium\r\n ', '\n0% Rain\r\n 75°\xa0\xa0Wind 10 mph Out ', '\n0% Rain\r\n 68°\xa0\xa0Wind 9 mph R-L ', '\n0% Rain\r\n 82°\xa0\xa0Wind 9 mph ', '\n0% Rain\r\n 81°\xa0\xa0Wind 5 mph R-L ', '\nDome\r\n In Domed Stadium\r\n ', '\n1% Rain\r\n 75°\xa0\xa0Wind 4 mph R-L ', '\n1% Rain\r\n 71°\xa0\xa0Wind 6 mph Out ', '\nDome\r\n In Domed Stadium\r\n ']
The second approach I've tried is:
from bs4 import BeautifulSoup
import requests
import pandas as pd
url = 'https://www.rotowire.com/baseball/daily-lineups.php'
r = requests.get(url)
soup = BeautifulSoup(r.text, "html.parser")
#weather = []
for i in soup.select(".lineup__bottom"):
forecast = i.select_one('.lineup__weather-text').text
weather.append(forecast)
#print(forecast)
rain = i.select_one('.lineup__weather-text b:contains("Rain") ~ span').text
This returns an AttributeError that 'NoneType' object has no attribute 'text'