Python crawler access to web pages the get requests a cookie

简介: Python in the process of accessing the web page,encounter with cookie,so we need to get it. cookie in Python is form of a dictionary exists ,so coo...

Python in the process of accessing the web page,encounter with cookie,so we need to get it.

cookie in Python is form of a dictionary exists ,so cookie is {'cookie':'cookies'} 

get cookie need webdriver(),Several browsers/drivers are supported (Firefox, Chrome, Internet Explorer, PhantomJS), as well as the Remote protocol.

1 from selenium import webdriver

Definition function,Because cookie in requests headers.so:

1 headers={}
2 if headers.get('cookie'):
3      'No!'
4 else:
5     'YES!'

Definition function:

def p(url,header):
    if header.get('cookie'):
        print 'YES!'
    else:
        print 'NO!'
        headers = {}
        driver=webdriver.Chrome()
        driver.get(url)
        cookie=driver.get_cookies()
        #print cookie
        s = []
        for i in cookie:
            lt.append(i.get('value'))
            s.append(i.get('name') + "=" +i.get('value') )
        #print s
        #headers['cookie'] = ','.join(s) # if headers.get('cookie')
        header.update(headers)         # if no headers.get('cookie')
        driver.quit()
        p(url,header) #xiuluo    

Interface ~:

1 if __name__ == '__main__':
2     header={'data':'dasda'}
3     url = ''
4     p(url,header)

If there is no entry, the function will not be executed ~~!!

 

Welcome to Python world! I have a contract in this world! How about you?
相关文章
|
6月前
|
数据采集 存储 Web App开发
Python爬虫技巧:设置Cookie永不超时的详细指南
Python爬虫技巧:设置Cookie永不超时的详细指南
|
5月前
|
数据采集 存储 数据库
Python爬虫开发:Cookie池与定期清除的代码实现
Python爬虫开发:Cookie池与定期清除的代码实现
|
6月前
|
Linux 数据库 数据安全/隐私保护
Python web Django快速入门手册全栈版,共2590字,短小精悍
本教程涵盖Django从安装到数据库模型创建的全流程。第一章介绍Windows、Linux及macOS下虚拟环境搭建与Django安装验证;第二章讲解项目创建、迁移与运行;第三章演示应用APP创建及项目汉化;第四章说明超级用户创建与后台登录;第五章深入数据库模型设计,包括类与表的对应关系及模型创建步骤。内容精炼实用,适合快速入门Django全栈开发。
245 1
|
5月前
|
数据采集 Web App开发 iOS开发
解决Python爬虫访问HTTPS资源时Cookie超时问题
解决Python爬虫访问HTTPS资源时Cookie超时问题
|
7月前
|
数据采集 存储 NoSQL
Python爬虫Cookie管理最佳实践:存储、清理与轮换
Python爬虫Cookie管理最佳实践:存储、清理与轮换
|
8月前
|
数据采集 人工智能 测试技术
Python有哪些好用且实用的Web框架?
Python 是一门功能强大的编程语言,在多个领域中得到广泛应用,包括爬虫、人工智能、游戏开发、自动化测试和 Web 开发。在 Web 开发中,Python 提供了多种框架以提高效率。以下是几个常用的 Python Web 框架:1) Django:开源框架,支持多种数据库引擎,适合新手;2) Flask:轻量级框架,基于简单核心并通过扩展增加功能;3) Web2py:免费开源框架,支持快速开发;4) Tornado:同时作为 Web 服务器和框架,适合高并发场景;5) CherryPy:简单易用的框架,连接 Web 服务器与 Python 代码。这些框架各有特色,可根据需求选择合适的工具。
395 14
|
10月前
|
数据采集 Web App开发 存储
打造高效的Web Scraper:Python与Selenium的完美结合
本文介绍如何使用Python结合Selenium,通过代理IP、设置Cookie和User-Agent抓取BOSS直聘的招聘信息,包括公司名称、岗位、要求和薪资。这些数据可用于行业趋势、人才需求、企业动态及区域经济分析,为求职者、企业和分析师提供宝贵信息。文中详细说明了环境准备、代理配置、登录操作及数据抓取步骤,并提醒注意反爬虫机制和验证码处理等问题。
244 1
打造高效的Web Scraper:Python与Selenium的完美结合
|
9月前
|
机器学习/深度学习 开发框架 API
Python 高级编程与实战:深入理解 Web 开发与 API 设计
在前几篇文章中,我们探讨了 Python 的基础语法、面向对象编程、函数式编程、元编程、性能优化、调试技巧以及数据科学和机器学习。本文将深入探讨 Python 在 Web 开发和 API 设计中的应用,并通过实战项目帮助你掌握这些技术。
|
2月前
|
算法 Java Go
【GoGin】(1)上手Go Gin 基于Go语言开发的Web框架,本文介绍了各种路由的配置信息;包含各场景下请求参数的基本传入接收
gin 框架中采用的路优酷是基于httprouter做的是一个高性能的 HTTP 请求路由器,适用于 Go 语言。它的设计目标是提供高效的路由匹配和低内存占用,特别适合需要高性能和简单路由的应用场景。
228 4

推荐镜像

更多