How does python crawler get cookies

Release: 2019-07-06 09:04:24
Original
7283 people have browsed it

How does python crawler get cookies

Cookie refers to the data (usually encrypted) stored on the user's local terminal by some websites in order to identify the user's identity and perform session tracking. For example, some websites require logging in before you can access a certain page. Before logging in, you want to capture the content of a certain page. The content before logging in is different from that after logging in, or it may not be allowed.

In python it provides us with the cookiejar module, which is located in the http package and is used to support Cookie. Through it, we can capture the cookie and resend it on subsequent connection requests, for example, we can implement the simulated login function. The main objects of this module are CookieJar, FileCookieJar, MozillaCookieJar, and LWPCookieJar.

How to obtain cookies

## cookie的获取 # -*- coding: UTF-8 -*- from urllib import request from http import cookiejar if __name__ == '__main__': #声明一个CookieJar对象实例来保存cookie cookie = cookiejar.CookieJar() #利用urllib.request库的HTTPCookieProcessor对象来创建cookie处理器,也就CookieHandler handler=request.HTTPCookieProcessor(cookie) #通过CookieHandler创建opener opener = request.build_opener(handler) #此处的open方法打开网页 response = opener.open('http://www.baidu.com') #打印cookie信息 for item in cookie: print('Name = %s' % item.name) print('Value = %s' % item.value)
Copy after login

For more Python-related technical articles, please visit thePython Tutorialcolumn to learn!

The above is the detailed content of How does python crawler get cookies. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!