Returning only last item and splitting into columns

Viewed 42

I'm having a couple of issues - I seem to be only returning the last item on this list. Can someone help me here please? I also want to split the df into columns filtering all of the postcodes into one column. Not sure where to start with this. Help much appreciated. Many thanks in advance!

import requests

from bs4 import BeautifulSoup

import pandas as pd

URL = "https://www.matki.co.uk/matki-dealers/"

page = requests.get(URL)
soup = BeautifulSoup(page.content, "html.parser")

results = soup.find(class_="dealer-overview") 
company_elements = results.find_all("article")

for company_element in company_elements:

company_info = company_element.getText(separator=u', ').replace('Find out more »', '')

print (company_info)

data = {company_info}

df = pd.DataFrame(data) 
df.shape

df
1 Answers

IIUC, you need to replace the loop with:

df = pd.DataFrame({'info': [e.getText(separator=u', ')
                             .replace('Find out more »', '')
                            for e in company_elements]})

output:

                                                  info
0    ESP Bathrooms & Interiors, Queens Retail Park,...
1    Paul Scarr & Son Ltd, Supreme Centre, Haws Hil...
2    Stonebridge Interiors, 19 Main Street, Pontela...
3    Bathe Distinctive Bathrooms, 55 Pottery Road, ...
4    Draw A Bath Ltd, 68 Telegraph Road, Heswall, W...
..                                                 ...
346  Warren Keys, Unit B Carrs Lane, Tromode, Dougl...
347  Haldane Fisher, Isle of Man Business Park, Coo...
348  David Scott (Agencies) Ltd, Supreme Centre, 11...
349  Ballycastle Homecare Ltd, 2 The Diamond, Bally...
350  Beggs & Partners, Great Patrick Street, Belfas...

[351 rows x 1 columns]
Related