Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

returning only the first Value while i am printing a list

Good day,
Could someone assist me with why when I execute the following code I get only one value returnedenter image description here

[![enter image description here][2]][2]

import requests
from bs4 import BeautifulSoup
import json

my_list = []
def extract(URL):
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) 
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'}
    r = requests.get(url, headers=headers)
    soup = BeautifulSoup(requests.get(url).content, "html.parser")
    return soup.select("[data-tooltip-phones]")

def transform(data):
    for item in data:
        phone_url = "https://yellowpages.com.eg" + item["data-tooltip-phones"]
        title = item.find_previous(class_="item-title").text
        address = item.find_previous(class_="address-text").text.strip().replace('\n', 
'')
        phones = requests.get(phone_url).json()
    business = {
        'name': title,
        'address': address,
        'telephone': phones
    }
    my_list.append(business)
x = 1
data = extract(f'https://yellowpages.com.eg/en/category/charcoal/p{x}')
transform(data)
print(my_list)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Indent the business and append line so it is inside the for loop:

for item in data:
        phone_url = "https://yellowpages.com.eg" + item["data-tooltip-phones"]
        title = item.find_previous(class_="item-title").text
        address = item.find_previous(class_="address-text").text.strip().replace('\n', 
'')
        phones = requests.get(phone_url).json()
        business = {
            'name': title,
            'address': address,
            'telephone': phones
        }
        my_list.append(business)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading