SQLSERVER 創立索引完成代碼。本站提示廣大學習愛好者:(SQLSERVER 創立索引完成代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是SQLSERVER 創立索引完成代碼正文
早據說用python做收集爬蟲異常便利,正好這幾天單元也有如許的需求,須要上岸XX網站下載部門文檔,因而本身親自實驗了一番,後果還不錯。
本例所登錄的某網站須要供給用戶名,暗碼和驗證碼,在此應用了python的urllib2直接登錄網站並處置網站的Cookie。
Cookie的任務道理:
Cookie由辦事端生成,然後發送給閱讀器,閱讀器會將Cookie保留在某個目次下的文本文件中。鄙人次要求統一網站時,會發送該Cookie給辦事器,如許辦事器就曉得該用戶能否正當和能否須要從新登錄。
Python供給了根本的cookielib庫,在初次拜訪某頁面時,cookie便會主動保留上去,以後拜訪其它頁面便都邑帶有正常登錄的Cookie了。
道理:
(1)激活cookie功效
(2)反“反盜鏈”,假裝成閱讀器拜訪
(3)拜訪驗證碼鏈接,並將驗證碼圖片下載到當地
(4)驗證碼的辨認計劃網上較多,python也有本身的圖象處置庫,此例挪用了火車頭收集器的OCR辨認接口。
(5)表單的處置,可用fiddler等抓包對象獲得須要提交的參數
(6)生成須要提交的數據,生成http要求並發送
(7)依據前往的js頁面斷定能否上岸勝利
(8)上岸勝利後下載其它頁面
此例中應用多個賬號輪詢上岸,每一個賬號下載3個頁面。
下載網址由於某些成績,就不洩漏了。
以下是部門代碼:
#!usr/bin/env python
#-*- coding: utf-8 -*-
import os
import urllib2
import urllib
import cookielib
import xml.etree.ElementTree as ET
#-----------------------------------------------------------------------------
# Login in www.***.com.cn
def ChinaBiddingLogin(url, username, password):
# Enable cookie support for urllib2
cookiejar=cookielib.CookieJar()
urlopener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
urllib2.install_opener(urlopener)
urlopener.addheaders.append(('Referer', 'http://www.chinabidding.com.cn/zbw/login/login.jsp'))
urlopener.addheaders.append(('Accept-Language', 'zh-CN'))
urlopener.addheaders.append(('Host', 'www.chinabidding.com.cn'))
urlopener.addheaders.append(('User-Agent', 'Mozilla/5.0 (compatible; MISE 9.0; Windows NT 6.1); Trident/5.0'))
urlopener.addheaders.append(('Connection', 'Keep-Alive'))
print 'XXX Login......'
imgurl=r'http://www.*****.com.cn/zbw/login/image.jsp'
DownloadFile(imgurl, urlopener)
authcode=raw_input('Please enter the authcode:')
#authcode=VerifyingCodeRecognization(r"http://192.168.0.106/images/code.jpg")
# Send login/password to the site and get the session cookie
values={'login_id':username, 'opl':'op_login', 'login_passwd':password, 'login_check':authcode}
urlcontent=urlopener.open(urllib2.Request(url, urllib.urlencode(values)))
page=urlcontent.read(500000)
# Make sure we are logged in, check the returned page content
if page.find('login.jsp')!=-1:
print 'Login failed with username=%s, password=%s and authcode=%s' \
% (username, password, authcode)
return False
else:
print 'Login succeeded!'
return True
#-----------------------------------------------------------------------------
# Download from fileUrl then save to fileToSave
# Note: the fileUrl must be a valid file
def DownloadFile(fileUrl, urlopener):
isDownOk=False
try:
if fileUrl:
outfile=open(r'/var/www/images/code.jpg', 'w')
outfile.write(urlopener.open(urllib2.Request(fileUrl)).read())
outfile.close()
isDownOK=True
else:
print 'ERROR: fileUrl is NULL!'
except:
isDownOK=False
return isDownOK
#------------------------------------------------------------------------------
# Verifying code recoginization
def VerifyingCodeRecognization(imgurl):
url=r'http://192.168.0.119:800/api?'
user='admin'
pwd='admin'
model='ocr'
ocrfile='cbi'
values={'user':user, 'pwd':pwd, 'model':model, 'ocrfile':ocrfile, 'imgurl':imgurl}
data=urllib.urlencode(values)
try:
url+=data
urlcontent=urllib2.urlopen(url)
except IOError:
print '***ERROR: invalid URL (%s)' % url
page=urlcontent.read(500000)
# Parse the xml data and get the verifying code
root=ET.fromstring(page)
node_find=root.find('AddField')
authcode=node_find.attrib['data']
return authcode
#------------------------------------------------------------------------------
# Read users from configure file
def ReadUsersFromFile(filename):
users={}
for eachLine in open(filename, 'r'):
info=[w for w in eachLine.strip().split()]
if len(info)==2:
users[info[0]]=info[1]
return users
#------------------------------------------------------------------------------
def main():
login_page=r'http://www.***.com.cnlogin/login.jsp'
download_page=r'http://www.***.com.cn***/***?record_id='
start_id=8593330
end_id=8595000
now_id=start_id
Users=ReadUsersFromFile('users.conf')
while True:
for key in Users:
if ChinaBiddingLogin(login_page, key, Users[key]):
for i in range(3):
pageUrl=download_page+'%d' % now_id
urlcontent=urllib2.urlopen(pageUrl)
filepath='./download/%s.html' % now_id
f=open(filepath, 'w')
f.write(urlcontent.read(500000))
f.close()
now_id+=1
else:
continue
#------------------------------------------------------------------------------
if __name__=='__main__':
main()