【百度云搜索,搜各種資料:http://www.bdyss.cn】
【搜網(wǎng)盤,搜各種資料:http://www.swpan.cn】
第一步。首先下載,大神者也的倒立文字驗證碼識別程序
下載地址:https://github.com/muchrooms/...
注意:此程序依賴以下模塊包
Keras==2.0.1
Pillow==3.4.2
jupyter==1.0.0
matplotlib==1.5.3
numpy==1.12.1
scikit-learn==0.18.1
tensorflow==1.0.1
h5py==2.6.0
numpy-1.13.1+mkl
我們用豆瓣園來加速安以上依賴裝如:
pip?install?-i?https://pypi.douban.com/simple?h5py==2.6.0
如果是win系統(tǒng),可能存在安裝失敗的可能,如果那個包安裝失敗,就到 http://www.lfd.uci.edu/~gohlk... ?找到win對應的版本下載到本地安裝,如:
pip?install?h5py-2.7.0-cp35-cp35m-win_amd64.whl
第二步,將者也的,驗證碼識別程序的zheye文件夾放到工程目錄里
第三步,爬蟲實現(xiàn)
start_requests()方法,起始url函數(shù),會替換start_urls
Request()方法,get方式請求網(wǎng)頁
url=字符串類型url
headers=字典類型瀏覽器代理
meta=字典類型的數(shù)據(jù),會傳遞給回調(diào)函數(shù)
callback=回調(diào)函數(shù)名稱
scrapy.FormRequest()post方式提交數(shù)據(jù)
url=字符串類型url
headers=字典類型瀏覽器代理
meta=字典類型的數(shù)據(jù),會傳遞給回調(diào)函數(shù)
callback=回調(diào)函數(shù)名稱
formdata=字典類型,要提交的數(shù)據(jù)字段
response.headers.getlist('Set-Cookie')?獲取響應Cookies
response.request.headers.getlist('Cookie')?獲取請求Cookies
#?-*-?coding:?utf-8?-*-
import?scrapy
from?scrapy.http?import?Request,FormRequest
import?re
class?PachSpider(scrapy.Spider):????????????????????????????#定義爬蟲類,必須繼承scrapy.Spider
????name?=?'pach'???????????????????????????????????????????#設置爬蟲名稱
????allowed_domains?=?['zhihu.com']????????????????????#爬取域名
????#?start_urls?=?['']?????????????????????????????????????#爬取網(wǎng)址,只適于不需要登錄的請求,因為沒法設置cookie等信息
????header?=?{'User-Agent':'Mozilla/5.0?(Windows?NT?10.0;?WOW64;?rv:54.0)?Gecko/20100101?Firefox/54.0'}??#設置瀏覽器用戶代理
????def?start_requests(self):????#起始url函數(shù),會替換start_urls
????????"""第一次請求一下登錄頁面,設置開啟cookie使其得到cookie,設置回調(diào)函數(shù)"""
????????return?[Request(
????????????url='https://www.zhihu.com/#signin',
????????????headers=self.header,
????????????meta={'cookiejar':1},???????#開啟Cookies記錄,將Cookies傳給回調(diào)函數(shù)
????????????callback=self.parse
????????)]
????def?parse(self,?response):
????????#?響應Cookies
????????Cookie1?=?response.headers.getlist('Set-Cookie')????????????????????????????#查看一下響應Cookie,也就是第一次訪問注冊頁面時后臺寫入瀏覽器的Cookie
????????print('后臺首次寫入的響應Cookies:',Cookie1)
????????#獲取xsrf密串
????????xsrf?=?response.xpath('//input[@name="_xsrf"]/@value').extract()[0]
????????print('獲取xsrf密串:'?+?xsrf)
????????#獲取驗證碼?????????
????????import?time?????????
????????t?=?str(int(time.time()*1000))?????????
????????captcha_url?=?'https://www.zhihu.com/captcha.gif?r={0}&type=login?=cn'.format(t)???#構(gòu)造驗證碼請求地址
????????yield?Request(url=captcha_url,?????????????????????????????????????????????????????????#請求驗證碼圖片
??????????????????????headers=self.header,
??????????????????????meta={'cookiejar':response.meta['cookiejar'],'xsrf':xsrf},???????????????#將Cookies和xsrf密串傳給回調(diào)函數(shù)
??????????????????????callback=self.post_tj
??????????????????????)
????def?post_tj(self,?response):
????????with?open('yzhm.jpg','wb')?as?f:????????#打開圖片句柄
????????????f.write(response.body)??????????????#將驗證碼圖片寫入本地
????????????f.close()???????????????????????????#關(guān)閉句柄
#---------------------------者也驗證碼識別-----------------------
????????from?zheye?import?zheye?????????????????#導入者也倒立文字驗證碼識別模塊對象
????????z?=?zheye()?????????????????????????????#實例化對象
????????positions?=?z.Recognize('yzhm.jpg')?????#將驗證碼本地路徑傳入Recognize方法識別,返回倒立圖片的坐標
????????#?print(positions)??????????????????????#默認倒立文字的y坐標在前,x坐標在后
????????#知乎網(wǎng)要求的倒立文字坐標是x軸在前,y軸在后,所以我們需要定義一個列表來改變默認的,倒立文字坐標位置
????????pos_arr?=?[]
????????if?len(positions)?==?2:
????????????if?positions[0][1]?>?positions[1][1]:??????????????????????#判斷列表里第一個元祖里的第二個元素如果大于,第二個元祖里的第二個元素
????????????????pos_arr.append([positions[1][1],positions[1][0]])
????????????????pos_arr.append([positions[0][1],?positions[0][0]])
????????????else:
????????????????pos_arr.append([positions[0][1],?positions[0][0]])
????????????????pos_arr.append([positions[1][1],?positions[1][0]])
????????else:
????????????pos_arr.append([positions[0][1],?positions[0][0]])
????????print('處理后的驗證碼坐標',pos_arr)
#?-------------者也驗證碼識別結(jié)束--------
????????if?len(pos_arr)?==?2:
????????????data?=?{????????????????????????????????????????????????????????????????????#?設置用戶登錄信息,對應抓包得到字段
????????????????'_xsrf':?response.meta['xsrf'],
????????????????'password':?'279819',
????????????????'captcha':?'{"img_size":[200,44],"input_points":[[%.2f,%f],[%.2f,%f]]}'?%(
????????????????????pos_arr[0][0]?/?2,?pos_arr[0][1]?/?2,?pos_arr[1][0]?/?2,?pos_arr[1][1]?/?2),??#因為驗證碼識別默認是400X88的尺寸所以要除以2
????????????????'captcha_type':?'cn',
????????????????'phone_num':?'15284816568'
????????????}
????????else:
????????????data?=?{????????????????????????????????????????????????????????????????????#?設置用戶登錄信息,對應抓包得到字段
????????????????'_xsrf':?response.meta['xsrf'],
????????????????'password':?'279819',
????????????????'captcha':?'{"img_size":[200,44],"input_points":[[%.2f,%f]]}'?%(
????????????????????pos_arr[0][0]?/?2,?pos_arr[0][1]?/?2),
????????????????'captcha_type':?'cn',
????????????????'phone_num':?'15284816568'
????????????}
????????print('登錄提交數(shù)據(jù)',data)
????????print('登錄中....!')
????????"""第二次用表單post請求,攜帶Cookie、瀏覽器代理、用戶登錄信息,進行登錄給Cookie授權(quán)"""
????????return?[scrapy.FormRequest(
????????????url='https://www.zhihu.com/login/phone_num',????????????????????????#真實post地址
????????????meta={'cookiejar':response.meta['cookiejar']},??????????????????????#接收第傳過來的Cookies
????????????headers=self.header,
????????????formdata=data,
????????????callback=self.next
????????)]
????def?next(self,response):
????????#?請求Cookie
????????Cookie2?=?response.request.headers.getlist('Cookie')
????????print('登錄時攜帶請求的Cookies:',Cookie2)
????????jieg?=?response.body.decode("utf-8")???#登錄后可以查看一下登錄響應信息
????????print('登錄響應結(jié)果:',jieg)
????????print('正在請需要登錄才可以訪問的頁面....!')
????????"""登錄后請求需要登錄才能查看的頁面,如個人中心,攜帶授權(quán)后的Cookie請求"""
????????yield?Request(
????????????url='https://www.zhihu.com/people/lin-gui-xiu-41/activities',
????????????headers=self.header,
????????????meta={'cookiejar':True},
????????????callback=self.next2
????????)
????def?next2(self,response):
????????#?請求Cookie
????????Cookie3?=?response.request.headers.getlist('Cookie')
????????print('查看需要登錄才可以訪問的頁面攜帶Cookies:',Cookie3)
????????leir?=?response.xpath('/html/head/title/text()').extract()??#得到個人中心頁面
????????print('最終內(nèi)容',leir)
????????#?print(response.body.decode("utf-8"))
【轉(zhuǎn)載自:http://www.lqkweb.com】
更多文章、技術(shù)交流、商務合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
