Python simulation login Taobao

Pig brother 66 2020-11-13 07:32:33
python simulation login taobao

Recently, I want to climb some of Taobao's products , However, if you want to use search and other functions, you need to log in , So I came up with an article to simulate the login of Taobao !
 Insert picture description here
Taobao series tutorial :

  • Chapter one :Python Simulated Login Taobao , Explain in detail how to use requests Library login Taobao pc End .
  • Second articles : Taobao auto login 2.0, newly added Cookies serialize , Teach you how to cookies Save up .
  • Third articles :Python Take Taobao condom , Teach you how to climb Taobao pc End product information .
  • Fourth articles :Python analysis 2000 A condom , How to do data analysis and draw conclusions .

Taobao complete series above ,vx Scan the QR code below the code , Focus on vx official account : Naked pigs reply : TaoBao You can get the tutorial and source code !
 Insert picture description here

I read a lot about simulated Login on Taobao , But it's basically using scrapy、pyppeteer、selenium Wait for the library to simulate login , But we haven't talked about these libraries yet , Just about requests library , Let's use it today requests Library simulation login Taobao

Talk about analog login before Taobao , Let's review the previous use of requests Database simulation login Douban and Sina weibo The process of : This kind of simulated Login is a relatively simple login , Just upload and verify the user name and password at the time of login request , That is to say, one step in place !

And Taobao login is more complex , Why is it complicated ? Because Taobao login There are many parameters involved And Ask more than once ! Let's talk about the process of Taobao login first , First, understand the process principle , Go ahead and type the code , It's easy for everyone to understand !

One 、 Taobao login process

For your understanding , Brother pig drew a non-standard Taobao login request sequence diagram
 Insert picture description here

TaoBao ua Parameters :ua(User-Agent) So it's called user agent , Taobao's ua Parameters are added to the browser 、ip、 The computer 、 Time and other information , And then encryption is generated , Use... In many places , It's not just logging in !

Above is a more detailed flow chart , Considering from the code level, brother pig divides the simulated Login to Taobao into the following four steps

  1. After entering the user name , The browser will ask Taobao ( To launch a post Request , Determine whether the slider verification appears !
  2. After the user enters the password , Browser to Taobao ( Launch another post request , Verify that the username and password are correct , If it is correct, return a token.
  3. Browser holding token Go to Alibaba ( In exchange for st code !
  4. Browser access st After a yard , Hold st Code acquisition cookies, Login successful

Some students may ask questions here : Why Taobao ( After the verification is passed, you need to take token Go to Alibaba ( In exchange for st Yard ? Let's talk about this later !
 Insert picture description here

Two 、 Simulated Login implementation

We only talked about the login process , Here, brother pig will first explain the operation of each step in detail , Then paste out the implementation code !

1. Determine if a verification code is needed

At present, when we log in to Taobao , In most cases, the slider verification code will not appear , Piggy tried to log in and out many times, but only once in the middle , What controls whether the slider verification code is needed ?
 Insert picture description here
As can be seen from the above figure , When pig enters the user name ( It must be a mobile phone number ) after , The browser will launch a post request , To verify whether the slider verification code needs to appear , If you return true, The slider verification code appears ! Otherwise, it will not appear , In general, it will not appear !

We can see in the picture this time post The request uploads two parameters :username、ua

Brother pig said before ua For browsers 、ip、 Device information and other multi information encryption parameters , So brother pig guesses whether the verification code of Taobao appears not only from the account point of view , also ip、 Equipment etc !

for instance : A device may have a large number of accounts logged in , At this time, Taobao can start from ua Parameter to get the device number , Then limit the device !

After knowing the process and request links and parameters , We can use code to request !
 Insert picture description here

2. Verify username and password

Here is the first step in the above sequence diagram 5 Step : Request login , The user name will be 、ua Parameters 、 Encryption password, etc 30 A dozen parameters post To Taobao ( To verify .
 Insert picture description here
Let's implement it in code , Don't be frightened by so many parameters , It's all copied from the browser !
 Insert picture description here
Look at the result of the request !
 Insert picture description here
You can see the application st Code link with a token, Specifically token What is it for? Let's analyze it later !

3. apply st code

We have applied to Taobao ( Of token, This step is to use token In exchange for st code .

Many people here may have questions : Why is Taobao so troublesome to log in ? Directly in Login is OK ? Why do you have to be first in taobao Verify username and password , Go to... After passing Exchange for st Code login ?

The framework of any company is the result of a gradual evolution , I think the initial Taobao login is not so complicated . But as Alibaba grows , Many business lines are divided , But these business lines are related , Such as user After logging into the Taobao account, tmall doesn't need to log in anymore ?( Note that Taobao and tmall's top-level domains are different , So we can't share cookis) To solve this problem , Single sign on And that's what happened .

Single sign on (Single Sign On), Referred to as SSO, Is one of the more popular enterprise business integration solutions .SSO Is defined in multiple application systems , Users only need to log in once to access all mutual trust application systems . —— Baidu Encyclopedia

Many large enterprises almost do single sign on , Alibaba's single sign on system must be the parent company Alibaba ( Here we go , All subsidiaries call the parent company interface !

We will come back to analyze why Taobao login is so complicated : User data is here on Taobao , So we need to Taobao now ( Verify user name and password , Verify by generating a token, Browser holding token Go to Alibaba ( Apply for single sign on code (st code ), Alibaba received a request to verify token Pass and return st code , So use token in st The reason for the code is single sign on !

After understanding the design principles , Code implementation is very clear !
 Insert picture description here

4. Use st Code login

Succeed in getting st After the code, we can log in , This step is through st Code to get the login cookies.
 Insert picture description here
Here we have simulated the success of Taobao login !

5. Get Taobao's nickname

In fact, we have successfully logged into Taobao and returned the link to the user's home page , In order to further verify the login success , Just ask for Taobao's user homepage , By the way, take out Taobao's nickname !
 Insert picture description here

3、 ... and 、 summary

Let's summarize a little after the whole talk , Mainly from The code structure and The problem is Two aspects :

1. The code structure

To release a code structure diagram , Let us have an intuitive understanding of
 Insert picture description here
This is the four steps of simulated Login Taobao that we mentioned earlier , But here we are using code to realize !

2. Existing problems

Before writing this tutorial, brother pig also learned about it on the Internet , Then I use the browser and the bag grabbing tool (Charles) Step by step practice , The most important thing is that you first need to understand the general process of Taobao login , Otherwise, you will be confused in actual operation , Let's talk about the current problems and existing problems

  1. The first problem is to unlock Taobao's slider , at present requests There's no good way to crack it , After introducing some crawler frames, let's break them !
  2. Brother pig tried many times (50 More than once ) There is no slider verification code for login and exit .
  3. Someone on the Internet uses an agent ip, It's no use here , As long as you're not super super super frequent and crawling a lot of data , In general, big factories are not very good at sealing ip, Because there is a wide range of injury rate and influence users , Maybe one is the whole community .
  4. When verifying the user name and password in the second step , Uploaded near 30 Parameters , If you put username、ua、 You can try to copy the encrypted password to verify it or not 30 Change one parameter to... In your browser !
  5. There is an occasional mistake in step 3 and step 4 , Just try again !

See here is not feeling Taobao analog login on a lot of clear , Interested students can collect and forward , Try it on your own at weekends . Conquer Taobao login , Other logins are relatively simple !

Access to the source code ,vx Scan the qr code below , Focus on vx official account 「 Naked pigs 」 reply : Simulated Login Taobao Can get !
 Insert picture description here

本文为[Pig brother 66]所创,转载请带上原文链接,感谢

  1. 利用Python爬虫获取招聘网站职位信息
  2. Using Python crawler to obtain job information of recruitment website
  3. Several highly rated Python libraries arrow, jsonpath, psutil and tenacity are recommended
  4. Python装饰器
  5. Python实现LDAP认证
  6. Python decorator
  7. Implementing LDAP authentication with Python
  8. Vscode configures Python development environment!
  9. In Python, how dare you say you can't log module? ️
  10. 我收藏的有关Python的电子书和资料
  11. python 中 lambda的一些tips
  12. python中字典的一些tips
  13. python 用生成器生成斐波那契数列
  14. python脚本转pyc踩了个坑。。。
  15. My collection of e-books and materials about Python
  16. Some tips of lambda in Python
  17. Some tips of dictionary in Python
  18. Using Python generator to generate Fibonacci sequence
  19. The conversion of Python script to PyC stepped on a pit...
  20. Python游戏开发,pygame模块,Python实现扫雷小游戏
  21. Python game development, pyGame module, python implementation of minesweeping games
  22. Python实用工具,email模块,Python实现邮件远程控制自己电脑
  23. Python utility, email module, python realizes mail remote control of its own computer
  24. 毫无头绪的自学Python,你可能连门槛都摸不到!【最佳学习路线】
  25. Python读取二进制文件代码方法解析
  26. Python字典的实现原理
  27. Without a clue, you may not even touch the threshold【 Best learning route]
  28. Parsing method of Python reading binary file code
  29. Implementation principle of Python dictionary
  30. You must know the function of pandas to parse JSON data - JSON_ normalize()
  31. Python实用案例,私人定制,Python自动化生成爱豆专属2021日历
  32. Python practical case, private customization, python automatic generation of Adu exclusive 2021 calendar
  33. 《Python实例》震惊了,用Python这么简单实现了聊天系统的脏话,广告检测
  34. "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system in Python
  35. Convolutional neural network processing sequence for Python deep learning
  36. Python data structure and algorithm (1) -- enum type enum
  37. 超全大厂算法岗百问百答(推荐系统/机器学习/深度学习/C++/Spark/python)
  38. 【Python进阶】你真的明白NumPy中的ndarray吗?
  39. All questions and answers for algorithm posts of super large factories (recommended system / machine learning / deep learning / C + + / spark / Python)
  40. [advanced Python] do you really understand ndarray in numpy?
  41. 【Python进阶】Python进阶专栏栏主自述:不忘初心,砥砺前行
  42. [advanced Python] Python advanced column main readme: never forget the original intention and forge ahead
  43. python垃圾回收和缓存管理
  44. java调用Python程序
  45. java调用Python程序
  46. Python常用函数有哪些?Python基础入门课程
  47. Python garbage collection and cache management
  48. Java calling Python program
  49. Java calling Python program
  50. What functions are commonly used in Python? Introduction to Python Basics
  51. Python basic knowledge
  52. Anaconda5.2 安装 Python 库(MySQLdb)的方法
  53. Python实现对脑电数据情绪分析
  54. Anaconda 5.2 method of installing Python Library (mysqldb)
  55. Python implements emotion analysis of EEG data
  56. Master some advanced usage of Python in 30 seconds, which makes others envy it
  57. python爬取百度图片并对图片做一系列处理
  58. Python crawls Baidu pictures and does a series of processing on them
  59. python链接mysql数据库
  60. Python link MySQL database