zoukankan      html  css  js  c++  java
  • 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 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 ~~!!

  • 相关阅读:
    写在毕业散伙饭后
    关于2005年高考志愿填报注意事项与分析
    如何在ASP.NET页面间传送数据
    MS Server 2000中的Table类型
    离职申请[转]
    JQUERY 获取text,areatext,radio,checkbox,select值
    DropdownList用法记录
    SQL小记
    认识SortedList类
    js中的|| 与 &&
  • 原文地址:https://www.cnblogs.com/zhuPython/p/7910879.html
Copyright © 2011-2022 走看看