python爬取淘宝排名

news/2024/7/19 10:43:11 标签: python, 爬虫, json
import time
import json
import requests
import xlrd
import random
import os

from xlutils.copy import copy #导入模块
excel = r'C:\Users\Administrator\Desktop\查排名.xls'
try:
rdx = xlrd.open_workbook(excel, formatting_info=True) #打开Excel,并保留原格式
except:
print( "no excel in %s " % excel )
newb = copy(rdx) #复制一份做输出
#sh = rdx.sheet_by_index(name)
print('\033[31;1m已录入型号 :\033[0m',(rdx.sheet_names())) #所有sheets.name
#sh=rdx.sheet_names()
for sheet_n in rdx.sheet_names(): #循环整个工作簿
sh1=rdx.sheet_by_name(sheet_n) #工作表对象
w_sheet=newb.get_sheet(sheet_n) #获取sheet名称查
id=str(int(sh1.cell_value(0,1))) #int浮点转整数 str转字符串
print('id' ,sh1.cell_value(0,1), id ,type(id))
print('\033[31;1m查询型号 :\033[0m'+ sheet_n)
tplt = "{:3}\t{:23}" #这里控制输出 行数
for i in range(sh1.nrows): #非空行行数 整个型号的词循环
i1=str(sh1.cell_value(i, 0))
if i1=="": #空行退出
break
time.sleep(random.uniform(1.5,3.6)) #随机浮点数
url='https://s.m.taobao.com/search?q='+i1+'&sst=1&n=20&buying=buyitnow&m=api4h5&token4h5=&abtest=3&wlsort=3&page=name'
        
#url获取地址
1.淘宝手机端链接地址登录s.m.taobao.com
2.输入查询的关键字 审查元素 这里记得刷新一次
3.network 右侧name 一个个打开 需要的信息就在preview里面
4.恭喜找到自己需要的信息

 

 



# url='https://s.m.taobao.com/search?q='+i1+'&sst=2&n=40&buying=buyitnow&m=api4h5&abtest=22&wlsort=22&page=name'
#这里是找到目标真正的地址

headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.name; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
comments = requests.get(url,headers=headers)

#这里有问题临时处理办法
try:
js = json.loads(comments.text)

except json.decoder.JSONDecodeError:
continue

count=0
for j in js['listItem']: #所有店铺信息
count+=1
if j['item_id']==id: #如果id相同
print('j]',j['item_id']==id)
w_sheet.write(i,2,count)
break
else :
# print(tplt.format('没找到',i1))
w_sheet.write(i,2,'没找到')
w_sheet.write(i+1,2,time.strftime('%m-%d ',time.gmtime()))
print(" 任务完成!")
os.remove(r'C:\Users\Administrator\Desktop\查排名.xls')
newb.save(r'C:\Users\Administrator\Desktop\查排名.xls')

print('\033[32;1m查询完成!\033[0m','\n','warning:以上是查询无线两页45位结果!')



ps:记得Excel 不要有多于空白工作表 不然会报错

转载于:https://www.cnblogs.com/sunny666/p/9958875.html


http://www.niftyadmin.cn/n/1089270.html

相关文章

python——多线程,多进程,协程

线程,进程 定义: 进程: 是对各种资源管理的集合,qq 要以一个整体的形式暴露给操作系统管理,里面包含对各种资源的调用,内存的管理,网络接口的调用等 线程: 是操作系统最小的调度单位, 是一串指令…

如何删除链表的最后一个节点_LeetCode 第十九题 删除链表的倒数第N个节点

给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。示例:给定一个链表: 1->2->3->4->5, 和 n 2.​当删除了倒数第二个节点后,链表变为 1->2->3->5.说明:给定的 n 保证是有效的。进…

AutomaticallyProfile 自动化引擎 MyBatis和DB沟通的引擎 (根据数据库信息自动给生成实体类那些...)...

1.首先建立一个文件夹AutomaticallyProfile 文件夹内有四个jar包 :mysql.jar mybatis-generator-core-1.3.2-sources.jar mybatis-generator-core-1.3.2-javadoc.jar mybatis-generator-core-1.3.2.jar 一个go命令:go.cmd 一个xml文件:genera…

vue.js 每次循环五个数据_我是For循环

我是for循环,重复是我的标签,但是我害怕递归。我是单次表达条件式:中间循环体。我喜欢这个名字,For,简单又不失从容。虽然我长得像方块但是有一颗变通的心,简单是我的追求但是也能完成复杂的处理。这是我刚…

Maven中scope的依赖范围

Maven中scope的依赖范围 <dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><!--依赖范围--><scope>test</scope></dependency></depe…

Django-Rest-Framework的序列化之serializers 序列化组件

Django-Rest-Framework的序列化之serializers 序列化组件 restful framework 正常的序列化 from django.http import HttpResponse, JsonResponse from django.views import View from demo import models import json from django.core import serializersclass BookView(View…

c#基础知识第十四节

字符串 class Program { static void Main(string[] args) { string str "欢迎来到新兵锋软件培训.NET世界"; Console.WriteLine(str[0]); Console.WriteLine(str[5]); Console.WriteLine(str[13]); Console.ReadKey();} string类的静态方法 Format()方法 string st…

python数据集分布可视化_Python技能 | Seaborn之数据集分布可视化

Seaborn(二)之数据集分布可视化当处理一个数据集的时候&#xff0c;我们经常会想要先看看特征变量是如何分布的。这会让我们对数据特征有个很好的初始认识&#xff0c;同时也会影响后续数据分析以及特征工程的方法。本篇将会介绍如何使用 seaborn 的一些工具来检测单变量和双变…