diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week10/.keep" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week10/.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/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week10/\347\254\254\345\215\201\345\221\250_\347\254\254\344\270\200\350\212\202.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week10/\347\254\254\345\215\201\345\221\250_\347\254\254\344\270\200\350\212\202.py" new file mode 100644 index 0000000000000000000000000000000000000000..a686123464b6040597317dc567145da5af960948 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week10/\347\254\254\345\215\201\345\221\250_\347\254\254\344\270\200\350\212\202.py" @@ -0,0 +1,12 @@ +import sys +import os +sys.path.append(os.getcwd()) +print(sys.path) +from jd_parser.search import parse_jd_item + + +with open("/test/jdsearch.html", "r", encoding ="utf-8") as f: + + html = f.read() + result= parse_jd_item(html) + print(result) \ 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/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/Ipad.html" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/Ipad.html" new file mode 100644 index 0000000000000000000000000000000000000000..5d97a1cd08fdab8f1a22e2b34db30112d405fac8 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/Ipad.html" @@ -0,0 +1,2891 @@ + + + + + + + + + + + + + + + + + + + + +Ipad - 商品搜索 - 京东 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + + +
+
+
+
+
+ +
+
+ +
+ + +
+
+
+
+ + + 我的购物车 +
+
+
+
+ +
+
+
+
+ +
+ + +
+ + +
+
+
+
+ +
+ > + 品牌:Apple +
+ "Ipad" +
+
+
+
+ +
+ +
+ +
+
+
电脑整机:
+
+
+ +
+
+ 确定 + 取消 +
+
+
+ 更多 + +
+
+
+ +
+
+
二手电脑整机:
+ +
+ 更多 + +
+
+
+ +
+
+
屏幕尺寸:
+
+
+ +
+
+ 确定 + 取消 +
+
+
+ 更多 + 多选 +
+
+
+
+
+
存储容量:
+
+
+ +
+
+ 确定 + 取消 +
+
+
+ 更多 + 多选 +
+
+
+
+
+
尺寸:
+ +
+ 更多 + 多选 +
+
+
+
+
+
高级选项:
+ +
+ + + + + + + + + + + + + + +
+
+
+
+
+
+ +
+
+ +
+ + + + + +
+
+
+
+ - +
+
+
+ + 清空 + 确定 +
+
+
+ + 1/100 + + < + > +
+
3.8万+件商品
+ +
+
+
+
配送至
+
+
+
北京
+ +
+
+
+
+
+
+
+ + +
+
+ + +
+ + + +
+
正在加载中,请稍后~~
+
+
+
+
+
+
+ +

商品精选

+
+
    +
+
+
+

精品推荐

+ +
+
+ +
+ + +
+
+
+
+
+ +
商品精选
+
+
+
+
+ +
+
+
+
+ +
+
+
    +
  1. + 品类齐全,轻松购物 +
  2. +
  3. + 多仓直发,极速配送 +
  4. +
  5. + 正品行货,精致服务 +
  6. +
  7. + 天天低价,畅选无忧 +
  8. +
+
+
+
+
+
+
购物指南
+
+ 购物流程 +
+
+ 会员介绍 +
+
+ 生活旅行/团购 +
+
+ 常见问题 +
+
+ 大家电 +
+
+ 联系客服 +
+
+
+
配送方式
+
+ 上门自提 +
+
+ 211限时达 +
+
+ 配送服务查询 +
+
+ 配送费收取标准 +
+
+ 海外配送 +
+
+
+
支付方式
+
+ 货到付款 +
+
+ 在线支付 +
+
+ 分期付款 +
+
+ 公司转账 +
+
+
+
售后服务
+
+ 售后政策 +
+
+ 价格保护 +
+
+ 退款说明 +
+
+ 返修/退换货 +
+
+ 取消订单 +
+
+
+
特色服务
+
+ 夺宝岛 +
+
+ DIY装机 +
+
+ 延保服务 +
+
+ 京东E卡 +
+
+ 京东通信 +
+
+ 京鱼座智能 +
+
+ +
+
+
+
+ + +
+ + + + + diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/details.html" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/details.html" new file mode 100644 index 0000000000000000000000000000000000000000..f13cdc3c6091f99dcddf7f0d1686f519b0ea378f --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/details.html" @@ -0,0 +1,857 @@ + + + + + + 京东(JD.COM)-正品低价、品质保障、配送及时、轻松购物! + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ +
+
+
+
+
    + +
+ + +
+
+ + + +
+
+
+ +
+ +
+
+
+
+
+
+
+
+
+
+
+
+
+
+ + +
+
+
+ +
+ 京东开放平台 + 商羚 + 京东零售云 + 氢舟 + 京东家医 + 家庭医生 + 东联计划 + 网络赚钱 + 青少年牙刷 + 洗面奶 + 唐狮拼色 连衣裙 + 苹果手机 + 琵琶 + 女士鞋 + 实木沙发 + 索尼电视 + 卫浴品牌 + 奥克斯空调 + 百香果 + 索尼耳机 + 尊贵十字对开门直冷冰箱 + SKG十字对开门风冷冰箱 + 美菱冰箱下乡产品 + 美的抽屉冰箱 + 美菱(MeiLing)冰箱 + 冰箱出水口在哪里 + 海尔(haier)bcd-186kb + 95升单门冰箱 + 容声十字门变频冰箱 + 468FDBB + 海尔冰箱266 + 海尔冰箱分期 + 对开门冰箱博世 + 迷你型冰箱 + 食物串味?多门冰箱让生活更简单 + 松下621哪款好?松下621怎么样好用吗? + 药品的储藏方法 + 电冰箱为何突然灯不亮 你是否真的了解 + 多门冰箱MeiLing排行榜,多门冰箱MeiLing十大排名推荐 + 新飞183哪款好?新飞183怎么样好用吗? +
+ + + + +
+ + + + + + + + + +
+ + + + + + + + + + + diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/jd_crawler/main.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/jd_crawler/main.py" new file mode 100644 index 0000000000000000000000000000000000000000..1f67cf2c2814ad6e2f4075efc343038539cfd296 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/jd_crawler/main.py" @@ -0,0 +1,68 @@ +import random +import pymysql +import requests +import sys +print(sys.path) +print(__name__) + +from jd_parser.search import parse_jd_item +from settings import MYSQL_CONF, HEADERS + + +def saver(item_array): + """ + 持久化爬取结果 + :param item_array: + :return: + """ + cursor = mysql_con.cursor() + SQL = """INSERT INTO jdsearch(sku_id, img, price, title, shop, icons) + VALUES (%s, %s, %s, &s, %s, %s)""" + cursor.executemany(SQL, item_array) + mysql_con.commit() + cursor.close() + + + + + + + +def donwloader(task): + """ + 请求目标网址的组件 + :param task: + :return: + """ + + url = "https://search.jd.com/Search" + params = { + "keyword" : task + } + + res = requests.get(url = url, params = params, headers = HEADERS) + return res + + + + +def main(task_array): + """ + 爬虫人物的调度 + :return: + """ + + #体现调度功能(for循环和逻辑语句) + for task in task_array: + result = donwloader(task) + item_array = parse_jd_item(result.text) + print(item_array) + saver(item_array) + + + +if __name__ == "__main__": + #用来代替生产者 + mysql_con = pymysql.connect(**MYSQL_CONF) + task_array = ["鼠标", "键盘", "显卡", "耳机", "Ipad"] + main(task_array) \ 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/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/jd_crawler/parser/search.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/jd_crawler/parser/search.py" new file mode 100644 index 0000000000000000000000000000000000000000..04c26eec25109aace2f1a4e1c32601fae7aec5fe --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/jd_crawler/parser/search.py" @@ -0,0 +1,38 @@ +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']") + 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)) + + + + result.append(sku_id, img, price, title, shop, icons) + + return result + + + +if __name__ == "__main__": + with open("../test/jdsearch.html", "r", encoding ="utf-8") as f: + + html = f.read() + parse_jd_item(html) \ 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/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/jd_crawler/settings.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/jd_crawler/settings.py" new file mode 100644 index 0000000000000000000000000000000000000000..ba9905fca811483da4917ca4e105a59f20c80a9e --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/jd_crawler/settings.py" @@ -0,0 +1,12 @@ +HEADERS = { + "user-agent" : "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36" + "upgrade - insecure - requests: 1" + } + +MYSQL_CONF = { + "host" : "127.0.0.1", + "user" : "root", + "password" : "2596070293h", + "db" : "learning" +} + diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/jd_crawler/test/jdsearch.html" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/jd_crawler/test/jdsearch.html" new file mode 100644 index 0000000000000000000000000000000000000000..9cf8a79f444b4361f58a6245eb60ea0733d46c41 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/jd_crawler/test/jdsearch.html" @@ -0,0 +1,6335 @@ + + + + + + + + + + + + + + + + + + + + +鼠标 - 商品搜索 - 京东 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + + +
+
+
+
+
+ +
+
+ +
+ + +
+
+
+
+ + + 我的购物车 +
+
+
+
+ +
+
+
+
+ +
+ + +
+ + +
+
+
+
+ +
+ > +
+ "鼠标" +
+
+
+
+ +
+ +
+ +
+
+
品牌:
+
+
    +
  • 所有品牌
  • +
  • A
  • +
  • B
  • +
  • C
  • +
  • D
  • +
  • E
  • +
  • F
  • +
  • G
  • +
  • H
  • +
  • I
  • +
  • J
  • +
  • K
  • +
  • L
  • +
  • M
  • +
  • N
  • +
  • O
  • +
  • P
  • +
  • Q
  • +
  • R
  • +
  • S
  • +
  • T
  • +
  • U
  • +
  • W
  • +
  • X
  • +
  • Y
  • +
  • Z
  • +
+
+
+ +
+
已选条件:
    +
    + 确定 + 取消 +
    +
    +
    + 更多 + 多选 +
    +
    +
    +
    +
    +
    外设产品:
    +
    +
    + +
    +
    + 确定 + 取消 +
    +
    +
    + 更多 + +
    +
    +
    + +
    +
    +
    电脑整机:
    +
    +
    + +
    +
    + 确定 + 取消 +
    +
    +
    + 更多 + +
    +
    +
    + +
    +
    +
    连接方式:
    +
    +
    + +
    +
    + 确定 + 取消 +
    +
    +
    + 更多 + 多选 +
    +
    +
    +
    +
    +
    适用场景:
    +
    +
    + +
    +
    + 确定 + 取消 +
    +
    +
    + 更多 + 多选 +
    +
    +
    +
    +
    +
    高级选项:
    + +
    + + + + + + + + + + + +
    +
    +
    +
    +
    +
    + +
    +
    + +
    + + + + + +
    +
    +
    +
    + - +
    +
    +
    + + 清空 + 确定 +
    +
    +
    + + 1/100 + + < + > +
    +
    68万+件商品
    + +
    +
    +
    +
    配送至
    +
    +
    +
    北京
    + +
    +
    +
    +
    +
    +
    +
    + + +
    +
    + + +
    + + + +
    +
    正在加载中,请稍后~~
    +
    +
    +
    +
    +
    +
    + +

    商品精选

    +
    +
      +
    +
    +
    +

    精品推荐

    + +
    +
    + +
    + + +
    +
    +
    +
    +
    + +
    商品精选
    +
    +
    +
    +
    + +
    +
    +
    +
    + +
    +
    +
      +
    1. + 品类齐全,轻松购物 +
    2. +
    3. + 多仓直发,极速配送 +
    4. +
    5. + 正品行货,精致服务 +
    6. +
    7. + 天天低价,畅选无忧 +
    8. +
    +
    +
    +
    +
    +
    +
    购物指南
    +
    + 购物流程 +
    +
    + 会员介绍 +
    +
    + 生活旅行/团购 +
    +
    + 常见问题 +
    +
    + 大家电 +
    +
    + 联系客服 +
    +
    +
    +
    配送方式
    +
    + 上门自提 +
    +
    + 211限时达 +
    +
    + 配送服务查询 +
    +
    + 配送费收取标准 +
    +
    + 海外配送 +
    +
    +
    +
    支付方式
    +
    + 货到付款 +
    +
    + 在线支付 +
    +
    + 分期付款 +
    +
    + 公司转账 +
    +
    +
    +
    售后服务
    +
    + 售后政策 +
    +
    + 价格保护 +
    +
    + 退款说明 +
    +
    + 返修/退换货 +
    +
    + 取消订单 +
    +
    +
    +
    特色服务
    +
    + 夺宝岛 +
    +
    + DIY装机 +
    +
    + 延保服务 +
    +
    + 京东E卡 +
    +
    + 京东通信 +
    +
    + 京鱼座智能 +
    +
    + +
    +
    +
    +
    + + +
    + + + + + diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/jd_crawler/test/parser_test.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/jd_crawler/test/parser_test.py" new file mode 100644 index 0000000000000000000000000000000000000000..a686123464b6040597317dc567145da5af960948 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/jd_crawler/test/parser_test.py" @@ -0,0 +1,12 @@ +import sys +import os +sys.path.append(os.getcwd()) +print(sys.path) +from jd_parser.search import parse_jd_item + + +with open("/test/jdsearch.html", "r", encoding ="utf-8") as f: + + html = f.read() + result= parse_jd_item(html) + print(result) \ 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/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/jd_search.html" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/jd_search.html" new file mode 100644 index 0000000000000000000000000000000000000000..c8b6ed354aee445456055f7d98c100790d0002fd --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/jd_search.html" @@ -0,0 +1,2552 @@ + + + + + + + + + + + + + + + + + + + + + +Ipad - 商品搜索 - 京东 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    +
    + + + +
    +
    +
    +
    +
    + +
    +
    + +
    +
    + +
    +
    +
    +
    + + 0 + 我的购物车 +
    +
    +
    +
    ipad2020|ipadpro|ipad air|华为平板|平板电脑|ipad mini|ipad mini5|平板|ipad air3|ipad2019|ipad air4|华为|iphone12|iphone
    + +
    + + +
    + + +
    +
    +
    +
    + +
    + > + 品牌:Apple +
    + "Ipad" +
    +
    +
    +
    + +
    + +
    + +
    +
    +
    电脑整机:
    +
    +
    + +
    +
    + 确定 + 取消 +
    +
    +
    + + +
    +
    +
    + +
    +
    +
    二手电脑整机:
    + +
    + + +
    +
    +
    + +
    +
    +
    屏幕尺寸:
    +
    +
    + +
    +
    + 确定 + 取消 +
    +
    +
    + + 多选 +
    +
    +
    +
    +
    +
    存储容量:
    +
    +
    + +
    +
    + 确定 + 取消 +
    +
    +
    + 更多 + 多选 +
    +
    +
    +
    +
    +
    尺寸:
    + +
    + + 多选 +
    +
    +
    +
    +
    +
    高级选项:
    + +
    + + + + + + + + + + + + + + +
    +
    +
    +
    +
    +
    + + + + +
    + + + +
    +
    正在加载中,请稍后~~
    +
    <上一页1234567...下一页>100页  到第确定
    +
    +
    +
    +
    +
    + +

    商品精选

    +
    +
      +
    +
    +
    + + +
    +
    + +
    + + +
    +
    +
    +
    +
    + +
    商品精选
    +
    +
    +
    +
    + +
    +
    +
    +
    + +
    +
    +
      +
    1. + 品类齐全,轻松购物 +
    2. +
    3. + 多仓直发,极速配送 +
    4. +
    5. + 正品行货,精致服务 +
    6. +
    7. + 天天低价,畅选无忧 +
    8. +
    +
    +
    +
    +
    +
    +
    购物指南
    +
    + 购物流程 +
    +
    + 会员介绍 +
    +
    + 生活旅行/团购 +
    +
    + 常见问题 +
    +
    + 大家电 +
    +
    + 联系客服 +
    +
    +
    +
    配送方式
    +
    + 上门自提 +
    +
    + 211限时达 +
    +
    + 配送服务查询 +
    +
    + 配送费收取标准 +
    +
    + 海外配送 +
    +
    +
    +
    支付方式
    +
    + 货到付款 +
    +
    + 在线支付 +
    +
    + 分期付款 +
    +
    + 公司转账 +
    +
    +
    +
    售后服务
    +
    + 售后政策 +
    +
    + 价格保护 +
    +
    + 退款说明 +
    +
    + 返修/退换货 +
    +
    + 取消订单 +
    +
    +
    +
    特色服务
    +
    + 夺宝岛 +
    +
    + DIY装机 +
    +
    + 延保服务 +
    +
    + 京东E卡 +
    +
    + 京东通信 +
    +
    + 京鱼座智能 +
    +
    + +
    +
    +
    +
    + + +

    京东会员

    京东会员 0
    成功加入购物车!
    购物车
    成功加入购物车!
    我的消息 0
    成功加入购物车!
    + + + + + + \ 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/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/lesson1.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/lesson1.py" new file mode 100644 index 0000000000000000000000000000000000000000..c6807f9f192369556fce35e2c24e9f15e8b1ad62 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/lesson1.py" @@ -0,0 +1,71 @@ +#京东搜索页面的请求 + +import requests +from urllib.parse import quote, unquote + + +def request_jd(keyword): + f = open(keyword + ".html", "w", encoding = "utf-8") + url = "https://search.jd.com/Search" + params = { + "keyword": keyword + } + headers = { + "user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36" + } + + + response = requests.get(url=url, params=params, headers=headers) + f.write(response.text) + f.close() + + print(response.text) + + +if __name__ == "__main__": + request_jd("Ipad") + print(quote("Ipad")) + + +#京东详情页面的请求 + +import requests +from urllib.parse import quote, unquote + +def requests_jd_details(keyword): + f = open("details.html", "w", encoding = "utf-8") + url = "https://item.jd.com" + + params = { + "keyword" : keyword + } + headers = { + "user - agent": "Mozilla / 5.0(Windows NT 10.0; WOW64) AppleWebKit / 537.36 (KHTML, like Gecko) Chrome / 88.0.4324.96 Safari / 537.36" + } + + response = requests.get(url=url, params=params, headers=headers) + + f.write(response.text) + f.close() + print(response.text) + + +if __name__ == "__main__": + requests_jd_details("AppleIpad") + + +#完成json练习 +import json + +test_dict = { + "a" : 1, + "b" : {"1", 2, None}, + "c" : {"d" : 1} +} +#json格式数据其实是个字符串 +#将python字典转换为json格式 +json_data = json.dumps(test_dict) +print(type(json_data), json_data) + +#将json数据格式转换为字典 +print(json.loads(json_data)) diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/lesson2.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/lesson2.py" new file mode 100644 index 0000000000000000000000000000000000000000..d7cfbfde3c643f6ef718207f1b6d1bf234263829 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_\345\260\217\345\261\213/week9/lesson2.py" @@ -0,0 +1,21 @@ +from bs4 import BeautifulSoup + +def jd_search_parse(html): + html = html.replace('\r\n', "").replace("\n", "").replace("\t", "") + soup = BeautifulSoup(html, "lxml") + item = soup.select("li[data-sku^='2']")[0] + print(item.next_sibling.next_sibling) + print(item.previous_sibling) + print(item.next.sibling) + print(item.parent) + print(item.children) + content = item.text.strip() + value = item.attrs["data-sku"] + + + + +if __name__ == "__main__": + with open("jd_search.html", "r", encoding="utf-8") as f: + html = f.read() + jd_search_parse(html) \ No newline at end of file