Reputation: 51
I need to extract from this website link name of the city where shops are located. I created this code:
def get_page_data(number):
print('number:', number)
url = 'https://www.biedronka.pl/pl/sklepy/lista,lat,52.25,lng,21,page,'.format(number)
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
container = soup.find(class_='s-content shop-list-page')
items = container.find_all(class_='shopListElement')
dane = []
for item in items:
miasto = item.find(class_='h4').get_text(strip=True)
adres = item.find(class_='shopFullAddress').get_text(strip=True)
dane.append([adres])
return dane
wszystkie_dane = []
for number in range(1, 2):
dane_na_stronie = get_page_data(number)
wszystkie_dane.extend(dane_na_stronie)
dane = pd.DataFrame(wszystkie_dane, columns=['miasto','adres'])
dane.to_csv('biedronki_lista.csv', index=False)
The problem appears in:
miasto = item.find(class_='h4').get_text(strip=True)
AttributeError: 'NoneType' object has no attribute 'get_text'
Any ideas how to extract name of the city (in h4) from this website?
Upvotes: 0
Views: 62
Reputation: 5202
Try using:
miasto = item.find('h4').text.split()[0]
Or:
miasto = item.find('h4').get_text(strip=True)
Note:
"h4" is a tag, not a class.
Explanation:
<h4 style="margin-bottom: 10px;">
Rzeszów <span class="shopFullAddress">ul.<span class="shopAddress"> </span></span>
.text
, it returns:'Rzeszów \tul.'
['Rzeszów', 'ul.']
So do this where-ever you face error in this code.
dane = []
for item in items:
miasto = item.find('h4').get_text(strip=True)
adres = item.find('shopFullAddress').get_text(strip=True)
dane.append([adres])
Upvotes: 2
Reputation: 1710
class_='h4'
is an attribute you are passing a tag name to the class which is not correct instead :
miasto = item.find('h4').get_text(strip=True)
Upvotes: 3