C#爬虫1688以图搜图API接口功能的实现

背景

在1688有个功能,就是上传图片,就可以找到类似的商品。如下

图片

网址 :https://www.1688.com/

这时候,我们可以使用程序来代替,大批量的完成图片上传功能。

实现思路

1、找到图片上传接口

图片


post请求,form表单中有signature签名

图片

2、再找sign生成接口,全局搜素找一下signature,发现了一个返回signature的接口。

图片


接口链接:https://open-s.1688.com/openservice/ossDataService
 

图片


这个接口也有一个变动的参数 appKey
 

图片


全局搜索后在js文件中查看一下
 

图片


往下找就可以发现appkey的生成了。
 

图片


通过debug来查看生成规则。
 

图片


获取加密时间的接口:https://open-s.1688.com/openservice/.htm?
参数:outfmt =json&serviceIds=cbu.searchweb.config.system.currenttime

需要先请求这个接口,获取加密时间。

   private void go(string parhfile)        {            var response = HttpHelper.CreateGetHttpResponse("https://open-s.1688.com/openservice/.htm?serviceIds=cbu.searchweb.config.system.currenttime&outfmt=json", 5000, null, null);            Stream myResponseStream = response.GetResponseStream();            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));            string retString = myStreamReader.ReadToEnd();            myStreamReader.Close();            myResponseStream.Close();            if (response.StatusCode == HttpStatusCode.OK)            {                var ss = JsonConvert.DeserializeObject<Currenttimemodel>(retString.Replace("cbu.searchweb.config.system.currenttime", "currenttime"));                //  SetText("加密时间:" + ss.currenttime.dataSet.ToString());                getsin(ss.currenttime.dataSet.ToString(), parhfile);            }            else            {                SetText("错误代码:" + response.StatusCode.ToString());            }        }

再把时间和appName 传入 getAppKey。
 

图片


然后e= “appname ; t” ,appName的base64编码之后的结果是 “cGNfdHVzb3U=”

图片

经过encode64返回 i ( appkey)

图片

然后通过上面生成sign的接口:https://open-s.1688.com/openservice/ossDataService

传入参数就行请求,就可以返回signature,policy,accessid。

params = {
     "appName": key,
     "appKey": base64.b64encode(appkey.encode("utf-8")),
         }
  • 1

  • 2

  • 3

  • 4

  /// <summary>        /// sign生成接口        /// </summary>        /// <param name="dataSet"></param>        private void getsin(string dataSet, string parhfile)        {            string appName = "pc_tusou";            //getAppKey            string appKey = Base64.EncodeBase64("utf-8", appName + ";" + dataSet.ToString());            var response = HttpHelper.CreateGetHttpResponse("https://open-s.1688.com/openservice/ossDataService?appName=" + appName + "&appKey=" + appKey, 5000, null, null);            Stream myResponseStream = response.GetResponseStream();            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));            string retString = myStreamReader.ReadToEnd();            myStreamReader.Close();            myResponseStream.Close();            if (response.StatusCode == HttpStatusCode.OK)            {                var ss = JsonConvert.DeserializeObject<Rootsin>(retString);                //  SetText("sign:" + ss.data.signature);                string key = "cbuimgsearch/" + Base64.Getimgname() + Base64.GetTimeStamp() + ".jepg";                var client = new RestClient("https://cbusearch.oss-cn-shanghai.aliyuncs.com/");                client.Timeout = -1;                var request = new RestRequest(Method.POST);                request.AddHeader("Origin", "https://www.1688.com");                client.UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36";                request.AddHeader("Accept", "*/*");                request.AddHeader("Cache-Control", "no-cache");                request.AddHeader("cookie", "_samesite_flag_=true; _tb_token_=ee5138b911917; cookie2=163f6e3722351213514df4c9ab9116f6; t=96e8d0ab6d636f19306c429b276db552; __cn_logon__=false; ali_ab=120.253.224.246.1587973275662.6; l=caJGIJNTkgnFkWiGkSYyeKDwPQuOAiFJdcPgDahIhDlFGpKMvULclIQGPBDmDhmDdCsLYIU; na=ijBRbdRXZeKwRcTHilfNHSt+; ");                request.AddHeader("refer", "https://www.1688.com/");                request.AddParameter("name", Base64.Getname() + ".jpeg");                request.AddParameter("key", key);                request.AddParameter("OSSAccessKeyId", ss.data.accessid);                request.AddParameter("callback", "");                request.AddParameter("policy", ss.data.policy);                request.AddParameter("signature", ss.data.signature);                request.AddParameter("success_action_status", "200");                request.AddFile("file", parhfile);                IRestResponse response2 = client.Execute(request);                Console.WriteLine(response2.Content);                if (response2.StatusCode == HttpStatusCode.OK)                {                    string picurl = "https://s.1688.com/youyuan/index.htm?tab=imageSearch&imageType=oss&imageAddress=" + key + "&spm=";                    SetText3("\r\n" + picurl);                    Write(picurl);                }                else                {                    SetText("错误代码:" + response2.StatusCode.ToString());                }            }            else            {                SetText("错误代码:" + response.StatusCode.ToString());            }        }

3、数据详情接口

图片上传之后,返回的数据接口:
https://search.1688.com/service/imageSearchOfferResultViewService?

图片


参数:
 

图片


imageAddress是在上传图片之后返回的值
requestId 初始化参数,可以为空。
整个流程就是这样了,接着构造请求就可以获取数据了。

图片

完整代码

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.IO;using System.Linq;using System.Net;using System.Net.Http;using System.Net.Http.Headers;using System.Net.Security;using System.Security.Cryptography.X509Certificates;using System.Text;using System.Threading;using System.Threading.Tasks;using System.Windows.Forms;using Newtonsoft.Json;using Reptiles1688;using RestSharp;
namespace WindowsFormsApp1{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }
        private void button1_Click(object sender, EventArgs e)        {            ThreadPool.QueueUserWorkItem(new WaitCallback(crawlingWeb), "test");        }
        public void Write(string s)        {            string path = System.Environment.CurrentDirectory + "\\图片url\\" + Guid.NewGuid().ToString();            if (!Directory.Exists(path))                Directory.CreateDirectory(path);            FileStream fs = new FileStream(path + "\\data.txt", FileMode.Create);            //获得字节数组            byte[] data = System.Text.Encoding.Default.GetBytes(s);            //开始写入            fs.Write(data, 0, data.Length);            //清空缓冲区、关闭流            fs.Flush();            fs.Close();        }
        private delegate void SetLabelDelegate(string value);
        private void SetText(string value)        {            if (this.InvokeRequired)            {                SetLabelDelegate d = new SetLabelDelegate(SetText);                this.Invoke(d, new object[] { value });            }            else            {                textBox1.Text = value.ToString() + textBox1.Text;            }        }
        private delegate void SetLabelDelegate3(string value);
        private void SetText3(string value)        {            if (this.InvokeRequired)            {                SetLabelDelegate3 d = new SetLabelDelegate3(SetText3);                this.Invoke(d, new object[] { value });            }            else            {                textBox3.Text = value.ToString() + textBox3.Text;            }        }
        private void crawlingWeb(object data)        {            for (int aa = 1; aa < 50; aa++)            {                //  SetText3(aa.ToString());                for (int a = 1; a < 4; a++)                {                    string ss = "D:\\pppppppppppppp\\" + a + ".jpg";                    //  SetText3(aa.ToString() + "-" + a);
                    go(ss);                    Thread.Sleep(500);                }            }
            SetText3("\r\n ok");        }
        private void go(string parhfile)        {            var response = HttpHelper.CreateGetHttpResponse("https://open-s.1688.com/openservice/.htm?serviceIds=cbu.searchweb.config.system.currenttime&outfmt=json", 5000, null, null);            Stream myResponseStream = response.GetResponseStream();            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));            string retString = myStreamReader.ReadToEnd();            myStreamReader.Close();            myResponseStream.Close();            if (response.StatusCode == HttpStatusCode.OK)            {                var ss = JsonConvert.DeserializeObject<Currenttimemodel>(retString.Replace("cbu.searchweb.config.system.currenttime", "currenttime"));                //  SetText("加密时间:" + ss.currenttime.dataSet.ToString());                getsin(ss.currenttime.dataSet.ToString(), parhfile);            }            else            {                SetText("错误代码:" + response.StatusCode.ToString());            }        }
        /// <summary>        /// sign生成接口        /// </summary>        /// <param name="dataSet"></param>        private void getsin(string dataSet, string parhfile)        {            string appName = "pc_tusou";            //getAppKey            string appKey = Base64.EncodeBase64("utf-8", appName + ";" + dataSet.ToString());            var response = HttpHelper.CreateGetHttpResponse("https://open-s.1688.com/openservice/ossDataService?appName=" + appName + "&appKey=" + appKey, 5000, null, null);            Stream myResponseStream = response.GetResponseStream();            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));            string retString = myStreamReader.ReadToEnd();            myStreamReader.Close();            myResponseStream.Close();            if (response.StatusCode == HttpStatusCode.OK)            {                var ss = JsonConvert.DeserializeObject<Rootsin>(retString);                //  SetText("sign:" + ss.data.signature);                string key = "cbuimgsearch/" + Base64.Getimgname() + Base64.GetTimeStamp() + ".jepg";                var client = new RestClient("https://cbusearch.oss-cn-shanghai.aliyuncs.com/");                client.Timeout = -1;                var request = new RestRequest(Method.POST);                request.AddHeader("Origin", "https://www.1688.com");                client.UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36";                request.AddHeader("Accept", "*/*");                request.AddHeader("Cache-Control", "no-cache");                request.AddHeader("cookie", "_samesite_flag_=true; _tb_token_=ee5138b911917; cookie2=163f6e3722351213514df4c9ab9116f6; t=96e8d0ab6d636f19306c429b276db552; __cn_logon__=false; ali_ab=120.253.224.246.1587973275662.6; l=caJGIJNTkgnFkWiGkSYyeKDwPQuOAiFJdcPgDahIhDlFGpKMvULclIQGPBDmDhmDdCsLYIU; na=ijBRbdRXZeKwRcTHilfNHSt+; ");                request.AddHeader("refer", "https://www.1688.com/");                request.AddParameter("name", Base64.Getname() + ".jpeg");                request.AddParameter("key", key);                request.AddParameter("OSSAccessKeyId", ss.data.accessid);                request.AddParameter("callback", "");                request.AddParameter("policy", ss.data.policy);                request.AddParameter("signature", ss.data.signature);                request.AddParameter("success_action_status", "200");                request.AddFile("file", parhfile);                IRestResponse response2 = client.Execute(request);                Console.WriteLine(response2.Content);                if (response2.StatusCode == HttpStatusCode.OK)                {                    string picurl = "https://s.1688.com/youyuan/index.htm?tab=imageSearch&imageType=oss&imageAddress=" + key + "&spm=";                    SetText3("\r\n" + picurl);                    Write(picurl);                }                else                {                    SetText("错误代码:" + response2.StatusCode.ToString());                }            }            else            {                SetText("错误代码:" + response.StatusCode.ToString());            }        }    }}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/250257.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

R、python读取空间转录组的8种方式

“ 空间转录组测序主要包括5个步骤&#xff0c;我们着重下游分析部分&#xff1a;空转数据分析和可视化。本篇主分享如何使用python和R读取空转数据&#xff0c;主要使用scanpy stlearn seurat包” 引言 在正式开始之前&#xff0c;我们先看看cellranger流程跑完之后&#xff0…

杰卡德的故事

三个男人分别是杰卡德距离 杰卡德相似系数和杰卡德系数 杰卡德相似系数和杰卡德距离是互为相反数的。 杰卡德系数和杰卡德距离是不是一回事 感觉是一回事

【论文阅读】Uncertainty-aware Self-training for Text Classification with Few Label

论文下载 GitHub bib: INPROCEEDINGS{mukherjee-awadallah-2020-ust,title "Uncertainty-aware Self-training for Few-shot Text Classification",author "Subhabrata Mukherjee and Ahmed Hassan Awadallah",booktitle "NeurIPS",yea…

mybatis高级扩展-插件和分页插件PageHelper

1、建库建表 create database mybatis-example; use mybatis-example; create table emp (empNo varchar(40),empName varchar(100),sal int,deptno varchar(10) ); insert into emp values(e001,张三,8000,d001); insert into emp values(e002,李四,9000,d001); insert into…

OpenHarmony应用开发——创建第一个OpenHarmonry工程

一、前言 本文主要介绍DevEco Studio的相关配置&#xff0c;以及创建第一个OpenHarmony应用程序。 二、详细步骤 打开DevEco Studio. 进入Settings. 随后SDK选择OpenHarmony&#xff0c;并完成下述API的选择与下载. 等待下载完成后&#xff0c;创建第一个Project. 此处选择Emp…

在React中实现好看的动画Framer Motion(案例:跨DOM元素平滑过渡)

前言 介绍 Framer Motion 是一个适用于 React 网页开发的动画库&#xff0c;它可以让开发者轻松地在他们的项目中添加复杂和高性能的动画效果。该库提供了一整套针对 React 组件的动画、过渡和手势处理功能&#xff0c;使得通过声明式的 API 来创建动画变得简单直观。 接下来…

ChatGPT4 Excel 高级组合函数用法index+match完成实际需求

在Excel 函数用法中有一对组合函数使用是非常多的,那就是Index+match组合函数。 接下来我们用一个实际的需求让ChatGPT来帮我们实现一下。 我们给ChatGPT4发送一个prompt:有一个表格A2至A14为业务员B列至H列为1月至7月的销售额,请根据J2单元格的业务员与K2单元格的月份查找出…

DevOps搭建(二)-阿里云镜像仓库的使用详解

博主介绍&#xff1a;Java领域优质创作者,博客之星城市赛道TOP20、专注于前端流行技术框架、Java后端技术领域、项目实战运维以及GIS地理信息领域。 &#x1f345;文末获取源码下载地址&#x1f345; &#x1f447;&#x1f3fb; 精彩专栏推荐订阅&#x1f447;&#x1f3fb;…

使用令牌桶和漏桶实现请求限流逻辑

实现请求限流 令牌桶算法原理实现案例案例目的:实例demo运行结果: 漏桶算法原理:实现案例:案例目的:案例代码运行结果: 令牌桶算法和漏桶算法是两种常用的限流算法&#xff0c;用于控制系统对请求或数据的访问速率。下面分别详细解释这两种算法的原理. 令牌桶算法 原理 令牌桶…

前端传递参数,后端如何接收

目录 简单参数 传递方式 获取方式一 获取方式二 相关注解 实体参数 数组集合参数 传递方式 相关注解 获取方式一 获取方式二 日期参数 传递方式 相关注解 获取方式 json参数 传递方式 相关注解 获取方式 路径参数 传递方式 相关注解 获取方式 传递多个…

DHCP最全讲解!(原理+配置)

一、概述 随着网络规模的不断扩大&#xff0c;网络复杂度不断提升&#xff0c;网络中的终端设备例如主机、手机、平板等&#xff0c;位置经常变化。终端设备访问网络时需要配置IP地址、网关地址、DNS服务器地址等。采用手工方式为终端配置这些参数非常低效且不够灵活。IETF于19…

day04-报表技术PDF

1 EasyPOI导出word 需求&#xff1a;使用easyPOI方式导出合同word文档 Word模板和Excel模板用法基本一致&#xff0c;支持的标签也是一致的&#xff0c;仅仅支持07版本的word也是只能生成后缀是docx的文档&#xff0c;poi对doc支持不好所以easyPOI中就没有支持doc&#xff0c…

【Linux】内核结构

一、Linux内核结构介绍 Linux内核结构框图 二、图解Linux系统架构 三、驱动认知 1、为什么要学习写驱动2、文件名与设备号3、open函数打通上层到底层硬件的详细过程 四、Shell Shell脚本 一、Linux内核结构介绍 Linux 内核是操作系统的核心部分&#xff0c;它负责管理系…

数据结构 之map/set练习

文章目录 1. 只出现一次的数字算法原理&#xff1a;代码&#xff1a; 2. 随机链表的复制算法原理&#xff1a;代码&#xff1a; 3. 宝石与石头算法原理&#xff1a;代码&#xff1a; 4. 坏键盘打字算法原理&#xff1a;代码&#xff1a; 5. 前K个高频单词算法原理&#xff1a;代…

UGUI 鼠标悬浮UI出现弹框,鼠标在图片边缘出现闪烁

1、背景&#xff1a;鼠标悬浮在UI上出现提示框 public class SpecialParam_list : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler {public void OnPointerEnter(PointerEventData eventData){TipBox.Instance.ShowBox(Input.mousePosition, value);}public void …

【从零开始学习--设计模式--代理模式】

返回首页 前言 感谢各位同学的关注与支持&#xff0c;我会一直更新此专题&#xff0c;竭尽所能整理出更为详细的内容分享给大家&#xff0c;但碍于时间及精力有限&#xff0c;代码分享较少&#xff0c;后续会把所有代码示例整理到github&#xff0c;敬请期待。 此章节介绍建…

基于中小微企业_个体工商户的信贷评分卡模型和用户画像(论文_专利_银行调研建模使用)

背景介绍 信用贷款是指由银行或其他金融机构向中小微企业和个体工商户提供的一种贷款产品。该贷款的特点是无需提供抵押品或担保&#xff0c;主要依据借款人的信用状况来进行评估和审批。 中小微企业和个体工商户信用贷款的申请流程相对简单&#xff0c;申请人只需要提供个人…

飞天使-docker知识点6-容器dockerfile各项名词解释

文章目录 docker的小技巧dockerfile容器为什么会出现启动了不暂停查看docker 网桥相关信息 docker 数据卷 docker的小技巧 [rootlight-test playbook-vars[]# docker inspect -f "{{.NetworkSettings.IPAddress}}" d3a9ae03ae5f 172.17.0.4docker d3a9ae03ae5f:/etc…

测试用例设计方法之判定表详解!!

理论部分 判定表是分析和表达多种输入条件下系统执行不同动作的工具&#xff0c;它可以把复杂的逻辑关系和多种 条件组合的情况表达得既具体又明确。 条件桩(Condition Stub)动作桩(Action Stub&#xff09;条件项(Condition Entry&#xff09;动作项(Action Entry&#xff0…

python+requests+pytest 接口自动化实现

最近工作之余拿公司的项目写了一个接口测试框架&#xff0c;功能还不是很完善&#xff0c;算是抛砖引玉了&#xff0c;欢迎各位来吐槽。 主要思路&#xff1a; ①对 requests 进行二次封装&#xff0c;做到定制化效果 ②使用 excel 存放接口请求数据&#xff0c;作为数据驱动 ③…
最新文章