rhat398

Untitled

Apr 2nd, 2022
1,218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 32.53 KB | None | 0 0
  1. # packages
  2. import scrapy
  3. from scrapy.crawler import CrawlerProcess
  4. import csv
  5. import json
  6. from datetime import datetime
  7.  
  8.  
  9. def parse_date(dt):
  10.     dateobj = datetime.strptime(dt, "%Y-%m-%dT%H:%M:%S%z")
  11.     return dateobj.strftime("%d-%m-%Y")
  12.  
  13.  
  14. # realtor scraper class
  15. class RealtorScraper(scrapy.Spider):
  16.  
  17.     name = "realtor-scraper"
  18.  
  19.     custom_settings = {
  20.         "FEED_FORMAT": "csv",
  21.         "FEED_URI": "detail_realtor_property.csv",
  22.         "LOG_FILE": "realtor.log",
  23.         # "CONCURRENT_REQUESTS": 2,
  24.     }
  25.  
  26.     base_url = "https://www.realtor.com/api/v1/hulk?client_id=rdc-x&schema=vesta"
  27.  
  28.     headers = {
  29.         "authority": "www.realtor.com",
  30.         "accept": "application/json",
  31.         "accept-language": "en-US,en;q=0.9",
  32.         "cache-control": "max-age=0",
  33.         "content-type": "application/json",
  34.         "origin": "https://www.realtor.com",
  35.         "sec-fetch-dest": "empty",
  36.         "sec-fetch-mode": "cors",
  37.         "sec-fetch-site": "same-origin",
  38.         "sec-gpc": "1",
  39.         "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Safari/537.36",
  40.     }
  41.  
  42.     json_data = {
  43.         "query": "",
  44.         "variables": {},
  45.         "callfrom": "PDP",
  46.         "isClient": True,
  47.     }
  48.  
  49.     def start_requests(self):
  50.         prop_id = []
  51.         with open("items_realtor_property_3.csv", "r") as file:
  52.             data = csv.DictReader(file)
  53.             for col in data:
  54.                 prop_id.append(col["id"])
  55.  
  56.         for id in prop_id:
  57.             print(id)
  58.             self.json_data[
  59.                 "query"
  60.             ] = f'{{\n    home(property_id: "{id}") {{\n      advertisers {{\n        team_name\n        address {{\n          city\n          country\n          line\n          postal_code\n          state\n          state_code\n        }}\n        builder {{\n          fulfillment_id\n        }}\n        broker {{\n          accent_color\n          designations\n          fulfillment_id\n          name\n          logo\n        }}\n        email\n        fulfillment_id\n        href\n        mls_set\n        name\n        nrds_id\n        office {{\n          address {{\n            city\n            coordinate {{\n              lat\n              lon\n            }}\n            country\n            line\n            postal_code\n            state\n            state_code\n          }}\n          application_url\n          email\n          lead_email {{\n            to\n            cc\n          }}\n          fulfillment_id\n          hours\n          href\n          mls_set\n          out_of_community\n          name\n          phones {{\n            ext\n            number\n            primary\n            trackable\n            type\n          }}\n          photo {{\n            href\n          }}\n          slogan\n        }}\n        phones {{\n          ext\n          number\n          primary\n          trackable\n          type\n        }}\n        photo {{\n          href\n        }}\n        slogan\n        type\n      }}\n      buyers {{\n        address {{\n          city\n          country\n          line\n          postal_code\n          state\n          state_code\n        }}\n        broker {{\n          accent_color\n          designations\n          fulfillment_id\n          name\n          logo\n        }}\n        email\n        fulfillment_id\n        href\n        mls_set\n        name\n        nrds_id\n        office {{\n          address {{\n            city\n            coordinate {{\n              lat\n              lon\n            }}\n            country\n            line\n            postal_code\n            state\n            state_code\n          }}\n          application_url\n          email\n          lead_email {{\n            to\n            cc\n          }}\n          fulfillment_id\n          hours\n          href\n          mls_set\n          out_of_community\n          name\n          phones {{\n            ext\n            number\n            primary\n            trackable\n            type\n          }}\n          photo {{\n            href\n          }}\n          slogan\n        }}\n        phones {{\n          ext\n          number\n          primary\n          trackable\n          type\n        }}\n        photo {{\n          href\n        }}\n        slogan\n        type\n      }}\n      community {{\n        permalink\n      }}\n      estimates {{\n        current_values(source: "corelogic")\n        @include_if(field: "status", operator: in, value: "sold,off_market,other") {{\n          estimate\n          estimate_high\n          estimate_low\n          date\n          source {{\n            type\n            name\n          }}\n        }}\n      }}\n      days_on_market\n      description {{\n        baths\n        baths_3qtr\n        baths_full\n        baths_full_calc\n        baths_half\n        baths_max\n        baths_min\n        baths_partial_calc\n        baths_total\n        beds\n        beds_max\n        beds_min\n        construction\n        cooling\n        exterior\n        fireplace\n        garage\n        garage_max\n        garage_min\n        garage_type\n        heating\n        logo {{\n          href\n        }}\n        lot_sqft\n        name\n        pool\n        roofing\n        rooms\n        sqft\n        sqft_max\n        sqft_min\n        stories\n        styles\n        sub_type\n        text\n        type\n        units\n        year_built\n        year_renovated\n        zoning\n      }}\n      details {{\n        category\n        parent_category\n        text\n      }}\n      flags {{\n        is_coming_soon\n        is_contingent\n        is_deal_available\n        is_for_rent\n        is_foreclosure\n        is_garage_present\n        is_new_construction\n        is_pending\n        is_price_excludes_land\n        is_senior_community\n        is_short_sale\n        is_subdivision\n      }}\n      href\n      last_sold_date\n      last_sold_price\n      list_date\n      list_price\n      listing_id\n      local {{\n        flood {{\n          firststreet_url\n          fsid\n          flood_factor_score\n          flood_factor_severity\n          environmental_risk\n          trend_direction\n          fema_zone\n          insurance_quotes{{\n            provider_url\n            provider_name\n            provider_logo\n            expires\n            price\n            home_coverage\n            contents_coverage\n            disclaimer\n          }}\n        }}\n        noise {{\n          score\n        }}\n      }}\n      location {{\n        address {{\n          city\n          coordinate {{\n            lat\n            lon\n          }}\n          country\n          line\n          postal_code\n          state\n          state_code\n          street_direction\n          street_name\n          street_number\n          street_post_direction\n          street_suffix\n          unit\n          validation_code\n        }}\n        county {{\n          fips_code\n          name\n          state_code\n        }}\n        neighborhoods {{\n          city\n          id\n          level\n          name\n          state_code\n          slug_id\n        }}\n        search_areas {{\n          city\n          state_code\n        }}\n      }}\n      nearby_schools {{\n        schools {{\n          coordinate {{\n            lat\n            lon\n          }}\n          distance_in_miles\n          district {{\n            id\n            name\n          }}\n          education_levels\n          funding_type\n          grades\n          greatschools_id\n          id\n          name\n          nces_code\n          parent_rating\n          rating\n          review_count\n          slug_id\n          student_count\n        }}\n      }}\n      photo_count\n      photos {{\n        title\n        description\n        href\n        type\n      }}\n      primary_photo {{\n        href\n      }}\n      property_history {{\n        date\n        event_name\n        price\n        price_sqft\n        source_listing_id\n        source_name\n        listing @include_if(field: "status", operator: in, value: "sold,off_market,other") {{\n          list_price\n          last_status_change_date\n          last_update_date\n          status\n          list_date\n          listing_id\n          suppression_flags\n          photos {{\n            href\n          }}\n          description {{\n            text\n          }}\n          advertisers {{\n            fulfillment_id\n            nrds_id\n            name\n            email\n            href\n            slogan\n            office {{\n              fulfillment_id\n              name\n              email\n              href\n              slogan\n              out_of_community\n              application_url\n              mls_set\n            }}\n            broker {{\n              fulfillment_id\n              name\n              accent_color\n              logo\n            }}\n            type\n            mls_set\n          }}\n          buyers {{\n            fulfillment_id\n            nrds_id\n            name\n            email\n            href\n            slogan\n            type\n            mls_set\n            address {{\n              line\n              city\n              postal_code\n              state_code\n              state\n              country\n              coordinate {{\n                lat\n                lon\n              }}\n            }}\n            office {{\n              fulfillment_id\n              name\n              email\n              href\n              slogan\n              hours\n              out_of_community\n              application_url\n              mls_set\n              address {{\n                line\n                city\n                postal_code\n                state_code\n                state\n                country\n              }}\n              phones {{\n                number\n                type\n                primary\n                trackable\n                ext\n              }}\n              county {{\n                name\n              }}\n            }}\n            phones {{\n              number\n              type\n              primary\n              trackable\n              ext\n            }}\n            broker {{\n              fulfillment_id\n              name\n              accent_color\n              logo\n            }}\n          }}\n          source {{\n            id\n            agents {{\n              agent_id\n              agent_name\n              office_id\n              office_name\n              office_phone\n              type\n            }}\n          }}\n        }}\n      }}\n      property_id\n      provider_url {{\n        href\n        level\n        type\n      }}\n      source {{\n        agents {{\n          agent_id\n          agent_name\n          id\n          office_id\n          office_name\n          office_phone\n          type\n        }}\n        disclaimer {{\n          href\n          logo {{\n            href\n            height\n            width\n          }}\n          text\n        }}\n        id\n        plan_id\n        listing_id\n        name\n        raw {{\n          status\n          style\n          tax_amount\n        }}\n        type\n        community_id\n      }}\n      status\n      suppression_flags\n      tags\n      tax_history {{\n        assessment {{\n          building\n          land\n          total\n        }}\n        market {{\n          building\n          land\n          total\n        }}\n        tax\n        year\n      }}\n    }}\n  }}'
  61.  
  62.             yield scrapy.Request(
  63.                 url=self.base_url,
  64.                 method="POST",
  65.                 dont_filter=True,
  66.                 headers=self.headers,
  67.                 body=json.dumps(self.json_data),
  68.                 callback=self.parse_details,
  69.             )
  70.  
  71.     def parse_details(self, response):
  72.         item = {}
  73.         prop_detail = response.json()
  74.  
  75.         item["property_id"] = prop_detail["data"]["home"]["property_id"]
  76.         item["property_link"] = prop_detail["data"]["home"]["href"]
  77.         item["city"] = prop_detail["data"]["home"]["location"]["address"]["city"]
  78.         item["lat"] = ""
  79.         item["lon"] = ""
  80.         item["address"] = prop_detail["data"]["home"]["location"]["address"]["line"]
  81.         item["postcode"] = prop_detail["data"]["home"]["location"]["address"][
  82.             "postal_code"
  83.         ]
  84.         item["state"] = prop_detail["data"]["home"]["location"]["address"]["state"]
  85.         item["state_code"] = prop_detail["data"]["home"]["location"]["address"][
  86.             "state_code"
  87.         ]
  88.         item["street_name"] = prop_detail["data"]["home"]["location"]["address"][
  89.             "street_name"
  90.         ]
  91.         item["street_num"] = prop_detail["data"]["home"]["location"]["address"][
  92.             "street_number"
  93.         ]
  94.         item["street_suffix"] = prop_detail["data"]["home"]["location"]["address"][
  95.             "street_suffix"
  96.         ]
  97.         item["listing_status"] = prop_detail["data"]["home"]["status"]
  98.         item["homestyle"] = ""
  99.         item["price"] = prop_detail["data"]["home"]["list_price"]
  100.         item["listing_date"] = ""
  101.         item["last_sold_price"] = prop_detail["data"]["home"]["last_sold_price"]
  102.         item["flood_factor_score"] = ""
  103.         item["flood_factor_severity"] = ""
  104.         item["listing_raw_status"] = ""
  105.         try:
  106.             item["last_sold_date"] = prop_detail["data"]["home"]["last_sold_date"]
  107.  
  108.         except:
  109.             item["last_sold_date"] = ""
  110.         item["environmental_risk"] = ""
  111.         item["fema_zone"] = ""
  112.         item["noise_score"] = ""
  113.         item["baths"] = prop_detail["data"]["home"]["description"]["baths"]
  114.         item["baths_3qtr"] = prop_detail["data"]["home"]["description"]["baths_3qtr"]
  115.         item["baths_full"] = prop_detail["data"]["home"]["description"]["baths_full"]
  116.         item["baths_full_calc"] = prop_detail["data"]["home"]["description"][
  117.             "baths_full_calc"
  118.         ]
  119.         item["baths_half"] = prop_detail["data"]["home"]["description"]["baths_half"]
  120.         item["baths_max"] = prop_detail["data"]["home"]["description"]["baths_max"]
  121.         item["baths_min"] = prop_detail["data"]["home"]["description"]["baths_min"]
  122.         item["baths_partial_calc"] = prop_detail["data"]["home"]["description"][
  123.             "baths_partial_calc"
  124.         ]
  125.         item["baths_total"] = prop_detail["data"]["home"]["description"]["baths_total"]
  126.         item["beds"] = prop_detail["data"]["home"]["description"]["beds"]
  127.         item["beds_max"] = prop_detail["data"]["home"]["description"]["beds_max"]
  128.         item["beds_min"] = prop_detail["data"]["home"]["description"]["beds_min"]
  129.         item["construction"] = prop_detail["data"]["home"]["description"][
  130.             "construction"
  131.         ]
  132.         item["cooling"] = prop_detail["data"]["home"]["description"]["cooling"]
  133.         item["exterior"] = prop_detail["data"]["home"]["description"]["exterior"]
  134.         item["fireplace"] = prop_detail["data"]["home"]["description"]["fireplace"]
  135.         item["garage"] = prop_detail["data"]["home"]["description"]["garage"]
  136.         item["garage_max"] = prop_detail["data"]["home"]["description"]["garage_max"]
  137.         item["garage_min"] = prop_detail["data"]["home"]["description"]["garage_min"]
  138.         item["garage_type"] = prop_detail["data"]["home"]["description"]["garage_type"]
  139.         item["heating"] = prop_detail["data"]["home"]["description"]["heating"]
  140.         item["lot_sqft"] = prop_detail["data"]["home"]["description"]["lot_sqft"]
  141.         item["pool"] = prop_detail["data"]["home"]["description"]["pool"]
  142.         item["rooms"] = prop_detail["data"]["home"]["description"]["rooms"]
  143.         item["sqft"] = prop_detail["data"]["home"]["description"]["sqft"]
  144.         item["sqft_max"] = prop_detail["data"]["home"]["description"]["sqft_max"]
  145.         item["sqft_min"] = prop_detail["data"]["home"]["description"]["sqft_min"]
  146.         item["stories"] = prop_detail["data"]["home"]["description"]["stories"]
  147.         item["type"] = prop_detail["data"]["home"]["description"]["type"]
  148.         item["year_built"] = prop_detail["data"]["home"]["description"]["year_built"]
  149.         item["year_renovated"] = prop_detail["data"]["home"]["description"][
  150.             "year_renovated"
  151.         ]
  152.         item["community_features"] = ""
  153.         item["unit_features"] = ""
  154.         item["bedrooms"] = ""
  155.         item["total_rooms"] = ""
  156.         item["basement_description"] = ""
  157.         item["appliances"] = ""
  158.         item["heating_feature"] = ""
  159.         item["cooling_feature"] = ""
  160.         item["bathrooms"] = ""
  161.         item["interior"] = ""
  162.         item["exterior_lot_features"] = ""
  163.         item["lot_size_acres"] = ""
  164.         item["lot_size_square_feet"] = ""
  165.         item["parking_feature"] = ""
  166.         item["asscociation"] = ""
  167.         item["asscociation_fee"] = ""
  168.         item["asscociation_frequency"] = ""
  169.         item["asscociation_includes"] = ""
  170.         item["calculated_total_monthly_association_fees"] = ""
  171.         item["school_info"] = ""
  172.         item["source_listing_status"] = ""
  173.         item["county"] = ""
  174.         item["cross_street"] = ""
  175.         item["source_property_type"] = ""
  176.         item["property_subtype"] = ""
  177.         item["parcel_number"] = ""
  178.         item["total_sqft_living"] = ""
  179.         item["construction_material"] = ""
  180.         item["foundation_details"] = ""
  181.         item["levels"] = ""
  182.         item["property_age"] = ""
  183.         item["roof_type"] = ""
  184.         item["sewer"] = ""
  185.         item["water_source"] = ""
  186.         item["tags"] = prop_detail["data"]["home"]["tags"]
  187.         details = prop_detail["data"]["home"]["details"]
  188.  
  189.         try:
  190.             item["broker_email"] = prop_detail["data"]["home"]["advertisers"][0][
  191.                 "email"
  192.             ]
  193.             item["broker_name"] = prop_detail["data"]["home"]["advertisers"][0]["name"]
  194.             item["broker_city"] = prop_detail["data"]["home"]["advertisers"][0][
  195.                 "office"
  196.             ]["address"]["city"]
  197.  
  198.             item["broker_country"] = prop_detail["data"]["home"]["advertisers"][0][
  199.                 "office"
  200.             ]["address"]["country"]
  201.             item["broker_line"] = prop_detail["data"]["home"]["advertisers"][0][
  202.                 "office"
  203.             ]["address"]["line"]
  204.             item["broker_state_code"] = prop_detail["data"]["home"]["advertisers"][0][
  205.                 "office"
  206.             ]["address"]["state_code"]
  207.             item["broker_office_name"] = prop_detail["data"]["home"]["advertisers"][0][
  208.                 "office"
  209.             ]["name"]
  210.             item["broker_phone_1"] = prop_detail["data"]["home"]["advertisers"][0][
  211.                 "office"
  212.             ]["phones"][0]["number"]
  213.             item["broker_phone_2"] = prop_detail["data"]["home"]["advertisers"][0][
  214.                 "phones"
  215.             ][0]["number"]
  216.             item["broker_phone_type"] = prop_detail["data"]["home"]["advertisers"][0][
  217.                 "office"
  218.             ]["phones"][0]["type"]
  219.         except:
  220.             item["broker_email"] = ""
  221.             item["broker_name"] = ""
  222.             item["broker_city"] = ""
  223.             item["broker_country"] = ""
  224.             item["broker_line"] = ""
  225.             item["broker_state_code"] = ""
  226.             item["broker_office_name"] = ""
  227.             item["broker_phone_1"] = ""
  228.             item["broker_phone_2"] = ""
  229.             item["broker_phone_type"] = ""
  230.         try:
  231.             for detail in details:
  232.                 if detail["category"] == "Appliances":
  233.                     item["appliances"] = detail["text"]
  234.  
  235.                 if detail["category"] == "Heating and Cooling":
  236.                     for txt in detail["text"]:
  237.                         if "Cooling Features: " in txt:
  238.                             item["cooling_feature"] = txt.replace(
  239.                                 "Cooling Features: ", ""
  240.                             )
  241.                         if "Heating Features: " in txt:
  242.                             item["heating_feature"] = txt.replace(
  243.                                 "Heating Features: ", ""
  244.                             )
  245.  
  246.                 if detail["category"] == "Unit Features":
  247.                     item["unit_features"] = prop_detail["data"]["home"]["details"][1][
  248.                         "text"
  249.                     ]
  250.  
  251.                 if detail["category"] == "Other Rooms":
  252.                     for txt in detail["text"]:
  253.                         if "Total Rooms: " in txt:
  254.                             item["total_rooms"] = txt.replace("Total Rooms: ", "")
  255.                         if "Basement Description: " in txt:
  256.                             item["basement_description"] = txt.replace(
  257.                                 "Basement Description: ", ""
  258.                             )
  259.  
  260.                 if detail["category"] == "Bathrooms":
  261.                     for idx, txt in enumerate(detail["text"]):
  262.                         detail["text"] = txt.replace("Full Bathrooms: ", "")
  263.                     item["bathrooms"] = detail["text"][0]
  264.  
  265.                 if detail["category"] == "Interior Features":
  266.                     for idx, txt in enumerate(detail["text"]):
  267.                         detail["text"] = txt.replace("Window Features: ", "")
  268.                     item["interior"] = detail["text"]
  269.  
  270.                 if detail["category"] == "Exterior and Lot Features":
  271.                     item["exterior_lot_features"] = detail["text"]
  272.  
  273.                 if detail["category"] == "Land Info":
  274.                     for txt in detail["text"]:
  275.                         if "Lot Size Acres: " in txt:
  276.                             item["lot_size_acres"] = txt.replace("Lot Size Acres: ", "")
  277.                         if "Lot Size Square Feet: " in txt:
  278.                             item["lot_size_square_feet"] = txt.replace(
  279.                                 "Lot Size Square Feet: ", ""
  280.                             )
  281.  
  282.                 if detail["category"] == "Garage and Parking":
  283.                     for idx, txt in enumerate(detail["text"]):
  284.                         detail["text"] = txt.replace("Parking Features: ", "")
  285.                     item["parking_feature"] = detail["text"]
  286.  
  287.                 if detail["category"] == "Homeowners Association":
  288.                     for idx, txt in enumerate(detail["text"]):
  289.                         detail["text"][idx] = (
  290.                             txt.replace("Association: ", "")
  291.                             .replace("Association Fee: ", "")
  292.                             .replace("Association Fee Frequency: ", "")
  293.                             .replace("Association Fee Includes: ", "")
  294.                             .replace("Calculated Total Monthly Association Fees: ", "")
  295.                         )
  296.                     try:
  297.                         item["asscociation"] = detail["text"][0]
  298.                         item["asscociation_fee"] = detail["text"][1]
  299.                         item["asscociation_frequency"] = detail["text"][2]
  300.                         item["asscociation_includes"] = detail["text"][3]
  301.                         item["calculated_total_monthly_association_fees"] = detail[
  302.                             "text"
  303.                         ][4]
  304.                     except:
  305.                         item["asscociation_fee"] = ""
  306.                         item["asscociation_frequency"] = ""
  307.                         item["asscociation_includes"] = ""
  308.  
  309.                 if detail["category"] == "School Information":
  310.                     for txt in detail["text"]:
  311.                         if "High School District: " in txt:
  312.                             item["school_info"] = txt.replace(
  313.                                 "High School District: ", ""
  314.                             )
  315.  
  316.                 if detail["category"] == "Other Property Info":
  317.                     for txt in detail["text"]:
  318.                         if "Source Listing Status: " in txt:
  319.                             item["source_listing_status"] = txt.replace(
  320.                                 "Source Listing Status: ", ""
  321.                             )
  322.                         if "County: " in txt:
  323.                             item["county"] = txt.replace("County: ", "")
  324.                         if "Cross Street: " in txt:
  325.                             item["cross_street"] = txt.replace("Cross Street: ", "")
  326.                         if "Source Property Type: " in txt:
  327.                             item["source_property_type"] = txt.replace(
  328.                                 "Source Property Type: ", ""
  329.                             )
  330.                         if "Property Subtype: " in txt:
  331.                             item["property_subtype"] = txt.replace(
  332.                                 "Property Subtype: ", ""
  333.                             )
  334.                         if "Parcel Number: " in txt:
  335.                             item["parcel_number"] = txt.replace("Parcel Number: ", "")
  336.  
  337.                 if detail["category"] == "Building and Construction":
  338.                     for txt in detail["text"]:
  339.                         if "Total Square Feet Living: " in txt:
  340.                             item["total_sqft_living"] = txt.replace(
  341.                                 "Total Square Feet Living: ", ""
  342.                             )
  343.                         if "Construction Materials: " in txt:
  344.                             item["construction_material"] = txt.replace(
  345.                                 "Construction Materials: ", ""
  346.                             )
  347.                         if "Foundation Details: " in txt:
  348.                             item["foundation_details"] = txt.replace(
  349.                                 "Foundation Details: ", ""
  350.                             )
  351.                         if "Levels: " in txt:
  352.                             item["levels"] = txt.replace("Levels: ", "")
  353.                         if "Property Age: " in txt:
  354.                             item["property_age"] = txt.replace("Property Age: ", "")
  355.                         if "Roof: " in txt:
  356.                             item["roof_type"] = txt.replace("Roof: ", "")
  357.                 if detail["category"] == "Utilities":
  358.                     for txt in detail["text"]:
  359.                         if "Sewer: " in txt:
  360.                             item["sewer"] = txt.replace("Sewer: ", "")
  361.                         if "Water Source: " in txt:
  362.                             item["water_source"] = txt.replace("Water Source: ", "")
  363.                 if detail["category"] == "Bedrooms":
  364.                     for txt in detail["text"]:
  365.                         if "Bedrooms: " in txt:
  366.                             item["bedrooms"] = txt.replace("Bedrooms: ", "")
  367.                 if detail["category"] == "Community Features":
  368.                     for txt in detail["text"]:
  369.                         item["community_features"] = txt
  370.  
  371.                 if detail["category"] == "Unit Features":
  372.                     for txt in detail["text"]:
  373.                         item["unit_features"] = txt
  374.         except:
  375.             pass
  376.  
  377.         try:
  378.             item["listing_raw_status"] = prop_detail["data"]["home"]["source"]["raw"][
  379.                 "status"
  380.             ]
  381.             item["homestyle"] = prop_detail["data"]["home"]["source"]["raw"]["style"]
  382.             item["listing_date"] = parse_date(prop_detail["data"]["home"]["list_date"])
  383.  
  384.             item["flood_factor_score"] = prop_detail["data"]["home"]["local"]["flood"][
  385.                 "flood_factor_score"
  386.             ]
  387.             item["flood_factor_severity"] = prop_detail["data"]["home"]["local"][
  388.                 "flood"
  389.             ]["flood_factor_severity"]
  390.             item["lat"] = prop_detail["data"]["home"]["location"]["address"][
  391.                 "coordinate"
  392.             ]["lat"]
  393.             item["lon"] = prop_detail["data"]["home"]["location"]["address"][
  394.                 "coordinate"
  395.             ]["lon"]
  396.             item["environmental_risk"] = prop_detail["data"]["home"]["local"]["flood"][
  397.                 "environmental_risk"
  398.             ]
  399.             item["fema_zone"] = prop_detail["data"]["home"]["local"]["flood"][
  400.                 "fema_zone"
  401.             ][0]
  402.             item["noise_score"] = prop_detail["data"]["home"]["local"]["noise"]["score"]
  403.         except:
  404.             pass
  405.         try:
  406.             prop_history = prop_detail["data"]["home"]["property_history"]
  407.             prop_tax = prop_detail["data"]["home"]["tax_history"]
  408.             if prop_history != None:
  409.                 history_date = [p["date"] for p in prop_history]
  410.  
  411.                 for idx, dt in enumerate(history_date):
  412.                     item[f"property_history_date_{idx}"] = dt
  413.  
  414.                 history_event = [e["event_name"] for e in prop_history]
  415.  
  416.                 for idx, evnt in enumerate(history_event):
  417.                     item[f"property_history_event_{idx}"] = evnt
  418.  
  419.                 history_price = [pri["price"] for pri in prop_history]
  420.  
  421.                 for idx, price in enumerate(history_price):
  422.                     item[f"property_history_price_{idx}"] = price
  423.  
  424.                 history_price_sqft = [
  425.                     pri_sqft["price_sqft"] for pri_sqft in prop_history
  426.                 ]
  427.  
  428.                 for idx, price_sqft in enumerate(history_price_sqft):
  429.                     item[f"property_history_price_sqft_{idx}"] = price_sqft
  430.  
  431.                 history_source_listing_id = [
  432.                     sld["source_listing_id"] for sld in prop_history
  433.                 ]
  434.  
  435.                 for idx, source_listing_id in enumerate(history_source_listing_id):
  436.                     item[
  437.                         f"property_history_source_listing_id_{idx}"
  438.                     ] = source_listing_id
  439.  
  440.                 history_source_name = [sn["source_name"] for sn in prop_history]
  441.  
  442.                 for idx, source_name in enumerate(history_source_name):
  443.                     item[f"property_history_source_name_{idx}"] = source_name
  444.  
  445.                 history_listing = [l["listing"] for l in prop_history]
  446.  
  447.                 for idx, listing in enumerate(history_listing):
  448.                     item[f"property_history_listing_{idx}"] = listing
  449.             else:
  450.                 print("history none", response.body)
  451.  
  452.             if prop_tax != None:
  453.                 for tax in prop_tax:
  454.                     if tax:
  455.                         tax_building_assessment = [
  456.                             tab["assessment"]["building"] for tab in prop_tax
  457.                         ]
  458.  
  459.                         for idx, tab in enumerate(tax_building_assessment):
  460.                             item[
  461.                                 f"property_history_tax_building_assessment_{idx}"
  462.                             ] = tab
  463.  
  464.                         tax_landing_assessment = [
  465.                             tal["assessment"]["land"] for tal in prop_tax
  466.                         ]
  467.  
  468.                         for idx, tla in enumerate(tax_landing_assessment):
  469.                             item[f"property_history_tax_landing_assessment_{idx}"] = tla
  470.  
  471.                         tax_total_assessment = [
  472.                             tat["assessment"]["total"] for tat in prop_tax
  473.                         ]
  474.  
  475.                         for idx, tta in enumerate(tax_total_assessment):
  476.                             item[f"property_history_tax_total_assessment_{idx}"] = tta
  477.  
  478.                         tax_building_market = [
  479.                             tmb["market"]["building"] for tmb in prop_tax
  480.                         ]
  481.  
  482.                         for idx, tbm in enumerate(tax_building_market):
  483.                             item[f"property_history_tax_building_market_{idx}"] = tbm
  484.  
  485.                         tax_land_market = [tml["market"]["land"] for tml in prop_tax]
  486.  
  487.                         for idx, tlm in enumerate(tax_land_market):
  488.                             item[f"property_history_tax_land_market_{idx}"] = tlm
  489.  
  490.                         tax_total_market = [tmt["market"]["total"] for tmt in prop_tax]
  491.  
  492.                         for idx, ttm in enumerate(tax_total_market):
  493.                             item[f"property_history_tax_total_market_{idx}"] = ttm
  494.  
  495.                         tax = [tx["tax"] for tx in prop_tax]
  496.  
  497.                         for idx, tx in enumerate(tax):
  498.                             item[f"property_history_tax_{idx}"] = tx
  499.  
  500.                         tax_year = [txy["year"] for txy in prop_tax]
  501.  
  502.                         for idx, txy in enumerate(tax_year):
  503.                             item[f"property_history_tax_year_{idx}"] = txy
  504.  
  505.         except:
  506.             pass
  507.         yield item
  508.  
  509.  
  510. # main driver
  511. if __name__ == "__main__":
  512.     # run scrapper
  513.     process = CrawlerProcess()
  514.     process.crawl(RealtorScraper)
  515.     process.start()
  516.  
Advertisement
Add Comment
Please, Sign In to add comment