美文网首页
2019-05-30 比价工具开发

2019-05-30 比价工具开发

作者: 年画儿 | 来源:发表于2019-05-30 20:21 被阅读0次

目前课程看到2-11

"""当当网爬取书籍"""
import requests
from lxml import html

def spider(sn, book_list=[]):
    """ 爬取当当网的数据 """
    url = 'http://search.dangdang.com/?key={sn}&act=input'.format(sn=sn)
    # 获取html内容
    html_data = requests.get(url).text

    # xpath对象
    selector = html.fromstring(html_data)

    # 找到书本列表
    ul_list = selector.xpath('//div[@id="search_nature_rg"]/ul/li')
    print(len(ul_list))
    for li in ul_list:
        # 标题
        title = li.xpath('a/@title')
        print(title[0])
        # 购买链接
        link = li.xpath('a/@href')
        print(link[0])
        # 价格
        price = li.xpath('p[@class="price"]/span[@class="search_now_price"]/text()')
        print(price[0].replace('¥', ''))

        # 商家
        store = li.xpath('p[@class="search_shangjia"]/a/text()')
        store = '当当自营' if len(store) == 0 else store[0]
        print(store)
        print('-----------------------')

        book_list.append({
            'title': title[0],
            'price': price[0].replace('¥', ''),
            'link': link[0],
            'store': store[0]
        })


if __name__ == '__main__':
    sn = '9787115428028'
    spider(sn)

"""京东爬取书籍"""
import requests
from lxml import html


def spider(sn, book_list=[]):
    """ 爬取京东的图书数据 """
    url = 'https://search.jd.com/Search?keyword={0}'.format(sn)
    # 获取HTML文档

    resp = requests.get(url)
    print(resp.encoding)
    resp.encoding = 'utf-8'

    html_doc = resp.text

    # 获取xpath对象
    selector = html.fromstring(html_doc)

    # 找到列表的集合
    ul_list = selector.xpath('//div[@id="J_goodsList"]/ul/li')
    print(len(ul_list))

    # 解析对应的内容,标题,价格,链接
    for li in ul_list:
        # 标题
        title = li.xpath('div/div[@class="p-name"]/a/@title')
        print(title[0])
        # 购买链接
        link = li.xpath('div/div[@class="p-name"]/a/@href')
        print(link[0])

        # 价格
        price = li.xpath('div/div[@class="p-price"]/strong/i/text()')
        print(price[0])

        # 店铺
        store = li.xpath('div//a[@class="curr-shop"]/@title')
        print(store[0])

        book_list.append({
            'title': title[0],
            'price': price[0],
            'link': link[0],
            'store': store[0]
        })


if __name__ == '__main__':
    spider('9787115428028')
"""一号店爬取书籍"""
import requests
from lxml import html


def spider(sn, book_list=[]):
    """ 爬取1号店的图书数据 """
    url = 'https://search.yhd.com/c0-0/k{0}/'.format(sn)
    # 获取到html源码
    html_doc = requests.get(url).text

    # xpath对象
    selector = html.fromstring(html_doc)

    # 书籍列表
    ul_list = selector.xpath('//div[@id="itemSearchList"]/div')
    print(len(ul_list))

    # 解析数据
    for li in ul_list:
        # 标题
        title = li.xpath('div/p[@class="proName clearfix"]/a/@title')
        print(title[0])
        # 价格
        price = li.xpath('div//p[@class="proPrice"]/em/@yhdprice')
        print(price[0])
        # 购买链接
        link = li.xpath('div/p[@class="proName clearfix"]/a/@href')
        print(link[0])
        # 店铺
        store = li.xpath('div/p[@class="storeName limit_width"]/a/@title')
        print(store)

        book_list.append({
            'title': title[0],
            'price': price[0],
            'link': link[0],
            'store': store[0]
        })



if __name__ == '__main__':
    spider('9787115428028')

相关文章

  • 2019-05-30 比价工具开发

    目前课程看到2-11

  • win驱动常用函数

    title: win驱动开发date: 2019-05-30 21:49:57tags: win驱动开发categ...

  • 商品优选

    1.有一些好用的网站或工具帮你完成历史比价,全网比价的。 第一步,就是要转变思路,马上百度“历史比价”就会收到一堆...

  • 2017-06-22

    一、受众: 一群人,认为杂货铺进货比价是大势所趋,要把一个杂货铺进货比价工具做好。(做出工具;让更多人知道有这个工...

  • 01 工厂模式

    挖个坑。。。2019-05-30

  • 【基础系列】SpringBoot 国际化支持实例开发

    【基础系列】SpringBoot 国际化支持实例开发 国际化的支持,对于app开发的小伙伴来说应该比价常见了;作为...

  • 比价

    今天晚上一个海外朋友发微信给我,说要10双乳胶手套,着实把我吓了一跳,我问他确定吗?他回复我至少是这个需求,我说好...

  • 比价

    这些天经常下雨,天气也转凉了。 这周末双休,晚上下班后去超市买点东西,多是自己的早餐奶粉和麦片。这个超市离家里要走...

  • 喵喵折比价工具怎么安装?

    喵喵折是一款提供购物比价服务的购物助手,原名为购物小蜜,是基于浏览器的增强扩展,可在基于Chromium内核的浏览...

  • 2019-05-29 图书比价工具

    基础知识:1.python操作json数据2.Xpath 知识点讲解3.爬虫基础知识4.requests 库的使用...

网友评论

      本文标题:2019-05-30 比价工具开发

      本文链接:https://www.haomeiwen.com/subject/lrrbtctx.html