Home > Enterprise >  find_all() returns only first item of the list with for item in articles
find_all() returns only first item of the list with for item in articles

Time:03-30

this is my code

from tkinter.ttk import Separator
import requests
from bs4 import BeautifulSoup

url='https://www.yellowpages.ca/search/si/2/hvac services/Ontario ON'

headers = {'user-agent' : 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36' , 'Accept-Language': 'en-US, en;q=0.5'}

r = requests.get(url, headers = headers)

soup = BeautifulSoup(r.content, 'html.parser')

articles = soup.find_all('div', class_ ='listing__content__wrapper')

for item in articles:

    company_name = item.find('a', class_ ="listing__name--link listing__link jsListingName").get_text(strip=True, separator='\n')


print(company_name)

#print(len(articles))

Questions: 1.print(len(articles)) = 34 list counts OK

2.print(company_name) = only one company_name showed not all of company_name list

How can I do for all of company_name list?

CodePudding user response:

It is just a little adjustment - Check the indet of your print command it should run in your for loop not outside:

for item in articles:
    company_name = item.find('a', class_ ="listing__name--link listing__link jsListingName").get_text(strip=True, separator='\n')
    print(company_name)

Example

import requests
from bs4 import BeautifulSoup

url='https://www.yellowpages.ca/search/si/2/hvac services/Ontario ON'

headers = {'user-agent' : 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36' , 'Accept-Language': 'en-US, en;q=0.5'}

r = requests.get(url, headers = headers)

soup = BeautifulSoup(r.content, 'html.parser')

articles = soup.find_all('div', class_ ='listing__content__wrapper')

for item in articles:
    company_name = item.find('a', class_ ="listing__name--link listing__link jsListingName").get_text(strip=True, separator='\n')
    print(company_name)

Output

Face Heating & Cooling
RK Heating & Cooling
HVAC Service
Bryant Heating & Cooling Service Experts
Lambton Climate Care
County Lines HVAC & Fireplaces
Brant Custom Heat & Air
Special Gas Services
Gerry Kuchma Mechanical Inc
Aaa Technical Services
Affordable Comfort Heating and Cooling
Gaynor Mechanical Services Ltd
Custom Mechanical Ltd
Marleau HVAC Services Ltd
Associateair Mechanical Systems Ltd
True North Home Comfort
Comfort Zone Heating & Cooling
Grossi Plumbing & Heating
Zed-Air Heating & Air Conditioning
City Experts
Absolute Comfort Heating & Cooling Inc
Prouse Mechanical Ltd
Precision Heating and Cooling
Simcoe Home Comfort
Postma Heating And Cooling Inc
Affordable Comfort Heating & Cooling
HVAC Service
Lucky Air Climate Control Ltd
Mr Furnace Heating and Air Conditioning
Quality Heating and Cooling
Avis Heating & Air Conditioning
Polar Mechanical
Furnace King Home Services
Richmond Heating & Air Conditioning
Bayview Sheet Metal Heating And Air Conditioning Inc
  • Related