Python實現的百度站長自動URL提交小工具
URL提交是百度提供的一個站長工具,用于給站長提供手工收錄某些URL的接口,但是該接口有驗證碼識別部分,比較難弄。所以編寫了如下程序進行驗證碼自動識別:
主要思路
獲取多個驗證碼,提交到 http://lab.ocrking.com/ 進行多次識別,然后計算每個驗證碼圖片識別出來的 字母或數字 進行統計,得出統計率最高的 即為驗證碼。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import time
import json
import re
if __name__ == "__main__":
i = 1
s = requests.session()
s.headers.update({'Referer':'http://zhanzhang.baidu.com/sitesubmit/index','User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36'})
r = s.get('http://zhanzhang.baidu.com/sitesubmit/index')
s2 = requests.session()
r = s.post('http://zhanzhang.baidu.com/captcha',data={'async':'false','n':time.time()})
url = json.loads(r.content)['url']
temp = []
while 1:
try:
r = s.get(url)
img_data = r.content
r = s2.get('http://lab.ocrking.com/')
try:
content = ' '.join(r.content.split())
sid = re.findall(r'"sid" : "(.+?)"',content)[0]
hash_1 = re.findall(r'"hash" : "(.+?)"',content)[0]
timestamp = re.findall(r'"timestamp" : "(.+?)"',content)[0]
except:
print 'error on get orking info!'
continue
files = {'Filedata':('icode.jpeg', img_data)}
data = {'Filename':'icode.jpeg','sid':sid,'hash':hash_1,'timestamp':timestamp}
r = s2.post('http://lab.ocrking.com/upload.html',files = files,data= data)
r = s2.post('http://lab.ocrking.com/ocrking.html',data={'upfile':r.content,'type':'captcha','charset':'7'})
icode = re.findall(r'<OcrResult>(.+?)</OcrResult>',r.content)[0]
if len(icode) != 4 :
continue
temp.append(icode)
i = i + 1
if i == 3 :
break
except Exception,e:
print e
pass
a = {'0':{},'1':{},'2':{},'3':{}}
for aa in temp:
i = 0
while i <=3 :
try:
a[str(i)][aa[i]] = a[str(i)][aa[i]] + 1
except:
a[str(i)][aa[i]] = 1
i = i + 1
icode = ['','','','']
for index in a:
temp_times = 0
for index_1 in a[index]:
if a[index][index_1] >= temp_times :
temp_times = a[index][index_1]
icode[int(index)] = index_1
icode = ''.join(icode)
img_name = 'temp\\'+icode+'.png'
file_object = open(img_name, 'w')
file_object.write(img_data)
file_object.close()
#r = s.post('http://zhanzhang.baidu.com/sitesubmit/sitepost',data={'url':'http://lab.ocrking.com/','captcha':icode})
#print r.content
相關文章
Python函數的返回值、匿名函數lambda、filter函數、map函數、reduce函數用法實例分析
這篇文章主要介紹了Python函數的返回值、匿名函數lambda、filter函數、map函數、reduce函數用法,結合實例形式分析了Python函數的返回值、匿名函數lambda、filter函數、map函數、reduce函數相關功能、原理與使用技巧,需要的朋友可以參考下2019-12-12pymongo給mongodb創(chuàng)建索引的簡單實現方法
這篇文章主要介紹了pymongo給mongodb創(chuàng)建索引的簡單實現方法,涉及Python使用pymongo模塊操作mongodb的技巧,需要的朋友可以參考下2015-05-05python實戰(zhàn)項目scrapy管道學習爬取在行高手數據
這篇文章主要為介紹了python實戰(zhàn)項目scrapy管道學習拿在行練手爬蟲項目,爬取在行高手數據,本篇博客的重點為scrapy管道pipelines的應用,學習時請重點關注2021-11-11在django項目中導出數據到excel文件并實現下載的功能
這篇文章主要介紹了在django項目中導出數據到excel文件并實現下載的功能,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03