我是靠谱客的博主 风趣朋友,最近开发中收集的这篇文章主要介绍淘宝商品数据爬取,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

import requests
import re

def getHTMLText(url):
    try:
        r = requests.get(url,timeout = 30)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except:
        return ""
    

def parsePage(ilt,html):     #getHTMLText抓取下来html文件,parsePage函数在html文件中提取关键信息,ilt作为存储信息的列表
    try:
        plt = re.findall(r'"view_price":"[d.]*"',html)
        tlt = re.findall(r'"raw_title":".*?"',html)
        for i in range(len(plt)):
            price = eval(plt[i].split(':')[1])
            title = eval(tlt[i].split(':')[1])
            ilt.append([price,title])
    except:
        print("")
    
def printGoodsList(ilt):          #打印出parsePage函数存储的信息
    tplt = "{:4}t{:8}t{:16}"    #规定3个字段大小
    print(tplt.format('序号','价格','商品名称'))    #先打出表头
    count = 0
    for g in ilt:   #再打印出parsePage函数存储的信息,信息都已经存储在ilt列表中,g[0]、g[1]表示在ilt列表中第一、二个字段
        count = count + 1
        print(tplt.format(count,g[0],g[1]))
        
    
    
def main():           #开始调用之前的函数
    goods='纸尿裤'
    depth=2           #抓取到第3页
    start_url = 'https://s.taobao.com/search?q=' + goods
    infoList = []
    for i in range(depth):
        try:
            url = start_url + '&s' + str(44*i)
            html = getHTMLText(url)
            parsePage(infoList,html)
        except:
            continue
    printGoodsList(infoList)
    
main()

最后

以上就是风趣朋友为你收集整理的淘宝商品数据爬取的全部内容,希望文章能够帮你解决淘宝商品数据爬取所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(69)

评论列表共有 0 条评论

立即
投稿
返回
顶部