[Python] Formatting web scraped data BS4

Discussão em 'Python' iniciado por Stack, Fevereiro 17, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using the following code to scrape some data but at the moment it outputs like this:

    output
    Fulford Road
    Water Lane
    York
    York
    YO10 4PA
    YO30 6PQ

    The desired output is this:

    line1 city postcode
    Fulford Road York YO10 4PA
    Water Lane York YO30 6PQ

    Code

    import requests
    from bs4 import BeautifulSoup
    import pandas as pd



    list1 = []



    response3 = requests.get("https://stores.aldi.co.uk/yorkshire-amp-humber/york")
    soup3 = BeautifulSoup(response3.text, "html.parser")


    try:
    for a1 in soup3.find_all('span', attrs={'class':'Address-field Address-line1'}):
    line1 = a1.get_text()
    print(line1)

    for a2 in soup3.find_all('span', attrs={'class':'Address-field Address-city'}):
    line2 = a2.get_text()
    print(line2)

    for a3 in soup3.find_all('span', attrs={'class':'Address-field Address-postalCode'}):
    line3 = a3.get_text()
    print(line3)


    except:
    pass

    data = pd.DataFrame(list1)


    I'd really appreciate any support you can give me to solve this.

    Thanks, S

    Continue reading...

Compartilhe esta Página