search
HomeBackend DevelopmentPython TutorialCookie introduction and simulated login demonstration

CookieIt can be said that it is closely related to our lives. When you shop on Taobao, those recommended products are the masterpieces of Cookie.

In the previous article, we explained the basic operations of Selenium PhantomJs. Today we bring you an introduction to Cookie and a simulated login demonstration.

Cookie introduction and simulated login demonstration

What are cookies?

In computer terminology, it refers to a method that allows the website server to store a small amount of data on the client's hard drive or memory, or read data from the client's hard drive. a technology.

Open Chrome's developer tools (F12), click on Network, click on a request URL, you can see a cookie information in the request header (Request Headers). Of course, we can see a Set-Cookie information from the above response header, which is some information about the server setting cookies to the browser, such as cookie scope, time, etc.

Purpose of Cookie

Storage user login information to determine whether the user is logged in; save user browsing footprints;

Cookie is stored by the server on the client information. This information is generated by the server and interpreted by the server.

When making a request, the client needs to send untimed cookies to the server. The server needs to parse cookies to determine user information.

Our browser will automatically store cookies every time you browse the web. For example, when you open the clear browser browsing history, there will be cookie information.

Cookies bring us a lot of convenience. They can also record our browsing footprints and the time we stay on the page. For example, when you are crazy about Taobao, Taobao's recommendations for products you like are generated based on your cookies to obtain which products you have browsed.

Simulated login demonstration

Let’s take Zhihu as an example. https://www.zhihu.com/people/yu-kun-73/answers

This URL is my Zhihu information page. Of course, you can use your own Zhihu page as an example. If we visit him directly, we will see the login button above, but I am already logged in and accessing. The login button will not be displayed, but some links to personal edit profiles.

So, what if we use a crawler to access.

import requests
 
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'
}
url = 'https://www.zhihu.com/people/yu-kun-73/answers'
resp = requests.get(url, headers=headers).text
print(resp)

Through the printed results, we can see that there is a login button in the html code. This proves that we are not logged in to this page. So, if we crawl some websites that require a login account to crawl data, we must solve the login problem. So how to implement login.

The answer is our Cookie. We said in a previous article that the requests library is very simple when handling cookies. We just need to add request cookies like we added 'User-Agent'.

import requests
 
headers = {
    'Cookie': # 你的登录过后的浏览器cookies,
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'
}
url = 'https://www.zhihu.com/people/yu-kun-73/answers'
resp = requests.get(url, headers=headers).text
print(resp)

When we look at the print information again, we can see a link to edit personal information. This means we have successfully logged in. Isn't it very simple? Don't worry, the hard part will come later.

at last

So, if we want to log in to some complex websites, such as Sina Weibo, which requires logging in to obtain information, using cookies to log in will definitely not work. Because these websites will frequently update the algorithms of some websites, our cookies will expire after a while, so we need to post our login information to the login page of these websites.

These login information parameters are generally stored in the Form Data under the request header of the login page. As long as we post this information, we can log in smoothly. But if you encounter a verification code, it will be very troublesome. The problem of verification code has always been a threshold for testing crawlers. Let’s study this by ourselves.

The above is the detailed content of Cookie introduction and simulated login demonstration. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
Python vs. C  : Understanding the Key DifferencesPython vs. C : Understanding the Key DifferencesApr 21, 2025 am 12:18 AM

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Python vs. C  : Which Language to Choose for Your Project?Python vs. C : Which Language to Choose for Your Project?Apr 21, 2025 am 12:17 AM

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

Reaching Your Python Goals: The Power of 2 Hours DailyReaching Your Python Goals: The Power of 2 Hours DailyApr 20, 2025 am 12:21 AM

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Maximizing 2 Hours: Effective Python Learning StrategiesMaximizing 2 Hours: Effective Python Learning StrategiesApr 20, 2025 am 12:20 AM

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Choosing Between Python and C  : The Right Language for YouChoosing Between Python and C : The Right Language for YouApr 20, 2025 am 12:20 AM

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python vs. C  : A Comparative Analysis of Programming LanguagesPython vs. C : A Comparative Analysis of Programming LanguagesApr 20, 2025 am 12:14 AM

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

2 Hours a Day: The Potential of Python Learning2 Hours a Day: The Potential of Python LearningApr 20, 2025 am 12:14 AM

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools