Please help, I am so loosing nerves now.I am having this problems since I started learning Python. Always come to a same issue and no one online can give any valid answer
My code:
from bs4 import BeautifulSoup
import requests
page = requests.get(
'https://forecast.weather.gov/MapClick.php?lat=34.05349000000007&lon=-118.24531999999999#.XswiwMCxWUk')
soup = BeautifulSoup(page.content, 'html.parser')
week = soup.find(id='seven-day-forecast-body')
items = week.find_all(class_='forecast-tombstone')
print(items[0].find(class_='period-name').get_text())
print(items[0].find(class_='short-desc').get_text())
print(items[0].find(class_='temp temp-high').get_text())
period_names = [item.find(class_='period-name').get_text() for item in items]
short_descp = [item.find(class_='short-desc').get_text() for item in items]
temp = [item.find(class_='temp temp-high').get_text() for item in items]
print(period_names)
print(short_descp)
print(temp)
output :
[Running] python -u "c:\Users\dukasu\Documents\Python\test.py"
ThisAfternoon
Partly Sunny
High: 76 �F
Traceback (most recent call last):
File "c:\Users\dukasu\Documents\Python\test.py", line 20, in <module>
temp = [item.find(class_='temp temp-high').get_text() for item in items]
File "c:\Users\dukasu\Documents\Python\test.py", line 20, in <listcomp>
temp = [item.find(class_='temp temp-high').get_text() for item in items]
AttributeError: 'NoneType' object has no attribute 'get_text'
[Done] exited with code=1 in 0.69 seconds
Issue is due to a utf-8 encoding (my PC is at cp1252), but how to solve it terminally (I think problem is cos it cant operate with degree symbol). There is a simple code in Python 2, but how to solve it in Python 3.xx. How to set encoding at start of the code and forget about this issue. anp please pardon my english, it is not my native language.