Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7,281 changes: 7,281 additions & 0 deletions Jason/NLP/NLP_LDA_test01.ipynb

Large diffs are not rendered by default.

2,472 changes: 2,472 additions & 0 deletions Jason/NLP/NLP_word2vec_doc2vec_final.ipynb

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
18 changes: 18 additions & 0 deletions Jason/Scrapy_BloomingDale/blmdl/items.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html

from scrapy import Item, Field


class BlmdlItem(Item):
brand = Field()
prod_name = Field()
price_reg = Field()
price_dis = Field()
# desc_list = Field()
# prod_image = Field()
# full_file_name = Field()
103 changes: 103 additions & 0 deletions Jason/Scrapy_BloomingDale/blmdl/middlewares.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# -*- coding: utf-8 -*-

# Define here the models for your spider middleware
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/spider-middleware.html

from scrapy import signals


class BlmdlSpiderMiddleware(object):
# Not all methods need to be defined. If a method is not defined,
# scrapy acts as if the spider middleware does not modify the
# passed objects.

@classmethod
def from_crawler(cls, crawler):
# This method is used by Scrapy to create your spiders.
s = cls()
crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
return s

def process_spider_input(self, response, spider):
# Called for each response that goes through the spider
# middleware and into the spider.

# Should return None or raise an exception.
return None

def process_spider_output(self, response, result, spider):
# Called with the results returned from the Spider, after
# it has processed the response.

# Must return an iterable of Request, dict or Item objects.
for i in result:
yield i

def process_spider_exception(self, response, exception, spider):
# Called when a spider or process_spider_input() method
# (from other spider middleware) raises an exception.

# Should return either None or an iterable of Response, dict
# or Item objects.
pass

def process_start_requests(self, start_requests, spider):
# Called with the start requests of the spider, and works
# similarly to the process_spider_output() method, except
# that it doesn’t have a response associated.

# Must return only requests (not items).
for r in start_requests:
yield r

def spider_opened(self, spider):
spider.logger.info('Spider opened: %s' % spider.name)


class BlmdlDownloaderMiddleware(object):
# Not all methods need to be defined. If a method is not defined,
# scrapy acts as if the downloader middleware does not modify the
# passed objects.

@classmethod
def from_crawler(cls, crawler):
# This method is used by Scrapy to create your spiders.
s = cls()
crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
return s

def process_request(self, request, spider):
# Called for each request that goes through the downloader
# middleware.

# Must either:
# - return None: continue processing this request
# - or return a Response object
# - or return a Request object
# - or raise IgnoreRequest: process_exception() methods of
# installed downloader middleware will be called
return None

def process_response(self, request, response, spider):
# Called with the response returned from the downloader.

# Must either;
# - return a Response object
# - return a Request object
# - or raise IgnoreRequest
return response

def process_exception(self, request, exception, spider):
# Called when a download handler or a process_request()
# (from other downloader middleware) raises an exception.

# Must either:
# - return None: continue processing this exception
# - return a Response object: stops process_exception() chain
# - return a Request object: stops process_exception() chain
pass

def spider_opened(self, spider):
spider.logger.info('Spider opened: %s' % spider.name)
27 changes: 27 additions & 0 deletions Jason/Scrapy_BloomingDale/blmdl/pipelines.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html


from scrapy.exporters import CsvItemExporter

class WriteItemPipeline(object):

def __init__(self):
self.filename = 'bloomingdale_0916.csv'

def open_spider(self, spider):
self.csvfile = open(self.filename, 'wb')
self.exporter = CsvItemExporter(self.csvfile, delimiter='\t')
self.exporter.start_exporting()

def close_spider(self, spider):
self.exporter.finish_exporting()
self.csvfile.close()

def process_item(self, item, spider):
self.exporter.export_item(item)
return item
90 changes: 90 additions & 0 deletions Jason/Scrapy_BloomingDale/blmdl/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# -*- coding: utf-8 -*-

# Scrapy settings for blmdl project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
# https://doc.scrapy.org/en/latest/topics/settings.html
# https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
# https://doc.scrapy.org/en/latest/topics/spider-middleware.html

BOT_NAME = 'blmdl'

SPIDER_MODULES = ['blmdl.spiders']
NEWSPIDER_MODULE = 'blmdl.spiders'


# Crawl responsibly by identifying yourself (and your website) on the user-agent
USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'

# Obey robots.txt rules
ROBOTSTXT_OBEY = True

# Configure maximum concurrent requests performed by Scrapy (default: 16)
#CONCURRENT_REQUESTS = 32

# Configure a delay for requests for the same website (default: 0)
# See https://doc.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
DOWNLOAD_DELAY = 3
# The download delay setting will honor only one of:
#CONCURRENT_REQUESTS_PER_DOMAIN = 16
#CONCURRENT_REQUESTS_PER_IP = 16

# Disable cookies (enabled by default)
#COOKIES_ENABLED = False

# Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED = False

# Override the default request headers:
#DEFAULT_REQUEST_HEADERS = {
# 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
# 'Accept-Language': 'en',
#}

# Enable or disable spider middlewares
# See https://doc.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
# 'blmdl.middlewares.BlmdlSpiderMiddleware': 543,
#}

# Enable or disable downloader middlewares
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
# 'blmdl.middlewares.BlmdlDownloaderMiddleware': 543,
#}

# Enable or disable extensions
# See https://doc.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = {
# 'scrapy.extensions.telnet.TelnetConsole': None,
#}

# Configure item pipelines
# See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
'blmdl.pipelines.WriteItemPipeline': 200,
}

# Enable and configure the AutoThrottle extension (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/autothrottle.html
#AUTOTHROTTLE_ENABLED = True
# The initial download delay
#AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
#AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
#AUTOTHROTTLE_DEBUG = False

# Enable and configure HTTP caching (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
#HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'
4 changes: 4 additions & 0 deletions Jason/Scrapy_BloomingDale/blmdl/spiders/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This package will contain the spiders of your Scrapy project
#
# Please refer to the documentation for information on how to create and manage
# your spiders.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
91 changes: 91 additions & 0 deletions Jason/Scrapy_BloomingDale/blmdl/spiders/blmdl_spider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from scrapy import Spider, Request
from blmdl.items import BlmdlItem
import re
import numpy as np
import urllib.request
import urllib.error
import os.path

class BlmdlSpider(Spider):
name = 'blmdl_spider'
allowed_urls = ['https://www.bloomingdales.com/']
start_urls = ['https://www.bloomingdales.com/shop/womens-apparel/tops-tees/Pageindex,Productsperpage/1,96?id=5619']

def parse(self, response):


pagelist_urls = ["https://www.bloomingdales.com/shop/womens-apparel/tops-tees/Pageindex,Productsperpage/{},96?id=5619".format(i) for i in range(1, 6)]

# Loop all the different product pages
for url in pagelist_urls:
print('#'*50)
print(url)
print('#'*50)

# Look into a particular page
yield Request(url = url, callback = self.parse_product_page)

def parse_product_page(self, response):

# Collect all the products into a list
products = response.xpath('//li[@class="small-6 medium-4 large-3 columns"]')

# Loop into the list to see the information of each product
for product in products:
# Get the link to the detailed page for a single item.
url_product = product.xpath('.//a[@class="productDescLink"]/@href').extract_first()
price_reg = product.xpath('.//div[@class="productDetail"]//span[@class="regular"]/text()').extract_first().strip()

# If there is a discount price then use it. If not just use the regular price.
if product.xpath('.//div[@class="productDetail"]//span[@class="discount"]/text()').extract_first() != None:
price_dis = product.xpath('.//div[@class="productDetail"]//span[@class="discount"]/text()').extract_first().split()[1]
else:
price_dis = price_reg

# Carry the price information into the next level.
# Get into the detailed product page.
yield Request(url = "https://www.bloomingdales.com"+url_product, meta={'price_reg':price_reg, 'price_dis':price_dis}, callback = self.parse_detail_page)

def parse_detail_page(self, response):

# Grab different information of a product
brand = response.xpath('//h1/a[@id="brandNameLink"]/text()').extract_first()
prod_name = response.xpath('//div[@id="productName"]/text()').extract_first()
desc_list = response.xpath('//ul[@class="product-details-bullet-list"]/li/text()').extract()
prod_image = response.xpath('//img[@itemprop="image"]/@src').extract_first()
prod_img_full = prod_image+'?wid=1200&qlt=90,0&layer=comp&op_sharpen=0&resMode=sharp2&op_usm=0.7,1.0,0.5,0&fmt=jpeg'

# Received the price information from the upper level.
price_reg = response.meta['price_reg']
price_dis = response.meta['price_dis']
print('#'*50)
print(prod_image)
print('#'*50)

# This is for extracting product image################################
if prod_image != '':
try:
full_file_name = 'img' + re.split("\/", prod_image)[-1].replace('.','_')
if ( not os.path.exists(full_file_name)):
urllib.request.urlretrieve(prod_img_full,full_file_name) #download img
except urllib.error.HTTPError as err:
if err.code == 404:
pass
else:
raise
######################################################################



item = BlmdlItem()
item['brand'] = brand
item['prod_name'] = prod_name
item['price_reg'] = price_reg
item['price_dis'] = price_dis
item['desc_list'] = desc_list
item['prod_image'] = prod_image
item['full_file_name'] = full_file_name

yield item