Hi am I am trying to extract text from a find all function in beautiful soup 4 but I don't know how to do this, here is my current code that is not working:
from bs4 import BeautifulSoup
page = requests.get("https://weather.com/en-IE/weather/tenday/l/e98742cdb581b2f4461e4f438badbfb0e16dc9e70ffbf4c8df1b0f7a4394f9f9")
soup = BeautifulSoup(page.content,"lxml")
info = soup.find_all("span", class_ = "DetailsSummary--extendedData--365A_")
day = soup.find_all("h2", class_ = "DetailsSummary--daypartName--2FBp2")
print(info.get_text())
print()
print()
print(info)
I hope people can help me as soon as they can because this is most likely a very simple question that I don't know the answer to.
CodePudding user response:
There's just one issue here, info will be a list that you need to iterate over. Using a for you can do
from bs4 import BeautifulSoup
import requests
page = requests.get("https://weather.com/en-IE/weather/tenday/l/e98742cdb581b2f4461e4f438badbfb0e16dc9e70ffbf4c8df1b0f7a4394f9f9")
soup = BeautifulSoup(page.content,"lxml")
info = soup.find_all("span", class_ = "DetailsSummary--extendedData--365A_")
day = soup.find_all("h2", class_ = "DetailsSummary--daypartName--2FBp2")
for item in info:
print(item.get_text())
print()
print()
print(info)
CodePudding user response:
You need to iterate over the find_all result --> docs
import requests
from bs4 import BeautifulSoup
url = "https://weather.com/en-IE/weather/tenday/l/e98742cdb581b2f4461e4f438badbfb0e16dc9e70ffbf4c8df1b0f7a4394f9f9"
page = requests.get(url)
soup = BeautifulSoup(page.content, "lxml")
info = soup.find_all("span", class_="DetailsSummary--extendedData--365A_")
day = soup.find_all("h2", class_="DetailsSummary--daypartName--2FBp2")
# Find_all if found returns an Iterator
# which you need to loop
info_text = [i.get_text() for i in info]
day_text = [i.get_text() for i in day]
print(info_text)
print(day_text)
Better Print the result
from pprint import pprint
pprint(info_text, indent=4)
pprint(day_text, indent=4)
CodePudding user response:
Here is the minimal working solution:
Code:
import requests
from bs4 import BeautifulSoup
page = requests.get("https://weather.com/en-IE/weather/tenday/l/e98742cdb581b2f4461e4f438badbfb0e16dc9e70ffbf4c8df1b0f7a4394f9f9")
soup = BeautifulSoup(page.content,"lxml")
cards= soup.find_all('summary',class_="Disclosure--Summary--UuybP DaypartDetails--Summary--3IBUr Disclosure--hideBorderOnSummaryOpen--ZdSDc")
for card in cards:
day = card.find("h2", class_ = "DetailsSummary--daypartName--2FBp2").get_text()
tem = card.find('div',attrs={"data-testid":"detailsTemperature"}).get_text()
print('day:' day, 'Temperature:' tem,end='\n\n')
Output:
Oday:Fri 12 Temperature:14°/9°
day:Sat 13 Temperature:13°/10°
day:Sun 14 Temperature:14°/10°
day:Mon 15 Temperature:12°/8°
day:Tue 16 Temperature:12°/8°
day:Wed 17 Temperature:11°/8°
day:Thu 18 Temperature:12°/9°
day:Fri 19 Temperature:13°/9°
day:Sat 20 Temperature:12°/7°
day:Sun 21 Temperature:10°/6°
day:Mon 22 Temperature:10°/6°
day:Tue 23 Temperature:9°/5°
day:Wed 24 Temperature:9°/7°
day:Thu 25 Temperature:10°/6°