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?
相关文章
|
4月前
|
存储 Web App开发 前端开发
Python + Requests库爬取动态Ajax分页数据
Python + Requests库爬取动态Ajax分页数据
|
4月前
|
Web App开发 安全 数据安全/隐私保护
利用Python+Requests实现抖音无水印视频下载
利用Python+Requests实现抖音无水印视频下载
|
4月前
|
JSON 网络安全 数据格式
Python网络请求库requests使用详述
总结来说,`requests`库非常适用于需要快速、简易、可靠进行HTTP请求的应用场景,它的简洁性让开发者避免繁琐的网络代码而专注于交互逻辑本身。通过上述方式,你可以利用 `requests`处理大部分常见的HTTP请求需求。
465 51
|
4月前
|
数据采集 API 调度
Python爬虫框架对比:Scrapy vs Requests在API调用中的应用
本文对比了 Python 中 Scrapy 与 Requests 两大爬虫框架在 API 调用中的差异,涵盖架构设计、调用模式、性能优化及适用场景,并提供实战建议,助力开发者根据项目需求选择合适工具。
|
5月前
|
Linux 数据库 数据安全/隐私保护
Python web Django快速入门手册全栈版,共2590字,短小精悍
本教程涵盖Django从安装到数据库模型创建的全流程。第一章介绍Windows、Linux及macOS下虚拟环境搭建与Django安装验证;第二章讲解项目创建、迁移与运行;第三章演示应用APP创建及项目汉化;第四章说明超级用户创建与后台登录;第五章深入数据库模型设计,包括类与表的对应关系及模型创建步骤。内容精炼实用,适合快速入门Django全栈开发。
228 1
|
6月前
|
网络协议 API 开发者
分析http.client与requests在Python中的性能差异并优化。
合理地选择 `http.client`和 `requests`库以及在此基础上优化代码,可以帮助你的Python网络编程更加顺利,无论是在性能还是在易用性上。我们通常推荐使用 `requests`库,因为它的易用性。对于需要大量详细控制的任务,或者对性能有严格要求的情况,可以考虑使用 `http.client`库。同时,不断优化并管理员连接、设定合理超时和重试都是提高网络访问效率和稳定性的好方式。
157 19
|
5月前
|
JSON 数据格式 Python
解决Python requests库POST请求参数顺序问题的方法。
总之,想要在Python的requests库里保持POST参数顺序,你要像捋顺头发一样捋顺它们,在向服务器炫耀你那有条不紊的数据前。抓紧手中的 `OrderedDict`与 `json`这两把钥匙,就能向服务端展示你的请求参数就像经过高端配置的快递包裹,里面的商品摆放井井有条,任何时候开箱都是一种享受。
128 10
|
5月前
|
网络协议 API Python
解析http.client与requests在Python中的性能比较和改进策略。
最后,需要明确的是,这两种库各有其优点和适用场景。`http.client` 更适合于基础且并行的请求,`requests` 则因其易用且强大的功能,更适用于复杂的 HTTP 场景。对于哪种更适合你的应用,可能需要你自己进行实际的测试来确定。
159 10
|
5月前
|
XML JSON 安全
分析参数顺序对Python requests库进行POST请求的影响。
最后,尽管理论上参数顺序对POST请求没影响,但编写代码时仍然建议遵循一定的顺序和规范,比如URL总是放在第一位,随后是data或json,最后是headers,这样可以提高代码的可读性和维护性。在处理复杂的请求时,一致的参数顺序有助于调试和团队协作。
187 9

推荐镜像

更多
下一篇
oss云网关配置