python爬虫爬取天猫店铺商品数据

#!coding=utf-8
import requests
import re
import random
import time
import json
from requests.packages.urllib3.exceptions import InsecureRequestWarning
import pandas as pd
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)  ###禁止提醒SSL警告

class tm(object):####手机端

    def __init__(self,path):  ###保存数据路径
        self.path=path

    def goodsid(self,url):  ###通过店铺URL获取店铺所有ID
        shopname = re.search('https://(.*?).tmall', url).group(1)
        searchurl = 'https://{}.m.tmall.com/shop/shop_auction_search.do?spm=a1z60.7754813.0.0.301755f0pZ1GjU&sort=defaul'.format(
            shopname)
        s=requests.session()
        headers = {'Accept': '*/*',
                   'Accept-Language': 'zh-CN',
                   'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) FxiOS/10.6b8836 Mobile/14G60 Safari/603.3.8',
                   'Referer':'https://{}.m.tmall.com/shop/shop_auction_search.htm?spm=a1z60.7754813.0.0.301755f0pZ1GjU&sort=default'.format(shopname)
                   }
        s.headers.update(headers)
        page1=s.get(url=searchurl,verify=False).text
        print(page1)
        js=json.loads(page1)
        total_page=int(js['total_page'])
        shop_id=js['shop_id']
        shop_title = js['shop_title']
        shop_id_list = []
        shop_title_list = []

        item_id=re.findall('"item_id":(.*?),"',page1)
        title=re.findall('"title":"(.*?)","',page1)
        sold=re.findall('"sold":"(.*?)","',page1)
        totalSoldQuantity=re.findall('"totalSoldQuantity":(.*?),"',page1)
        skuurl=re.findall('"url":"(.*?)","',page1)
        price=re.findall('"price":"(.*?)","',page1)
        item_id_l=len(item_id)
        shop_id_list.append(shop_id)
        shop_id_list.extend(shop_id_list*(int(item_id_l)-1))
        shop_title_list.append(shop_title)
        shop_title_list.extend(shop_title_list*(int(item_id_l)-1))
        # print(js)
        # print(len(shop_id_list))
        # print(len(shop_title_list))
        # print(len(item_id))
        # print(len(title))
        # print(len(sold))
        # print(len(totalSoldQuantity))
        # print(len(skuurl))
        # print(len(price))


        data = {'shop_id': shop_id_list,'shop_title': shop_title_list,'item_id': item_id, 'title': title, 'sold':sold, 'totalSoldQu
  • 4
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值