How to get cookies in python

 

In a web application, when a user visits a website, they usually request access to some user information (for example: Cookie) stored on the server, which contains some personal information of the user, such as: name, address, password, etc. For the user, if the information is not needed, the data on the server may not be requested, but the server will save the information somewhere. A cookie is an access credential in the form of data (data encrypted certificate), and users will request these cookies when using web applications.

  • 1. Types of cookies used

    There are many types of cookies, and different cookie types correspond to different types of data. Therefore, when obtaining cookies, it is necessary to identify the correct type of data. In this example, we need to get two data, username and password. In the method of obtaining the user name and password, we can use the following code: Three parameters are used in the above code: As you can see, these three parameters correspond to three cookie types. But because there are a large number of websites on the Internet that use POST to obtain data, we will not introduce the method of POST to obtain Cookie in detail here. Of course, you can also use some other methods to obtain cookies: Since obtaining cookies by POST will occupy resources, you can use the following code: In the above code, the network method is used to obtain cookies. This method requires the network environment to support Nginx and Session. implementation, let's take a look at other ways to get cookies:

  • 2. Use the PATH parameter

    If you are using the Python language, you can use the PATH parameter to get the cookies stored on the server. The PATH parameter is a string type parameter, which is used to specify whether the browser or server can read or write the cookie. The following is an example of using the PATH parameter to obtain cookies: You can submit this code to the test.py file, and then read the cookie from the Test.py file and save it locally. This file contains a cookie that can be used as part of the web application. This code will try to get all cookies saved on the server, if successful, the cookie is valid. Here we can see that two cookies are stored on the server: cs.cookie and cs.cookies. In the previous example, we successfully fetched all cookies from the Test.py file. But if we want to get cookies from another website, we need to add a new file to save all cookies on this website, and the path to add this new file must be:

  • 3. Use regular expressions

    Regular expressions are the most powerful syntax in Python, which can quickly search for matching specified strings. When we want to find a specific string, we can use regular expressions to match. Example: In the above example, we have used a regular expression to search for a string. When we call this regex, it will search for all characters in the specified string. Of course, regular expressions are not a panacea, and we need to use other methods to find the corresponding matching string. Finally, we use python's cookie function to get the corresponding cookie. Through the above two methods, we can quickly find the corresponding string from the cookie.

  • 4. Use other methods

    Below we introduce some other ways to get Cookie: Using the getCookie () method, before getting the Cookie, you first need to get the name and type of the getCookie object. This method judges after the type of the object is obtained, and if it is legal, saves the obtained Cookie into a variable. Use the jobs.in se rt () method, which is to judge after the object type is obtained.

  • 5. Separate request and response

    On the server, the object of the request is the Cookie, and the object of the response is the user's account number and password. If we want to obtain the user's account and password, we only need to change the request object to the user's account and password, and then request the server to obtain it. First, we can use regular expressions to match user accounts and passwords. For example: However, in the regular expression, we don't see information about cookies. When using regular expressions, we need to pay attention to some rules: 1) Only uppercase and lowercase letters can be used in the string; 2) Numbers or underscores cannot be used; 3) Special characters (spaces, carriage returns) cannot be used; if We want to get the user account and password, we need to add the account and password to be obtained in the regular expression.

  • 6. Sample code for getting cookies

    To get cookies, you need to use a Python library, this library is pygame. content (), this library can be used to get cookies on the website. To obtain Cookie, you need to use a Python module: Before obtaining Cookie, you need to do some preparatory work: ① First, you need to install the pygame. content () function on the web server ② Then connect the pygame. content () function to a remote Server, the server is located in a virtual host ③ Then pass the cookie parameter to the pygame. content () function of this virtual host, and set the scope of the parameter (that is, whether to get all cookies on the website). If you need to obtain the cookies on a specific website, you can specify the website, for example: to obtain the cookies of a certain website on a specific website. Finally connect the pygame.content() function to the cookie list.

The following are some commonly used Python codes to obtain Cookies:
1. Use the requests library to obtain Cookies:
```python
import requests
# Send a GET request to obtain Cookies
response = requests.get('http://www.example.com' )
cookie = response.cookies.get_dict()
# Output Cookie
print(cookie)
```
2. Use urllib library to get Cookie:
```python
import urllib.request
# Send GET request to get Cookie
response = urllib.request.urlopen ('http://www.example.com')
cookie = response.headers.get('Set-Cookie')
# Output Cookie
print(cookie)
```
3. Use selenium library to get Cookie:
```python
from selenium import webdriver
# start browser
browser = webdriver.Chrome()
# Open the webpage and get Cookie
browser.get('http://www.example.com')
cookie = browser.get_cookies()
# Output Cookie
print(cookie)
```
The above code is for reference only, the specific implementation method can be based on Adjust to actual needs.

Guess you like

Origin blog.csdn.net/qq_42751978/article/details/130618509