Attribute error with BeautifulSoup get method
<p>I'm trying to make a webscraper in Python using urllib and BeautifulSoup. My laptop works on Debian, so I don't use the latest version of urllib.</p>

<p>My goal is quite simple : extract values from a Wikipedia table <a href="https://fr.wikipedia.org/wiki/Liste_des_monuments_historiques_de_Chaumont" rel="nofollow">like this one</a>.</p>

<p>So I started my script with :</p>

<pre><code>import urllib
from bs4 import BeautifulSoup

start ="https://fr.wikipedia.org/wiki/Liste_des_monuments_historiques_de_Strasbourg"
url = urllib.urlopen(start).read()
bsObj = BeautifulSoup(url)

table = bsObj.find("table", {"class":"wikitable sortable"})
lines = table.findAll("tr")
</code></pre>

<p>Then, I used a for loop to scrap specific values from each row of the Wikipedia table :</p>

<pre><code>for line in lines:
    longitude = line.find("data", {"class":"p-longitude"})
    print(longitude)
    latitude = line.find("data", {"class":"p-latitude"})
    print(latitude)
</code></pre>

<p>This gave for example :</p>

<pre><code>&lt;data class="p-longitude" value="7.764953"&gt;7Â°Â 45â²Â 54â³Â Est&lt;/data&gt;
&lt;data class="p-latitude" value="48.588848"&gt;48Â°Â 35â²Â 20â³Â Nord&lt;/data&gt;
</code></pre>

<p>I thought that get() method would work fine, as :</p>

<pre><code>longitude = line.find("data", {"class":"p-longitude"}).get("value")
print(longitude)
</code></pre>

<p>But my terminal print this error :</p>

<pre><code>Traceback (most recent call last):
  File "scraper_monu_historiques_wikipedia.py", line 14, in &lt;module&gt;
longitude = line.find("data", {"class":"p-longitude"}).get("value")
AttributeError: 'NoneType' object has no attribute 'get'
</code></pre>

<p>I didn't understand why, because my variables latitude and longitude are BeautifulSoup Tags (I checked with a type()), so the get methode should work...</p>

<p>Thanks in advance if you have the solution !</p>

<p>In this loop:</p>

<pre><code>for line in lines:
    longitude = line.find("data", {"class":"p-longitude"})
    print(longitude)
    latitude = line.find("data", {"class":"p-latitude"})
    print(latitude)
</code></pre>

<p>For some of the lines, <code>longitude</code> and <code>latitude</code> are found, but for others the are not found, so they are set to <code>None</code>. You have to check whether it is found or not before performing any further operations, e.g.:</p>

<pre><code>for line in lines:
    longitude = line.find("data", {"class":"p-longitude"})
    latitude = line.find("data", {"class":"p-latitude"})
    if longitude and latitude:
        longitude_value = longitude.get('value')
        latitude_value = latitude.get('value')
        print(longitude_value, latitude_value)
</code></pre>

python get tags beautifulsoup