简单的Python爬虫应用_学习笔记003

从京东网站抓取_007

`接上篇内容 从京东网站抓取_006

组织成列表的形式

if __name__ == '__main__':
    bose_info = get_content_from_jd('Bose', '1', '3')  
    bose_content = BeautifulSoup(bose_info)
    bose_div = bose_content.find_all('div', class_ = 'lh-wrap') 

    bose_id = [i.strong['class'][0] for i in bose_div]

    info_results = [] #初始化一个空的列表用来存放抓取的商品信息

    for item in bose_div:
        data = {} #初始化一个空的字典用来存放信息
        data['url'] = item.find('div', class_ = 'p-img').a['href']#商品链接
        data['img'] = item.find('img')['data-lazyload']#商品图片
        data['title'] = item.find('div', class_ = 'p-name').get_text().strip()#商品标题
        data['id'] = item.find('div', class_ = 'p-price').strong['class'][0]#商品id
        info_results.append(data)

    print info_results[0] #通过下标可查询任意一条商品信息

返回内容

{
'url': 'http://item.jd.com/1150767.html', 
'id': 'J_1150767', 
'img': 'http://img12.360buyimg.com/n7/jfs/t160/263/2138317114/91577/2ad0b9a5/53c64152Nfa21b93d.jpg', 
'title': u'Bose SoundLink Mini\u84dd\u7259\u626c\u58f0\u5668 \u84dd\u7259\u97f3\u7bb1'
}

商品价格的抓取
京东网站价格通过GET命令获取,通过chrome调试工具先获取价格网址

Json.PNG

http://p.3.cn/prices/mgets?skuids=J_1150767,J_1225287,J_1150768,J_1237665,J_1439929797,J_1150689,J_1225340,J_1264417,J_1153768,J_1253203,J_1150696,J_1150765,J_1237667,J_1150699,J_1253201,J_1253221,J_1253226,J_1253232,J_1237663,J_1237666,J_1253123,J_1253126,J_1222097,J_1150701,J_1150735,J_1230490,J_1217252,J_1237670,J_1255249,J_1150760&area=1_72_2799_0&type=1
此网址由三个参数组成skuid,area,type

构建函数获取商品价格
函数接受所有商品ID,之后构建如上链接,获取商品价格

import json
def get_price(all_id):
  params = {

            'skuids':','.join(all_id),
            'area':'1_72_2799_0',
            'type':'1'
  }
  html_price = urllib.urlencode(params)
  html_price_content = urllib.urlopen('http://p.3.cn/prices/mgets?'+html_price)
  price_content = html_price_content.read()
  html_price_content.close()
  return json.loads(price_content)

修改主程序增加商品价格字段

if __name__ == '__main__':
    bose_info = get_content_from_jd('Bose', '1', '3')  
    bose_content = BeautifulSoup(bose_info)
    bose_div = bose_content.find_all('div', class_ = 'lh-wrap') 
    # <strong class="J_1150767"></strong>
    bose_id = [i.strong['class'][0] for i in bose_div]

    bose_price = get_price(bose_id)

    info_results = []

    for item in bose_div:
        data = {}

        data['url'] = item.find('div', class_ = 'p-img').a['href']
        data['img'] = item.find('img')['data-lazyload']
        data['title'] = item.find('div', class_ = 'p-name').get_text().strip()
        data['id'] = item.find('div', class_ = 'p-price').strong['class'][0]
        data['price'] = filter(lambda price: price['id'] == data['id'], bose_price)[0]['p']

        info_results.append(data)
    for i in info_results:
        print json.dumps(i, encoding="gb2312", ensure_ascii=False)

返回结果

{"url": "http://item.jd.com/1150767.html", "price": "1880.00", "id": "J_1150767", "img": "http://img12.360buyimg.com/n7/jfs/t160/263/2138317114/91577/2ad0b9a5/53c64152Nfa21b93d.jpg", "title": "Bose SoundLink Mini蓝牙扬声器 蓝牙音箱"}
{"url": "http://item.jd.com/1225287.html", "price": "2680.00", "id": "J_1225287", "img": "http://img12.360buyimg.com/n7/jfs/t382/299/358410988/102067/28bc7fa/5417ef6eN1a471400.jpg", "title": "Bose QuietComfort25有源消噪耳机-黑色  QC25"}
{"url": "http://item.jd.com/1150768.html", "price": "2600.00", "id": "J_1150768", "img": "http://img13.360buyimg.com/n7/jfs/t208/86/2195379170/121054/373a1635/53c6414cNa684fbeb.jpg", "title": "Bose SoundLink 蓝牙扬声器III 蓝牙音箱"}
{"url": "http://item.jd.com/1237665.html", "price": "999.00", "id": "J_1237665", "img": "http://img10.360buyimg.com/n7/jfs/t340/338/1222059663/138599/83529a01/5434b619Nd24a3d4b.jpg", "title": "Bose SoundLink Colour蓝牙扬声器-红色"}
{"url": "http://item.jd.com/1439929797.html", "price": "138.00", "id": "J_1439929797", "img": "http://img12.360buyimg.com/n7/jfs/t700/310/356730772/324161/83da6250/54a3bc7eNfa25365f.jpg", "title": "山水D11迷你音响便携式插卡收音机老人带mp3音乐播放器外放小音箱 红色"}
{"url": "http://item.jd.com/1150689.html", "price": "1390.00", "id": "J_1150689", "img": "http://img14.360buyimg.com/n7/jfs/t208/317/2213636018/63579/6e4b116f/53c748efN7baca4a7.jpg", "title": "Bose SoundTrue 耳罩式耳机-薄荷黑"}
{"url": "http://item.jd.com/1225340.html", "price": "2680.00", "id": "J_1225340", "img": "http://img10.360buyimg.com/n7/jfs/t223/352/2430825655/67306/a9994f48/5417ef44N0c7a8927.jpg", "title": "Bose QuietComfort25有源消噪耳机-白色  QC25"}
{"url": "http://item.jd.com/1264417.html", "price": "230.00", "id": "J_1264417", "img": "http://img12.360buyimg.com/n7/jfs/t517/72/112548631/56020/8bda8115/544ef80cN9a010b1f.jpg", "title": "Bose SoundLink Mini蓝牙扬声器封套-橙色 蓝牙音箱配件"}
{"url": "http://item.jd.com/1153768.html", "price": "2900.00", "id": "J_1153768", "img": "http://img13.360buyimg.com/n7/jfs/t166/272/2180604273/69161/fb60368e/53c6403dNee2eb466.jpg", "title": "Bose QC3有源消噪耳机"}
{"url": "http://item.jd.com/1253203.html", "price": "860.00", "id": "J_1253203", "img": "http://img13.360buyimg.com/n7/jfs/t322/59/1919666393/48470/e8feae8a/5444db09N9675a5b6.jpg", "title": "Bose SoundTrue 耳塞式耳机-Audio白色"}
{"url": "http://item.jd.com/1150696.html", "price": "1390.00", "id": "J_1150696", "img": "http://img11.360buyimg.com/n7/jfs/t148/316/2168034496/55985/33491bc7/53c748f6N18f6bba2.jpg", "title": "Bose SoundTrue 耳罩式耳机-白色"}
{"url": "http://item.jd.com/1150765.html", "price": "1190.00", "id": "J_1150765", "img": "http://img10.360buyimg.com/n7/jfs/t301/274/605583376/53605/b339d875/541a76b2N5ccb18e3.jpg", "title": "Bose SoundTrue 贴耳式耳机-薄荷绿"}
{"url": "http://item.jd.com/1237667.html", "price": "999.00", "id": "J_1237667", "img": "http://img12.360buyimg.com/n7/jfs/t289/182/1218390339/163662/9fe085a4/5434b621Naedfdec8.jpg", "title": "Bose SoundLink Colour蓝牙扬声器-蓝色"}
{"url": "http://item.jd.com/1150699.html", "price": "1100.00", "id": "J_1150699", "img": "http://img14.360buyimg.com/n7/jfs/t181/31/2177504157/57468/1c4635a/53c748feN97183aa9.jpg", "title": "BOSE FreeStyle耳塞式耳机-靛蓝色"}
{"url": "http://item.jd.com/1253201.html", "price": "860.00", "id": "J_1253201", "img": "http://img11.360buyimg.com/n7/jfs/t346/156/1911266815/54489/7c8e3997/5444db0dNa623d2b8.jpg", "title": "Bose SoundTrue 耳塞式耳机-Audio黑色"}
{"url": "http://item.jd.com/1253221.html", "price": "1100.00", "id": "J_1253221", "img": "http://img11.360buyimg.com/n7/jfs/t289/140/1935678963/56537/582956b2/5444db00N572caa4a.jpg", "title": "Bose SoundTrue 耳塞式耳机-MFI黑色"}
{"url": "http://item.jd.com/1253226.html", "price": "1100.00", "id": "J_1253226", "img": "http://img11.360buyimg.com/n7/jfs/t277/71/1930074315/50436/5acf672a/5444dafcN0c4791d5.jpg", "title": "Bose SoundTrue 耳塞式耳机-MFI白色"}
{"url": "http://item.jd.com/1253232.html", "price": "1100.00", "id": "J_1253232", "img": "http://img12.360buyimg.com/n7/jfs/t328/196/1930225986/61739/bbec7731/5444daf9Nddbecfb6.jpg", "title": "Bose SoundTrue 耳塞式耳机-MFI红色"}
{"url": "http://item.jd.com/1237663.html", "price": "999.00", "id": "J_1237663", "img": "http://img13.360buyimg.com/n7/jfs/t319/242/1280480196/152009/5fe7709a/5434b615Nbcfa5d7c.jpg", "title": "Bose SoundLink Colour蓝牙扬声器-白色"}
{"url": "http://item.jd.com/1237666.html", "price": "999.00", "id": "J_1237666", "img": "http://img11.360buyimg.com/n7/jfs/t304/242/1241674581/146981/c06eaa1f/5434b61dN9c7696b3.jpg", "title": "Bose SoundLink Colour蓝牙扬声器-黑色"}
{"url": "http://item.jd.com/1253123.html", "price": "1280.00", "id": "J_1253123", "img": "http://img13.360buyimg.com/n7/jfs/t292/198/1897986217/53974/f562c7e1/5444db17Ne54d0602.jpg", "title": "Bose SoundSport 耳塞式运动耳机-MFI蓝色"}
{"url": "http://item.jd.com/1253126.html", "price": "1280.00", "id": "J_1253126", "img": "http://img11.360buyimg.com/n7/jfs/t292/189/1898519967/51313/51a8a428/5444db10Nf3ff2fd3.jpg", "title": "Bose SoundSport 耳塞式运动耳机-MFI绿色"}
{"url": "http://item.jd.com/1222097.html", "price": "1300.00", "id": "J_1222097", "img": "http://img12.360buyimg.com/n7/jfs/t361/202/251727987/123343/f26572be/5412c577N18d44163.jpg", "title": "Bose SoundDock XT 扬声器-黄色"}
{"url": "http://item.jd.com/1150701.html", "price": "1100.00", "id": "J_1150701", "img": "http://img11.360buyimg.com/n7/jfs/t190/337/2171347628/71312/aaee683/53c74901N1f1f18c6.jpg", "title": "BOSE FreeStyle耳塞式耳机-冰蓝色"}
{"url": "http://item.jd.com/1150735.html", "price": "2100.00", "id": "J_1150735", "img": "http://img10.360buyimg.com/n7/jfs/t145/287/2185253825/90804/2249f972/53c748dfN2ddf2080.jpg", "title": "Bose AE2w蓝牙 音乐耳机"}
{"url": "http://item.jd.com/1230490.html", "price": "3900.00", "id": "J_1230490", "img": "http://img10.360buyimg.com/n7/jfs/t271/122/797716279/40566/938f086d/54250485Nd234341a.jpg", "title": "Bose Solo 15 电视音响"}
{"url": "http://item.jd.com/1217252.html", "price": "280.00", "id": "J_1217252", "img": "http://img12.360buyimg.com/n7/jfs/t391/286/362695776/68919/aa56f3ad/5417d812N5f28a56b.jpg", "title": "Bose SoundLink 蓝牙扬声器III封套-灰色"}
{"url": "http://item.jd.com/1237670.html", "price": "200.00", "id": "J_1237670", "img": "http://img10.360buyimg.com/n7/jfs/t289/169/1495591883/259103/d0ae681e/543ceff6N239ff5f6.jpg", "title": "Bose SoundLink Colour蓝牙扬声器 便携包"}
{"url": "http://item.jd.com/1255249.html", "price": "988.00", "id": "J_1255249", "img": "http://img14.360buyimg.com/n7/jfs/t280/299/1915466812/56681/67fa4c6b/5444dae9Na3c39a8c.jpg", "title": "Bose SoundTrue 耳塞式耳机-SMSG黑色"}
{"url": "http://item.jd.com/1150760.html", "price": "1190.00", "id": "J_1150760", "img": "http://img10.360buyimg.com/n7/jfs/t172/238/2201679276/52515/cab70722/53c748baN815e7a20.jpg", "title": "Bose SoundTrue 贴耳式耳机-白色"}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,802评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,109评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,683评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,458评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,452评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,505评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,901评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,550评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,763评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,556评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,629评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,330评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,898评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,897评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,140评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,807评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,339评论 2 342

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,370评论 25 707
  • 从京东网站抓取_006 目标:抓取的内容组织成如下字典形式 分析及组织访问网址字段以键对值的形式表示,其中psor...
    plectrum阅读 780评论 0 5
  • 1 前言 作为一名合格的数据分析师,其完整的技术知识体系必须贯穿数据获取、数据存储、数据提取、数据分析、数据挖掘、...
    whenif阅读 18,051评论 45 523
  • 雨里 风吹的那么用劲 风中 你笑的多么痴情 殊不知 伞下的错过 遮掩了 原本荒唐的曾经
    馐澀de純綪寞鹿阅读 126评论 1 3
  • 秋雨过后的夜晚 没有月亮 路灯惺忪着眼 没有风 蛐蛐在草丛里鸣叫 独自一人 徜徉在夜色里 谁的心思 在雨后的夜里 ...
    丝美天下阅读 209评论 0 0