<jupyter_start><jupyter_text><jupyter_code>import numpy as np
import pandas as pd
import requests
from bs4 import BeautifulSoup
url='https://www.yellowpages.ca/search/si/1/Grocery+Stores/Toronto+ON'
headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36'}
response=requests.get(url,headers=headers)
soup = BeautifulSoup(response.content,'html.parser')
all_list = soup.find_all('div',class_='listing__content__wrapper')
len(all_list)
name = all_list[0].find('h3').text.strip()
address = all_list[0].find('div',class_='listing__address address mainLocal').text.strip()
country_wise=all_list[0].find('article',class_='listing__details__teaser').text.strip()
phone_no=all_list[0].find_all('li',class_='mlr__item mlr__item--more mlr__item--phone jsMapBubblePhone')[0].text.strip()
name=[]
address=[]
phone_no=[]


for i in range(1,34):
  try:
    name.append(all_list[i].find('h3').text.strip())
  except:
    name.append(np.nan)
  try:
    address.append(all_list[i].find('span',class_='listing__address--full').text.strip())
  except:
    address.append(np.nan)
  try:
    phone_no.append(all_list[i].find_all('li',class_='mlr__item mlr__item--more mlr__item--phone jsMapBubblePhone')[0].text.strip())
  except:
    phone_no.append(np.nan)
d={'Name':name,'Address':address,'Phone_no':phone_no}
df=pd.DataFrame(d)
df
new_df =pd.DataFrame()
for j in range(1,65):
  url='https://www.yellowpages.ca/search/si/{}/Grocery+Stores/Toronto+ON'.format(j)
  headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36'}
  response=requests.get(url,headers=headers)
  soup = BeautifulSoup(response.content,'html.parser')
  all_list = soup.find_all('div',class_='listing__content__wrapper')
  name=[]
  address=[]
  phone_no=[]


  for i in range(1,len(all_list)):
    try:
      name.append(all_list[i].find('h3').text.strip())
    except:
      name.append(np.nan)
    try:
      address.append(all_list[i].find('span',class_='listing__address--full').text.strip())
    except:
      address.append(np.nan)
    try:
      phone_no.append(all_list[i].find_all('li',class_='mlr__item mlr__item--more mlr__item--phone jsMapBubblePhone')[0].text.strip())
    except:
      phone_no.append(np.nan)
  d={'Name':name,'Address':address,'Phone_no':phone_no}
  df=pd.DataFrame(d)
  new_df = new_df.append(df, ignore_index=True)
new_df
# new_df.to_csv('African_Grocery Stores')
# pd.read_csv('African_Grocery Stores.csv')
new_df.head(5)
new_df['Name'] = new_df['Name'].str.split(' ').str.slice(0,4).str.join(' ')
new_df
new_df['Address'] = new_df['Address'].str.split(',').str.get(0)
new_df['Address'] = new_df['Address'].str.split(' ').str.slice(1,3).str.join(' ')
new_df['Phone_no'] = new_df['Phone_no'].str.split(' ').str.get(1)
new_df['Phone_no'] = new_df['Phone_no'].astype(str)
new_df['Phone_no'] = new_df['Phone_no'].str.replace('Number\n\n','')
new_df
# new_df.drop(columns=['Address','Country_wise'],index=1,inplace=True)
new_df
new_df.isnull().sum()
new_df.dropna(inplace=True)
new_df.isnull().sum()
new_df.info()
new_df['Phone_no'] = new_df['Phone_no'].str.replace('-\n',' ')
new_df = new_df[new_df['Phone_no']!='4163647397\n\n18006640430']
new_df
new_df = new_df[new_df['Phone_no']!='4164980922\n\n18669876453']
new_df['Phone_no'].astype(str)
new_df['Phone_no'] = new_df['Phone_no'].str.replace('\n\n','')
new_df['Phone_no'] = new_df['Phone_no'].astype(str)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19926408151712427, 'fraction_numerical': 0.035380696292103025, 'mean_word_length': 6.42436974789916, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '387199', 'license': 'Apache-2.0', 'n_tokens_mistral': 1487, 'n_tokens_neox': 1403, 'n_words': 159, 'path': 'African_Grocery_Stores.ipynb', 'repo_name': 'Awaistolu/web_scraping', 'size': '3533'}