最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python爬蟲豆瓣網(wǎng)的模擬登錄實現(xiàn)

 更新時間:2019年08月21日 10:48:49   作者:Python很簡單  
這篇文章主要介紹了python爬蟲豆瓣網(wǎng)的模擬登錄實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

思路

一、想要實現(xiàn)登錄豆瓣關鍵點

分析真實post地址 ----尋找它的formdata,如下圖,按瀏覽器的F12可以找到。

實戰(zhàn)操作

  • 實現(xiàn):模擬登錄豆瓣,驗證碼處理,登錄到個人主頁就算是success
  • 數(shù)據(jù):沒有抓取數(shù)據(jù),此實戰(zhàn)主要是模擬登錄和處理驗證碼的學習。要是有需求要抓取數(shù)據(jù),編寫相關的抓取規(guī)則即可抓取內容。

登錄成功展示如圖:

spiders文件夾中DouBan.py主要代碼如下:

# -*- coding: utf-8 -*-
import scrapy,urllib,re
from scrapy.http import Request,FormRequest
import ruokuai
'''
遇到不懂的問題?Python學習交流群:821460695滿足你的需求,資料都已經上傳群文件,可以自行下載!
'''
class DoubanSpider(scrapy.Spider):
 name = "DouBan"
 allowed_domains = ["douban.com"]
 #start_urls = ['http://douban.com/']
 header={"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"} #供登錄模擬使用
 def start_requests(self):
  url='https://www.douban.com/accounts/login'
  return [Request(url=url,meta={"cookiejar":1},callback=self.parse)]#可以傳遞一個標示符來使用多個。如meta={'cookiejar': 1}這句,后面那個1就是標示符

 def parse(self, response):
  captcha=response.xpath('//*[@id="captcha_image"]/@src').extract() #獲取驗證碼圖片的鏈接
  print captcha
  if len(captcha)>0:
   '''此時有驗證碼'''
   #人工輸入驗證碼
   #urllib.urlretrieve(captcha[0],filename="C:/Users/pujinxiao/Desktop/learn/douban20170405/douban/douban/spiders/captcha.png")
   #captcha_value=raw_input('查看captcha.png,有驗證碼請輸入:')

   #用快若打碼平臺處理驗證碼--------驗證碼是任意長度字母,成功率較低
   captcha_value=ruokuai.get_captcha(captcha[0])
   reg=r'<Result>(.*?)</Result>'
   reg=re.compile(reg)
   captcha_value=re.findall(reg,captcha_value)[0]
   print '驗證碼為:',captcha_value

   data={
    "form_email": "weisuen007@163.com",
    "form_password": "weijc7789",
    "captcha-solution": captcha_value,
    #"redir": "https://www.douban.com/people/151968962/",  #設置需要轉向的網(wǎng)址,由于我們需要爬取個人中心頁,所以轉向個人中心頁
   }
  else:
   '''此時沒有驗證碼'''
   print '無驗證碼'
   data={
    "form_email": "weisuen007@163.com",
    "form_password": "weijc7789",
    #"redir": "https://www.douban.com/people/151968962/",
   }
  print '正在登陸中......'
  ####FormRequest.from_response()進行登陸
  return [
   FormRequest.from_response(
    response,
    meta={"cookiejar":response.meta["cookiejar"]},
    headers=self.header,
    formdata=data,
    callback=self.get_content,
   )
  ]
 def get_content(self,response):
  title=response.xpath('//title/text()').extract()[0]
  if u'登錄豆瓣' in title:
   print '登錄失敗,請重試!'
  else:
   print '登錄成功'
   '''
   可以繼續(xù)后續(xù)的爬取工作
   '''

ruokaui.py代碼如下:

我所用的是若塊打碼平臺,選擇url識別驗證碼,直接給打碼平臺驗證碼圖片的鏈接地址,傳回驗證碼的值。

# -*- coding: utf-8 -*-
import sys, hashlib, os, random, urllib, urllib2
from datetime import *
'''
遇到不懂的問題?Python學習交流群:821460695滿足你的需求,資料都已經上傳群文件,可以自行下載!
'''
class APIClient(object):
 def http_request(self, url, paramDict):
  post_content = ''
  for key in paramDict:
   post_content = post_content + '%s=%s&'%(key,paramDict[key])
  post_content = post_content[0:-1]
  #print post_content
  req = urllib2.Request(url, data=post_content)
  req.add_header('Content-Type', 'application/x-www-form-urlencoded')
  opener = urllib2.build_opener(urllib2.HTTPCookieProcessor()) 
  response = opener.open(req, post_content) 
  return response.read()

 def http_upload_image(self, url, paramKeys, paramDict, filebytes):
  timestr = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  boundary = '------------' + hashlib.md5(timestr).hexdigest().lower()
  boundarystr = '\r\n--%s\r\n'%(boundary)
  
  bs = b''
  for key in paramKeys:
   bs = bs + boundarystr.encode('ascii')
   param = "Content-Disposition: form-data; name=\"%s\"\r\n\r\n%s"%(key, paramDict[key])
   #print param
   bs = bs + param.encode('utf8')
  bs = bs + boundarystr.encode('ascii')
  
  header = 'Content-Disposition: form-data; name=\"image\"; filename=\"%s\"\r\nContent-Type: image/gif\r\n\r\n'%('sample')
  bs = bs + header.encode('utf8')
  
  bs = bs + filebytes
  tailer = '\r\n--%s--\r\n'%(boundary)
  bs = bs + tailer.encode('ascii')
  
  import requests
  headers = {'Content-Type':'multipart/form-data; boundary=%s'%boundary,
     'Connection':'Keep-Alive',
     'Expect':'100-continue',
     }
  response = requests.post(url, params='', data=bs, headers=headers)
  return response.text

def arguments_to_dict(args):
 argDict = {}
 if args is None:
  return argDict
 
 count = len(args)
 if count <= 1:
  print 'exit:need arguments.'
  return argDict
 
 for i in [1,count-1]:
  pair = args[i].split('=')
  if len(pair) < 2:
   continue
  else:
   argDict[pair[0]] = pair[1]

 return argDict

def get_captcha(image_url):
 client = APIClient()
 while 1:
  paramDict = {}
  result = ''
  act = raw_input('請輸入打碼方式url:')
  if cmp(act, 'info') == 0: 
   paramDict['username'] = raw_input('username:')
   paramDict['password'] = raw_input('password:')
   result = client.http_request('http://api.ruokuai.com/info.xml', paramDict)
  elif cmp(act, 'register') == 0:
   paramDict['username'] = raw_input('username:')
   paramDict['password'] = raw_input('password:')
   paramDict['email'] = raw_input('email:')
   result = client.http_request('http://api.ruokuai.com/register.xml', paramDict)
  elif cmp(act, 'recharge') == 0:
   paramDict['username'] = raw_input('username:')
   paramDict['id'] = raw_input('id:')
   paramDict['password'] = raw_input('password:')
   result = client.http_request('http://api.ruokuai.com/recharge.xml', paramDict)
  elif cmp(act, 'url') == 0:
   paramDict['username'] = '********'
   paramDict['password'] = '********'
   paramDict['typeid'] = '2000'
   paramDict['timeout'] = '90'
   paramDict['softid'] = '76693'
   paramDict['softkey'] = 'ec2b5b2a576840619bc885a47a025ef6'
   paramDict['imageurl'] = image_url
   result = client.http_request('http://api.ruokuai.com/create.xml', paramDict)
  elif cmp(act, 'report') == 0:
   paramDict['username'] = raw_input('username:')
   paramDict['password'] = raw_input('password:')
   paramDict['id'] = raw_input('id:')
   result = client.http_request('http://api.ruokuai.com/create.xml', paramDict)
  elif cmp(act, 'upload') == 0:
   paramDict['username'] = '********'
   paramDict['password'] = '********'
   paramDict['typeid'] = '2000'
   paramDict['timeout'] = '90'
   paramDict['softid'] = '76693'
   paramDict['softkey'] = 'ec2b5b2a576840619bc885a47a025ef6'
   paramKeys = ['username',
     'password',
     'typeid',
     'timeout',
     'softid',
     'softkey'
    ]

   from PIL import Image
   imagePath = raw_input('Image Path:')
   img = Image.open(imagePath)
   if img is None:
    print 'get file error!'
    continue
   img.save("upload.gif", format="gif")
   filebytes = open("upload.gif", "rb").read()
   result = client.http_upload_image("http://api.ruokuai.com/create.xml", paramKeys, paramDict, filebytes)
  
  elif cmp(act, 'help') == 0:
   print 'info'
   print 'register'
   print 'recharge'
   print 'url'
   print 'report'
   print 'upload'
   print 'help'
   print 'exit'
  elif cmp(act, 'exit') == 0:
   break
  
  return result

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 零基礎寫python爬蟲之爬蟲編寫全記錄

    零基礎寫python爬蟲之爬蟲編寫全記錄

    前面九篇文章從基礎到編寫都做了詳細的介紹了,第十篇么講究個十全十美,那么我們就來詳細記錄一下一個爬蟲程序如何一步步編寫出來的,各位看官可要看仔細了
    2014-11-11
  • Python中tqdm的使用和例子

    Python中tqdm的使用和例子

    Tqdm是一個快速,可擴展的Python進度條,可以在 Python 長循環(huán)中添加一個進度提示信息,用戶只需要封裝任意的迭代器tqdm(iterator),下面這篇文章主要給大家介紹了關于Python中tqdm的使用和例子的相關資料,需要的朋友可以參考下
    2022-09-09
  • python有證書的加密解密實現(xiàn)方法

    python有證書的加密解密實現(xiàn)方法

    這篇文章主要介紹了python有證書的加密解密實現(xiàn)方法,采用了M2Crypto組件進行相關的加密解密操作,包含了詳細的完整實現(xiàn)過程,需要的朋友可以參考下
    2014-11-11
  • 利用Python編寫一個蹭WiFi的軟件

    利用Python編寫一個蹭WiFi的軟件

    這篇文章主要為大家詳細介紹了如何利用Python編寫一個簡易的蹭WiFi的軟件,文中的示例代碼講解詳細,感興趣的小伙伴可以學習一下
    2023-06-06
  • Pytorch:Conv2d卷積前后尺寸詳解

    Pytorch:Conv2d卷積前后尺寸詳解

    這篇文章主要介紹了Pytorch:Conv2d卷積前后尺寸,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 跟老齊學Python之啰嗦的除法

    跟老齊學Python之啰嗦的除法

    python 除法運算 比較奇怪,和別的程序語言不大一樣。從Python2.2開始,除法運算符除了/之外,又引入了一個除法運算符://,后一種運算符只用于進行整除法。對于除法運算符/,默認時的行為跟Python2.2之前的一樣,它視操作數(shù)而定,既可以進行整除,也可以進行真除法。
    2014-09-09
  • Pytorch測試神經網(wǎng)絡時出現(xiàn) RuntimeError:的解決方案

    Pytorch測試神經網(wǎng)絡時出現(xiàn) RuntimeError:的解決方案

    這篇文章主要介紹了Pytorch測試神經網(wǎng)絡時出現(xiàn) RuntimeError:的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • Python實現(xiàn)井字棋小游戲

    Python實現(xiàn)井字棋小游戲

    這篇文章主要為大家詳細介紹了Python實現(xiàn)井字棋小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • 使用Python Typing模塊提升代碼可讀性和健壯性實例探索

    使用Python Typing模塊提升代碼可讀性和健壯性實例探索

    這篇文章主要為大家介紹了使用Python Typing模塊提升代碼可讀性和健壯性實例探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01
  • 利用Python實現(xiàn)數(shù)值積分的方法

    利用Python實現(xiàn)數(shù)值積分的方法

    這篇文章主要介紹了利用Python實現(xiàn)數(shù)值積分。本文主要用于對比使用Python來實現(xiàn)數(shù)學中積分的幾種計算方式,并和真值進行對比,加深大家對積分運算實現(xiàn)方式的理解
    2022-02-02

最新評論

固镇县| 华池县| 平谷区| 太谷县| 吉木乃县| 宣武区| 托克逊县| 肥城市| 龙泉市| 射阳县| 府谷县| 邯郸市| 瓦房店市| 城固县| 板桥市| 比如县| 建湖县| 芜湖县| 东丰县| 中方县| 井陉县| 石渠县| 开原市| 彭阳县| 兴山县| 济源市| 谷城县| 浦江县| 沙坪坝区| 四川省| 平和县| 德兴市| 开封县| 桂林市| 囊谦县| 集贤县| 宁城县| 天峻县| 克拉玛依市| 邵阳县| 吴旗县|