What are your thoughts and comments relating to areas I could improve on?
import csv
import re
import requests
from bs4 import BeautifulSoup
class Resto:
def __init__(self,url,filename="default"):
self.page = 1
url = url + "&page={0}"
self.url = url
self.filename = filename
def info(self):
linkz = []
while True:
content = requests.get(self.url.format(self.page)).content
soup = BeautifulSoup(content)
links = [i["href"] for i in soup.find_all("a",{"itemprop":"name"})]
if not links:
break
linkz.extend(links)
self.page += 1
return linkz
def email(self):
names = []
emails = []
sites = []
streets = []
cities = []
numbers = []
for i in self.info():
i = "http://www.yellowpages.com" + i
content2 = requests.get(i).content
soup2 = BeautifulSoup(content2)
name = [i.text for i in soup2.find_all("h1",{"itemprop":"name"})]
email = [re.sub(r'mailto:', "",i["href"]) for i in soup2.find_all("a",{"class":"email-business"})]
if email:
email = [email[0]]
site = [i["href"] for i in soup2.find_all("a",{"class":"custom-link"}) if i.text == "Visit Website"]
street = [i.text for i in soup2.find_all("p",{"class":"street-address"})]
city = [i.text for i in soup2.find_all("p",{"class":"city-state"})]
phone = [i.text for i in soup2.find_all("p",{"class":"phone"})]
if not email:
email = ["---"]
if not site:
site = ["---"]
if not street:
street = ["---"]
if not city:
city = ["---"]
if not phone:
phone = ["---"]
names.extend(name)
emails.extend(email)
sites.extend(site)
streets.extend(street)
cities.extend(city)
numbers.extend(phone)
return names,streets,cities,numbers,sites,emails
def csv(self):
"""saves csv file in root of python 3 directory"""
items = self.email()
items = list(zip(*items))
with open("{0}.csv".format(self.filename), "w")as shops:
writer = csv.writer(shops)
writer.writerows(items)
a = Resto("http://www.yellowpages.com/search?search_terms=restaurants&geo_location_terms=tx")
a.csv()