I've created a crawler which is scraping name, phone number and web address of each profile from houzz website. Hope I did it the right way. Here is what I've written:
import requests
from lxml import html
url="https://www.houzz.com/professionals/"
def scraper_func(mainurl):
response = requests.get(mainurl).text
tree = html.fromstring(response)
for titles in tree.xpath("//a[@class='sidebar-item-label']"):
link = titles.xpath(".//@href")
for item in link:
paging_stuff(item)
# Done crawling links to the category from left-sided bar
def paging_stuff(process_links):
response = requests.get(process_links).text
tree = html.fromstring(response)
for titles in tree.xpath("//ul[@class='pagination']"):
link = titles.xpath(".//a[@class='pageNumber']/@href")
for item in link:
processing_stuff(item)
# Going to each page to crawl the whole links spread through pagination connected to the profile page
def processing_stuff(procured_links):
response = requests.get(procured_links).text
tree = html.fromstring(response)
for titles in tree.xpath("//div[@class='name-info']"):
links = titles.xpath(".//a[@class='pro-title']/@href")[0]
main_stuff(links)
# Going to the profile page of each link
def main_stuff(main_links):
response = requests.get(main_links).text
tree = html.fromstring(response)
def if_exist(titles,xpath):
info=titles.xpath(xpath)
if info:
return info[0]
return ""
for titles in tree.xpath("//div[@class='profile-cover']"):
name = if_exist(titles,".//a[@class='profile-full-name']/text()")
phone = if_exist(titles,".//a[contains(concat(' ', @class, ' '), ' click-to-call-link ')]/@phone")
web = if_exist(titles,".//a[@class='proWebsiteLink']/@href")
print(name,phone,web)
scraper_func(url)