From c4aacc8c12991517560028f8f4e2152d22f54a37 Mon Sep 17 00:00:00 2001 From: doepy <8428487+doepy@user.noreply.gitee.com> Date: Sun, 7 Mar 2021 12:02:58 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E6=96=B0=E5=BB=BA=203=E7=8F=AD---=E9=99=B6?= =?UTF-8?q?=E5=86=B6---=E7=AC=AC=E5=8D=81=E5=91=A8=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /.keep" diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /.keep" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /.keep" new file mode 100644 index 00000000..e69de29b -- Gitee From 6c49dee1418c4a6005d8820d22cf7f7d5a11f828 Mon Sep 17 00:00:00 2001 From: doepy <8428487+doepy@user.noreply.gitee.com> Date: Sun, 7 Mar 2021 12:03:30 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E7=AC=AC=E5=8D=81?= =?UTF-8?q?=E5=91=A8--=E7=AC=AC=E4=B8=80=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\270\200\350\212\202/.keep" diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\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/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\270\200\350\212\202/.keep" new file mode 100644 index 00000000..e69de29b -- Gitee From 9f14201dd353e0e97e22234da681956dae15bbcc Mon Sep 17 00:00:00 2001 From: doepy <8428487+doepy@user.noreply.gitee.com> Date: Sun, 7 Mar 2021 12:03:42 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E7=AC=AC=E5=8D=81?= =?UTF-8?q?=E5=91=A8--=E7=AC=AC=E4=BA=8C=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\272\214\350\212\202/.keep" diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\272\214\350\212\202/.keep" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\272\214\350\212\202/.keep" new file mode 100644 index 00000000..e69de29b -- Gitee From 0304dc41a584590ae24539905a1be1a3bc0ba3f3 Mon Sep 17 00:00:00 2001 From: doepy <8428487+doepy@user.noreply.gitee.com> Date: Sun, 7 Mar 2021 12:03:55 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E7=AC=AC=E5=8D=81?= =?UTF-8?q?=E5=91=A8--=E7=AC=AC=E4=B8=89=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\270\211\350\212\202/.keep" diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\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/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\270\211\350\212\202/.keep" new file mode 100644 index 00000000..e69de29b -- Gitee From 074114bc39a6b9d88f69fc892daded7f8c7fd3c1 Mon Sep 17 00:00:00 2001 From: doepy <8428487+doepy@user.noreply.gitee.com> Date: Sun, 7 Mar 2021 12:04:23 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E7=AC=AC=E5=8D=81=E5=91=A8--=E7=AC=AC?= =?UTF-8?q?=E4=B8=80=E8=8A=82=20=E7=AC=AC=E5=8D=81=E5=91=A8--=E7=AC=AC?= =?UTF-8?q?=E4=B8=80=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test.py" | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\270\200\350\212\202/test.py" diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\270\200\350\212\202/test.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\270\200\350\212\202/test.py" new file mode 100644 index 00000000..911c7664 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\270\200\350\212\202/test.py" @@ -0,0 +1,11 @@ +#在terminal 那里打入 cd jd_crawler +import sys +print(sys.path) +import os +sys.path.append(os.getcwd()) +from jd_parser.search import parse_jd_item + +with open("test\search.html", "r", encoding="utf_8") as f: + html = f.read() + result = parse_jd_item(html) + print(result) -- Gitee From 976182d90c255df093f7405186be315ed6db1363 Mon Sep 17 00:00:00 2001 From: doepy <8428487+doepy@user.noreply.gitee.com> Date: Sun, 7 Mar 2021 12:05:07 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E7=AC=AC=E5=8D=81=E5=91=A8--=E7=AC=AC?= =?UTF-8?q?=E4=BA=8C=E8=8A=82=20=E7=AC=AC=E5=8D=81=E5=91=A8--=E7=AC=AC?= =?UTF-8?q?=E4=BA=8C=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jd_search.py" | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\272\214\350\212\202/jd_search.py" diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\272\214\350\212\202/jd_search.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\272\214\350\212\202/jd_search.py" new file mode 100644 index 00000000..69e8775b --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\272\214\350\212\202/jd_search.py" @@ -0,0 +1,20 @@ +import scrapy + +class JdSearch(scrapy.Spider): + name = "jd_search" + + def start_resquest(self): + for keyword in ["鼠标","键盘","显卡","耳机"]: + for page_num in range(1,11): + url = f"https://search.jd.com/Search?keyword={keyword}&page={page_num}" + + # 选用FormRequest是因为它即可以发送GET请求,又可以发送POST请求 + yield scrapy.FormRequest( + url = url, + method = 'GET', + #formdata = date, 如果是post请求携带数据使用formdata + callback=self.parse_search + ) + break + def parse_search(self,response): + print(response) \ No newline at end of file -- Gitee From 5391186490e151545ffb2ad6ab4969b0958e7132 Mon Sep 17 00:00:00 2001 From: doepy <8428487+doepy@user.noreply.gitee.com> Date: Sun, 7 Mar 2021 12:06:26 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E7=AC=AC=E5=8D=81=E5=91=A8--=E7=AC=AC?= =?UTF-8?q?=E4=B8=89=E8=8A=82=20=E7=AC=AC=E5=8D=81=E5=91=A8--=E7=AC=AC?= =?UTF-8?q?=E4=B8=89=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jd_search.py" | 54 +++++++++ .../middlewares.py" | 114 ++++++++++++++++++ .../pipelines.py" | 32 +++++ .../run.py" | 5 + .../settings.py" | 103 ++++++++++++++++ 5 files changed, 308 insertions(+) create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\270\211\350\212\202/jd_search.py" create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\270\211\350\212\202/middlewares.py" create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\270\211\350\212\202/pipelines.py" create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\270\211\350\212\202/run.py" create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\270\211\350\212\202/settings.py" diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\270\211\350\212\202/jd_search.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\270\211\350\212\202/jd_search.py" new file mode 100644 index 00000000..6b15ec63 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\270\211\350\212\202/jd_search.py" @@ -0,0 +1,54 @@ +import scrapy +import json +from bs4 import BeautifulSoup +from jd_crawler_scrapy.jd_crawler_scrapy.items import JdCrawlerScrapyItem + +class JdSearch(scrapy.Spider): + name = "jd_search" + + def start_resquest(self): + for keyword in ["鼠标","键盘","显卡","耳机"]: + for page_num in range(1,11): + url = f"https://search.jd.com/Search?keyword={keyword}&page={page_num}" + + # 选用FormRequest是因为它即可以发送GET请求,又可以发送POST请求 + yield scrapy.FormRequest( + url = url, + method = 'GET', + #formdata = date, 如果是post请求携带数据使用formdata + callback=self.parse_search + ) + 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["date-sku"] + img = item.select("img[data-img='1']") + price = item.select("div[class='p-price']") + 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].span.a.attrs['title'] if shop[0].text.strip() 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/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\270\211\350\212\202/middlewares.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\270\211\350\212\202/middlewares.py" new file mode 100644 index 00000000..cdb805d7 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\270\211\350\212\202/middlewares.py" @@ -0,0 +1,114 @@ +# 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 + +from scrapy import signals + +# useful for handling different item types with a single interface +from itemadapter import is_item, ItemAda + + +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): + """ + 在正式请求前为当前请求添加headers + :param request: + :param spider: + :return: + """ + request.headers["user-agent"] = "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Mobile Safari/537.36" \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\270\211\350\212\202/pipelines.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\270\211\350\212\202/pipelines.py" new file mode 100644 index 00000000..e93def15 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\270\211\350\212\202/pipelines.py" @@ -0,0 +1,32 @@ +# 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 +import pymysql +from jd_crawler_scrapy.jd_crawler_scrapy.items import JdCrawlerScrapyItem + +class JdCrawlerScrapyPipeline: + def __init__(self): + self.mysql_com = None + + + + def process_item(self, item, spider): + if not self.mysql_com: + self.mysql_com = pymysql.connect(**spider.settings["MYSQL_COMF"]) + + if isinstance(item,JdCrawlerScrapyItem): + cursor = self.mysql_com.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_com.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/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\270\211\350\212\202/run.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\270\211\350\212\202/run.py" new file mode 100644 index 00000000..46fa91f4 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\270\211\350\212\202/run.py" @@ -0,0 +1,5 @@ +from scrapy import cmdline + + +command = "scrapy crawl jd_search".split() +cmdline.execute(command) \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\270\211\350\212\202/settings.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\270\211\350\212\202/settings.py" new file mode 100644 index 00000000..6f0ae4fe --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/3\347\217\255/3\347\217\255_\351\231\266\345\206\266/3\347\217\255---\351\231\266\345\206\266---\347\254\254\345\215\201\345\221\250\344\275\234\344\270\232 /\347\254\254\345\215\201\345\221\250--\347\254\254\344\270\211\350\212\202/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": "lmc.123", + "db": "tunan_class3" +} + + +#log +LOG_FILE = "F:\log\jd_search.log" +LOG_LEVEL= "DEBUG" \ No newline at end of file -- Gitee