What I'm doing: I'm writing a webpage extractor to gather weather data. This is what I did so far:
import urllib.request
from bs4 import BeautifulSoup
# open the webpage and assign the content to a new variable
base = urllib.request.urlopen('http://www.weather.com/weather/today/Beijing+CHXX0008:1:CH')
f = base.readlines()
f = str(f)
soup = BeautifulSoup(f)
rn_base = soup.find_all(itemprop="temperature-fahrenheit")
If you print
the variable rn_base
, you get: [<span class="wx-value" itemprop="temperature-fahrenheit">75</span>]
, which i think is a list with only one element. The number 75
is my objective.
Question: I tried several ways to obtain the number but failed. They are, namely, 1) use str.join()
to convert rn_base
to a string, but failed because rn_base
is a ResultSet
object; 2)use index slicing, but because it's not a string subject, failed. 3)use get_text()
as specified in beautifulsoup documentation, but got AttributeError: 'ResultSet' object has no attribute 'get_text'
.
Any help is greatly appreciated!