I've written some code using python. My goal was to harvest name, phone, and web address from extracting all the links available in that webpage. The scraper is so far following the instructions nicely and harvesting data as it is supposed to.
The logic I applied here is crystal and easy to maintain. If any flaw is found in it's basic design, It is my incapability I could not shake that off. Hope I'll have any suggestion to improve the design. Thanks a lot for going it through.
The complete code:
import requests
from lxml import html
class HouzzParser:
main_url = "https://www.houzz.com/professionals/"
def __init__(self):
self.keeper = []
self.session = requests.Session()
def Scrape(self):
self.receiver(self.main_url)
def receiver(self, link):
# Collecting side-bar links.
response = self.session.get(link)
tree = html.fromstring(response.text)
for item in tree.xpath("//a[@class='sidebar-item-label']"):
itm_lnks = item.xpath(".//@href")
for page in itm_lnks:
self.profile_links(page) # Sending links to create new hrefs for getting to each profile of first page
self.pagination_links(page) # Sending links to process hrefs to the next page
def pagination_links(self, link):
# Creating links for the next page
response = self.session.get(link)
tree = html.fromstring(response.text)
for n_url in tree.xpath("//ul[@class='pagination']"):
items = n_url.xpath(".//a[@class='pageNumber']/@href")
for item in items:
self.profile_links(item)
def profile_links(self, links):
# Creating links for the profile page
response = self.session.get(links)
tree = html.fromstring(response.text)
for titles in tree.xpath("//div[@class='name-info']"):
links = titles.xpath(".//a[@class='pro-title']/@href")
for doc in links:
self.target_page(doc)
def target_page(self, urls):
# Extracting necessary docs from here
response = self.session.get(urls)
tree = html.fromstring(response.text)
for titles in tree.xpath("//div[@class='profile-cover']"):
name = titles.findtext(".//a[@class='profile-full-name']")
phone = titles.xpath(".//a[contains(concat(' ', @class, ' '), ' click-to-call-link ')]/@phone")[0] if titles.xpath(".//a[contains(concat(' ', @class, ' '), ' click-to-call-link ')]/@phone") else ""
web = titles.xpath(".//a[@class='proWebsiteLink']/@href")[0] if titles.xpath(".//a[@class='proWebsiteLink']/@href") else ""
print(name, phone, web)
data = name, phone, web
if data not in self.keeper:
self.keeper.append(data)
def __str__(self):
return self.keeper
if __name__ == '__main__':
scraper = HouzzParser()
scraper.Scrape()
for item in scraper.keeper:
print(item)