python 爬照片 模拟浏览器 先登录账号

# -*- coding: utf-8 -*-
"""
Created on Mon Mar 7 10:53:40 2016

@author: root
"""
import urllib2
import ssl
from selenium import webdriver
sel=webdriver.Firefox()


loginurl = 'https://18.63.191.89:80/Account/Login'
sel.get(loginurl)
sel.find_element_by_id('USERNAME').send_keys('admin')
sel.find_element_by_id('Password').send_keys('Admin')
inps=sel.find_elements_by_tag_name('input')
inps[4].click()

 

cookie = [item["name"] + "=" + item["value"] for item in sel.get_cookies()]
cookiestr = ';'.join(item for item in cookie)

ssl._create_default_https_context=ssl._create_unverified_context




def GetUrlImg(url,filename):

headers = {'cookie':cookiestr}
req = urllib2.Request(url, headers = headers)
response = urllib2.urlopen(req)
text = response.read()
if len(text)>0:
fd = open(filename, 'w')
fd.write(text)
fd.close()

#url = 'https://18.63.191.89:80/F/Image/?img=1&id=4355'
#GetUrlImg(url)
#i=2000
for i in range(2753,4517):
url='https://18.63.191.89:80/F/Image/?img=1&id={0}'.format(i)
filename = 'ID_{:0>4}.jpg'.format(i)
print url,filename
GetUrlImg(url,'/root/ID/'+filename)





posted @ 2017-01-16 12:59  ahuo  阅读(280)  评论(1编辑  收藏  举报