diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/4\347\217\255/4\347\217\255_LiPing/\347\254\254\345\215\201\345\221\250/.keep" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/4\347\217\255/4\347\217\255_LiPing/\347\254\254\345\215\201\345\221\250/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/4\347\217\255/4\347\217\255_LiPing/\347\254\254\345\215\201\345\221\250/\347\254\254\344\270\200\350\212\202/.keep" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/4\347\217\255/4\347\217\255_LiPing/\347\254\254\345\215\201\345\221\250/\347\254\254\344\270\200\350\212\202/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/4\347\217\255/4\347\217\255_LiPing/\347\254\254\345\215\201\345\221\250/\347\254\254\344\270\200\350\212\202/\347\254\254\345\215\201\345\221\250 \347\254\254\344\270\200\350\212\202.png" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/4\347\217\255/4\347\217\255_LiPing/\347\254\254\345\215\201\345\221\250/\347\254\254\344\270\200\350\212\202/\347\254\254\345\215\201\345\221\250 \347\254\254\344\270\200\350\212\202.png" new file mode 100644 index 0000000000000000000000000000000000000000..0ab4fc406245ad58a658a09dd3f618a4a9d9d823 Binary files /dev/null and "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/4\347\217\255/4\347\217\255_LiPing/\347\254\254\345\215\201\345\221\250/\347\254\254\344\270\200\350\212\202/\347\254\254\345\215\201\345\221\250 \347\254\254\344\270\200\350\212\202.png" differ diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/4\347\217\255/4\347\217\255_LiPing/\347\254\254\345\215\201\345\221\250/\347\254\254\344\272\214\343\200\201\344\270\211\350\212\202/.keep" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/4\347\217\255/4\347\217\255_LiPing/\347\254\254\345\215\201\345\221\250/\347\254\254\344\272\214\343\200\201\344\270\211\350\212\202/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/4\347\217\255/4\347\217\255_LiPing/\347\254\254\345\215\201\345\221\250/\347\254\254\344\272\214\343\200\201\344\270\211\350\212\202/jd_crawler_scrapy/items.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/4\347\217\255/4\347\217\255_LiPing/\347\254\254\345\215\201\345\221\250/\347\254\254\344\272\214\343\200\201\344\270\211\350\212\202/jd_crawler_scrapy/items.py" new file mode 100644 index 0000000000000000000000000000000000000000..80a1c44dcf970a5cd3912c48e0c75e7a702a324a --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/4\347\217\255/4\347\217\255_LiPing/\347\254\254\345\215\201\345\221\250/\347\254\254\344\272\214\343\200\201\344\270\211\350\212\202/jd_crawler_scrapy/items.py" @@ -0,0 +1,17 @@ +# Define here the models for your scraped items +# +# See documentation in: +# https://docs.scrapy.org/en/latest/topics/items.html + +import scrapy + + +class JdCrawlerScrapyItem(scrapy.Item): + # define the fields for your item here like: + # name = scrapy.Field() + sku_id = scrapy.Field() + img = scrapy.Field() + price = scrapy.Field() + title = scrapy.Field() + shop = scrapy.Field() + icons = scrapy.Field() diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/4\347\217\255/4\347\217\255_LiPing/\347\254\254\345\215\201\345\221\250/\347\254\254\344\272\214\343\200\201\344\270\211\350\212\202/jd_crawler_scrapy/middlewares.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/4\347\217\255/4\347\217\255_LiPing/\347\254\254\345\215\201\345\221\250/\347\254\254\344\272\214\343\200\201\344\270\211\350\212\202/jd_crawler_scrapy/middlewares.py" new file mode 100644 index 0000000000000000000000000000000000000000..dcdf643b88f6e8d1d3ccae14425a36daa3e0504a --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/4\347\217\255/4\347\217\255_LiPing/\347\254\254\345\215\201\345\221\250/\347\254\254\344\272\214\343\200\201\344\270\211\350\212\202/jd_crawler_scrapy/middlewares.py" @@ -0,0 +1,112 @@ +# Define here the models for your spider middleware +# +# See documentation in: +# https://docs.scrapy.org/en/latest/topics/spider-middleware.html + +from scrapy import signals + +# useful for handling different item types with a single interface +from itemadapter import is_item, ItemAdapter + + +class JdCrawlerScrapySpiderMiddleware: + # 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, 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 Request 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 JdCrawlerScrapyDownloaderMiddleware: + # 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) + + +class UAMiddleware: + + def process_request(self, request, spider): + + request.headers["user-agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36" + request.meta["proxy"] = [f'https:144.255.48.62', f'https:144.255.48.62'] + diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/4\347\217\255/4\347\217\255_LiPing/\347\254\254\345\215\201\345\221\250/\347\254\254\344\272\214\343\200\201\344\270\211\350\212\202/jd_crawler_scrapy/pipelines.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/4\347\217\255/4\347\217\255_LiPing/\347\254\254\345\215\201\345\221\250/\347\254\254\344\272\214\343\200\201\344\270\211\350\212\202/jd_crawler_scrapy/pipelines.py" new file mode 100644 index 0000000000000000000000000000000000000000..542713a1ddf192da4e843b864c03bfdb625a02ab --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/4\347\217\255/4\347\217\255_LiPing/\347\254\254\345\215\201\345\221\250/\347\254\254\344\272\214\343\200\201\344\270\211\350\212\202/jd_crawler_scrapy/pipelines.py" @@ -0,0 +1,28 @@ +# Define your item pipelines here +# +# Don't forget to add your pipeline to the ITEM_PIPELINES setting +# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html + + +# useful for handling different item types with a single interface +from itemadapter import ItemAdapter +from jd_crawler_scrapy.items import JdCrawlerScrapyItem +import pymysql + +class JdCrawlerScrapyPipeline: + def __init__(self): + self.mysql_con = None + + def process_item(self, item, spider): + if not self.mysql_con: + self.mysql_con = pymysql.connect(**spider.settings["MYSQL_CONF"]) + if isinstance(item, JdCrawlerScrapyItem): + cursor = self.mysql_con.cursor() + SQL = """INSERT INTO jd_search(sku_id, img, price, title, shop, icons) + VALUES ('{}','{}','{}','{}','{}','{}')""".format( + item['sku_id'], item['img'], item['price'], item['title'], item['shop'], item['icons'] + ) + cursor.execute(SQL) + self.mysql_con.commit() + cursor.close() + return item diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/4\347\217\255/4\347\217\255_LiPing/\347\254\254\345\215\201\345\221\250/\347\254\254\344\272\214\343\200\201\344\270\211\350\212\202/jd_crawler_scrapy/settings.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/4\347\217\255/4\347\217\255_LiPing/\347\254\254\345\215\201\345\221\250/\347\254\254\344\272\214\343\200\201\344\270\211\350\212\202/jd_crawler_scrapy/settings.py" new file mode 100644 index 0000000000000000000000000000000000000000..c9294d36c115d69f39b04e60451792f40e3ec3c4 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/4\347\217\255/4\347\217\255_LiPing/\347\254\254\345\215\201\345\221\250/\347\254\254\344\272\214\343\200\201\344\270\211\350\212\202/jd_crawler_scrapy/settings.py" @@ -0,0 +1,103 @@ +# Scrapy settings for jd_crawler_scrapy project +# +# For simplicity, this file contains only settings considered important or +# commonly used. You can find more settings consulting the documentation: +# +# https://docs.scrapy.org/en/latest/topics/settings.html +# https://docs.scrapy.org/en/latest/topics/downloader-middleware.html +# https://docs.scrapy.org/en/latest/topics/spider-middleware.html + +BOT_NAME = 'jd_crawler_scrapy' + +SPIDER_MODULES = ['jd_crawler_scrapy.spiders'] +NEWSPIDER_MODULE = 'jd_crawler_scrapy.spiders' + + +# Crawl responsibly by identifying yourself (and your website) on the user-agent +#USER_AGENT = 'jd_crawler_scrapy (+http://www.yourdomain.com)' + +# Obey robots.txt rules +ROBOTSTXT_OBEY = False + +# 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://docs.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://docs.scrapy.org/en/latest/topics/spider-middleware.html +#SPIDER_MIDDLEWARES = { +# 'jd_crawler_scrapy.middlewares.JdCrawlerScrapySpiderMiddleware': 543, +#} + +# Enable or disable downloader middlewares +# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html +DOWNLOADER_MIDDLEWARES = { + # 'jd_crawler_scrapy.middlewares.JdCrawlerScrapyDownloaderMiddleware': 543, + 'jd_crawler_scrapy.middlewares.UAMiddleware': 100, +} + +# Enable or disable extensions +# See https://docs.scrapy.org/en/latest/topics/extensions.html +#EXTENSIONS = { +# 'scrapy.extensions.telnet.TelnetConsole': None, +#} + +# Configure item pipelines +# See https://docs.scrapy.org/en/latest/topics/item-pipeline.html +ITEM_PIPELINES = { + 'jd_crawler_scrapy.pipelines.JdCrawlerScrapyPipeline': 300, +} + +# Enable and configure the AutoThrottle extension (disabled by default) +# See https://docs.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://docs.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' + + +# MYSQL CONF +MYSQL_CONF = { + "host": "127.0.0.1", + "user": "root", + "password": "0000", + "db": "tunan_class_4" +} + + +# LOG +LOG_FILE = "F:/log/jd_search.log" +LOG_LEVEL = "DEBUG" diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/4\347\217\255/4\347\217\255_LiPing/\347\254\254\345\215\201\345\221\250/\347\254\254\344\272\214\343\200\201\344\270\211\350\212\202/jd_crawler_scrapy/spiders/jd_search.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/4\347\217\255/4\347\217\255_LiPing/\347\254\254\345\215\201\345\221\250/\347\254\254\344\272\214\343\200\201\344\270\211\350\212\202/jd_crawler_scrapy/spiders/jd_search.py" new file mode 100644 index 0000000000000000000000000000000000000000..7b150cb8355ac05062a1dba1a6e7ad609a031443 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/4\347\217\255/4\347\217\255_LiPing/\347\254\254\345\215\201\345\221\250/\347\254\254\344\272\214\343\200\201\344\270\211\350\212\202/jd_crawler_scrapy/spiders/jd_search.py" @@ -0,0 +1,56 @@ +import scrapy +from bs4 import BeautifulSoup +import json +from jd_crawler_scrapy.items import JdCrawlerScrapyItem + + +class JdSearch(scrapy.Spider): + name = "jd_search" + + def start_requests(self): + for keyword in ["手机", "电脑", "相机", "书籍"]: + for page_number in range(1, 11): + url = f"https://search.jd.com/Search?keyword={keyword}&page={page_number}" + + # 选用FormRequest是可以发送GET和POST请求 + yield scrapy.FormRequest( + url=url, + method='GET', + # formdata=data #如果是post请求,携带数据使用formdata参数 + callback=self.parse_search # 指定回调函数处理response对象 + ) + break + + def parse_search(self, response): + print(response) + soup = BeautifulSoup(response.text, "lxml") + item_array = soup.select("ul[class='gl-warp clearfix'] li[class='gl-item']") + for item in item_array: + try: + sku_id = item.attrs["data-sku"] + img = item.select("img[data-img='1']") + price = item.select("div[class='p-price']") + # title = item.select("div[class='p-name']") + title = item.select("div[class='p-name p-name-type-2']") + shop = item.select("div[class='p-shop']") + icons = item.select("div[class='p-icons']") + + img = img[0].attrs['data-lazy-img'] if img else "" + price = price[0].strong.i.text if price else "" + title = title[0].text.strip() if title else "" + shop = shop[0].a.attrs['title'] if shop else "" + icons = json.dumps([tag_ele.text for tag_ele in icons[0].select("i")]) if icons else '[]' + + item = JdCrawlerScrapyItem() + item["sku_id"] = sku_id + item["img"] = img + item["price"] = price + item["title"] = title + item["shop"] = shop + item["icons"] = icons + yield item + + + except Exception as e: + print(e.args) + diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/4\347\217\255/4\347\217\255_LiPing/\347\254\254\345\215\201\345\221\250/\347\254\254\344\272\214\343\200\201\344\270\211\350\212\202/jd_crawler_scrapy/spiders/run.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/4\347\217\255/4\347\217\255_LiPing/\347\254\254\345\215\201\345\221\250/\347\254\254\344\272\214\343\200\201\344\270\211\350\212\202/jd_crawler_scrapy/spiders/run.py" new file mode 100644 index 0000000000000000000000000000000000000000..d65b88a0b2aeb9dfeeb123844ab46fd82295043f --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/4\347\217\255/4\347\217\255_LiPing/\347\254\254\345\215\201\345\221\250/\347\254\254\344\272\214\343\200\201\344\270\211\350\212\202/jd_crawler_scrapy/spiders/run.py" @@ -0,0 +1,4 @@ +from scrapy import cmdline + +command = "scrapy crawl jd_search".split() +cmdline.execute(command) \ No newline at end of file