I have been trying to web scrapping using Beautiful soup

But I get this error

Traceback (most recent call last):
  File "/home/osint/Desktop/DATAPRAC/oldcode.py", line 16, in <module>
    NAME    = listings[index].div.div.find('div', class_='info').find('div', class_='info-section info-primary').h2.a.text #use class with an underscore (class_) since it's a key word in python
              ~~~~~~~~^^^^^^^
  File "/home/osint/anaconda3/lib/python3.11/site-packages/bs4/element.py", line 1573, in __getitem__
    return self.attrs[key]
           ~~~~~~~~~~^^^^^
KeyError: 0
import pandas as pd
import requests
from bs4 import BeautifulSoup

info = pd.DataFrame()


html_text = requests.get(f'https://www.yellowpages.com/search?search_terms=Plumbers&geo_location_terms=St+Tammany%2C+LA').text
soup = BeautifulSoup(html_text, 'lxml')

listings = soup.find('div', class_ = 'search-results center-ads').find('div', class_='result')

#LOOPING THROUGH ALL LISTINGS
for index, value in enumerate(listings):
  
    NAME    = listings[index].div.div.find('div', class_='info').find('div', class_='info-section info-primary').h2.a.text #use class with an underscore (class_) since it's a key word in python
    #business_Description = soup.find('div', class_ = 'search-results center-ads').find('div', class_='result').div.div.find('div', class_='info').find('div', class_='info-section info-primary').find('div', class_='categories').a.text
    CATEGORY= listings[index].div.div.find('div', class_='info').find('div', class_='info-section info-primary').find('div', class_='categories').text
    #RATING = listings[index].div.div.find('div', class_='info').find('div', class_='info-section info-primary').find('div', class_='categories').a.span
    PHONE   = listings[index].div.div.find('div', class_='info').find('div', class_='info-section info-secondary').find('div', class_='phone').text
    ADDRESS = listings[index].div.div.find('div', class_='info').find('div', class_='info-section info-secondary').p.text
    LINK    = listings[index].div.div.find('div', class_='info').find('div', class_='info-section info-primary').h2.a.get('href')
    print(f'Name: {NAME}nCategory: {CATEGORY}nPhone {PHONE}nAddress: {ADDRESS},nLink: www.yellowpages.com{LINK}')

New contributor

MOSIMOS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Khám phá các thẻ bài đăng