Python crawler access to web pages the get requests a cookie

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.

 from selenium import webdriver

Definition function,Because cookie in requests headers.so:

 headers={}
if headers.get('cookie'):
'No!'
else:
'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 ~:

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

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

上一篇:splay详解(一)


下一篇:第14章 Linux账号管理与ACL权限设置