Home >Backend Development >Python Tutorial >How to Automate Webpage Login and Retrieve Cookies with Python?
Using Python to Automate Webpage Login and Retrieve Cookies for Subsequent Usage
To access and parse a webpage that requires cookies, you must first establish a login session. This requires sending POST parameters to the webpage's login script. Subsequently, you can retrieve the cookies from the response header and store them for use in subsequent requests to the webpage data.
In Python, this task can be accomplished using the requests library:
<code class="python">from requests import session payload = { 'action': 'login', 'username': USERNAME, 'password': 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) print(response.text)</code>
This code establishes a session with the server, sending POST parameters during login. It then makes a GET request to the data page, and the cookies retrieved during login are automatically used in the request. The response headers and text can then be printed for further processing.
Using the requests library provides a convenient and efficient method to automate webpage login and retrieve cookies, enabling you to access and parse webpage content effectively.
The above is the detailed content of How to Automate Webpage Login and Retrieve Cookies with Python?. For more information, please follow other related articles on the PHP Chinese website!