Home > Article > Backend Development > How to Access Webpages with Cookies Using Python?
When accessing certain webpages, it may be necessary to first authenticate with the server by setting cookies. This is particularly relevant when downloading and parsing webpages that require login authentication. In this guide, we will explore how to use Python 2.6's builtin modules to login to a webpage via HTTP POST and retrieve the corresponding cookies for later usage.
Suppose we have a website with a login page at "/login.php" and a data page at "/data.php" accessible after successful login. To access the data page, we need to set cookies by sending two POST parameters ("username" and "password") to the login page.
To achieve this in Python, we can use the following steps:
The following Python code demonstrates these steps:
<code class="python">from requests import session payload = { 'username': 'YOUR_USERNAME', 'password': 'YOUR_PASSWORD' } with session() as c: c.post('http://example.com/login.php', data=payload) response = c.get('http://example.com/protected_page.php') print(response.headers) # Prints cookie information print(response.text) # Prints the HTML content of the page</code>
By executing this code, we can successfully login to the webpage, retrieve the associated cookies, and access the restricted content at "/data.php" for further processing.
The above is the detailed content of How to Access Webpages with Cookies Using Python?. For more information, please follow other related articles on the PHP Chinese website!