# -*- coding: utf-8 -*-
"""
Created on Mon Mar 21 11:04:54 2017
@author: sl
"""
import requests
import time
#################################################################################
################先找到對應的爬取驗證碼連接,例如我要爬取車違章信息#################################
###############找到車違章鏈接http://smart.gzeis.edu.cn:8081/Content/AuthCode.aspx#####################
#################根據網頁源碼找到對應的登錄鏈接https://www.stc.gov.cn/szwsjj_web/jsp/xxcx/jdcjtwfcx.jsp#######
################根據網頁源碼找到對應的驗證碼鏈接https://www.stc.gov.cn:443/szwsjj_web/ImgServlet.action?######
#################################################################################
def downloads_pic(pic_name):
#url='http://smart.gzeis.edu.cn:8081/Content/AuthCode.aspx'
url='https://www.stc.gov.cn/szwsjj_web/ImgServlet.action?'
res=requests.get(url,stream=True) ####在罕見的情況下你可能想獲取來自服務器的原始套接字響應,那么你可以訪問 r.raw如果你確實想這么干,那請你確保在初始請求中設置了stream=True
print res
with open(r'G:\DownloadsVerificationCode\%s.jpg'%(pic_name),'wb') as f:
print res.iter_content(chunk_size=1024)
for chunk in res.iter_content(chunk_size=1024): ####使用Response.iter_content將會處理大量你直接使用Response.raw不得不處理的.當流下載時,上面是優先推薦的獲取內容方式
print chunk
if chunk: ###過濾下保持活躍的新塊
f.write(chunk)
f.flush() #方法是用來刷新緩沖區的,即將緩沖區中的數據立刻寫入文件,同時清空緩沖區,不需要是被動的等待輸出緩沖區寫入
f.close()
if __name__=='__main__':
for i in range(300):
pic_name=int(time.time()*1000000) #返回當前時間的時間戳(1970紀元后經過的浮點秒數)
downloads_pic(pic_name)