• 淘宝商品定向爬取


    #获取淘宝搜索页面的信息,提取其中商品名称和价格
    
    #理解:获得淘宝的搜索接口
    #       翻页的处理
    
    #步骤1:提交商品搜索请求,循环获取网页,首先获得第一页,然后通过循环获得其他页面
    #步骤2:对于每个页面,提取商品名称和价格信息
    #步骤3:将信息输出到屏幕上
    
    
    import requests
    import re
    
    def getHTMLText(url):
        try:
            r = requests.get(url)
            r.raise_for_status()
            r.encoding = r.apparent_encoding
            return r.text
        except:
            return ''
    
    def parsePage(ilt,html):
        try:
            plt = re.findall(r'"price":"[d.]*"',html)
            tlt = re.findall(r'"title":".*?"',html)#使用了最小匹配
            for i in range(len(plt)):
                #eval()函数把获得的字符串的最外层的单引号和双引号去掉
                price = eval(plt[i].split(':')[1])
                title = eval(tlt[i].split(':')[1])
                ilt.append([price,title])
        except:
            print('')
    
    def printGoodsList(ilt):
        tplt = '{:4}	{:8}	{:16}'
        print(tplt.format('序号','价格','商品名称'))
        count = 0
        for g in ilt:
            count = count + 1
            print(tplt.format(count,g[0],g[1]))
    
    
    def main():
        goods = '笔记本电脑' #搜索关键词
        depth = 4 #仅仅爬取两页
        start_url = 'http://s.taobao.com/search?q=' + goods
        infolist = []
        for i in range(depth):
            try:
                url = start_url + '&s=' + str(i*44)
                html = getHTMLText(url)
                parsePage(infolist,html)
            except:
                continue
        printGoodsList(infolist)
    
    main()
  • 相关阅读:
    Prototype.doc in Netsuite
    中文编码问题(utf8转为中文)
    js 取得 Unix时间戳(Unix timestamp)
    关于'跳墙'
    webex js 判断是否是ie 以及兼容性代码
    VLOOKUP函数对查找内容列排序增加效率
    netsuite动态绑定事件
    netsuite filter的选择框 代码控制
    html js 跨域 p3p
    netsuite 记录类型 权限分配 use permissions
  • 原文地址:https://www.cnblogs.com/zhanghaijie/p/8418270.html
Copyright © 2020-2023  润新知