diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_\345\230\230\345\230\230/1\347\217\255_\345\230\230\345\230\230_\347\254\254\344\271\235\345\221\250_\344\275\234\344\270\232/1\347\217\255_\345\230\230\345\230\230_\347\254\254\344\271\235\345\221\250_\347\254\254\344\270\211\350\212\202\350\257\276\344\275\234\344\270\232/jd_parser/search.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_\345\230\230\345\230\230/1\347\217\255_\345\230\230\345\230\230_\347\254\254\344\271\235\345\221\250_\344\275\234\344\270\232/1\347\217\255_\345\230\230\345\230\230_\347\254\254\344\271\235\345\221\250_\347\254\254\344\270\211\350\212\202\350\257\276\344\275\234\344\270\232/jd_parser/search.py" new file mode 100644 index 0000000000000000000000000000000000000000..46dbcb2904d783ebbbcac57204ca2bfea52b6d85 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_\345\230\230\345\230\230/1\347\217\255_\345\230\230\345\230\230_\347\254\254\344\271\235\345\221\250_\344\275\234\344\270\232/1\347\217\255_\345\230\230\345\230\230_\347\254\254\344\271\235\345\221\250_\347\254\254\344\270\211\350\212\202\350\257\276\344\275\234\344\270\232/jd_parser/search.py" @@ -0,0 +1,33 @@ +from bs4 import BeautifulSoup +import json + +def parse_jd_item(html): + result = [] + + soup = BeautifulSoup(html, "lxml") + item_array = soup.select("ul[class='gl-warp clearfix'] li[class='gl-item']") + for item in item_array: + 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 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 '[]' + + result.append((sku_id, img, price, title, shop, icons)) + + return result + + + +if __name__ == "__main__": + with open("search.html", "r", encoding="utf_8") as f: + html = f.read() + result = parse_jd_item(html) + print(result) diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_\345\230\230\345\230\230/1\347\217\255_\345\230\230\345\230\230_\347\254\254\344\271\235\345\221\250_\344\275\234\344\270\232/1\347\217\255_\345\230\230\345\230\230_\347\254\254\344\271\235\345\221\250_\347\254\254\344\270\211\350\212\202\350\257\276\344\275\234\344\270\232/main.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_\345\230\230\345\230\230/1\347\217\255_\345\230\230\345\230\230_\347\254\254\344\271\235\345\221\250_\344\275\234\344\270\232/1\347\217\255_\345\230\230\345\230\230_\347\254\254\344\271\235\345\221\250_\347\254\254\344\270\211\350\212\202\350\257\276\344\275\234\344\270\232/main.py" index 0e0cbb8e923bca4a1ed2cbb60b02926ad25cbca1..97c3ff85c4b45a5c489518a9d65da0415eaf44b2 100644 --- "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_\345\230\230\345\230\230/1\347\217\255_\345\230\230\345\230\230_\347\254\254\344\271\235\345\221\250_\344\275\234\344\270\232/1\347\217\255_\345\230\230\345\230\230_\347\254\254\344\271\235\345\221\250_\347\254\254\344\270\211\350\212\202\350\257\276\344\275\234\344\270\232/main.py" +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_\345\230\230\345\230\230/1\347\217\255_\345\230\230\345\230\230_\347\254\254\344\271\235\345\221\250_\344\275\234\344\270\232/1\347\217\255_\345\230\230\345\230\230_\347\254\254\344\271\235\345\221\250_\347\254\254\344\270\211\350\212\202\350\257\276\344\275\234\344\270\232/main.py" @@ -2,6 +2,9 @@ import random import pymysql import requests from search import parse_jd_item +import sys +print(sys.path) +sys.path.append(r"C:\Users\张虚平\PycharmProjects\认识python\second-python-bootcamp\第二期训练营\1班\1班_嘘嘘\1班_嘘嘘_第九周_作业\1班_嘘嘘_第九周_第三节课作业") from settings import MYSQL_CONF, HEADERS import time import threading @@ -39,6 +42,7 @@ def downloader(task): return res + def main(task_array): """ 爬虫任务的调度 @@ -54,7 +58,7 @@ if __name__ == "__main__": #用来代替生产者 mysql_con = pymysql.connect(**MYSQL_CONF) task_array = ["鼠标", "键盘", "显卡", "耳机"] - main(task_array) - for i in range(1, 5): - t = threading.Thread(target=main(task_array)) - t.start() \ No newline at end of file + # main(task_array) + # for i in range(1, 5): + # t = threading.Thread(target=main(task_array)) + # t.start() \ No newline at end of file