自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python爬蟲:手把手教你采集登陸后才能看到數(shù)據(jù)

開發(fā) 前端
爬蟲在采集網(wǎng)站的過程中,部分?jǐn)?shù)據(jù)價值較高的網(wǎng)站,會限制訪客的訪問行為。這種時候建議通過登錄的方式,獲取目標(biāo)網(wǎng)站的cookie,然后再使用cookie配合代理IP進(jìn)行數(shù)據(jù)采集分析。

[[351342]]

 爬蟲在采集網(wǎng)站的過程中,部分?jǐn)?shù)據(jù)價值較高的網(wǎng)站,會限制訪客的訪問行為。這種時候建議通過登錄的方式,獲取目標(biāo)網(wǎng)站的cookie,然后再使用cookie配合代理IP進(jìn)行數(shù)據(jù)采集分析。

1 使用表單登陸

這種情況屬于post請求,即先向服務(wù)器發(fā)送表單數(shù)據(jù),服務(wù)器再將返回的cookie存入本地。

  1. #! -*- encoding:utf-8 -*- 
  2.  
  3. import requests 
  4.  
  5. import random 
  6.  
  7. import requests.adapters 
  8.  
  9.  
  10.  
  11. # 要訪問的目標(biāo)頁面 
  12.  
  13. targetUrlList = [ 
  14.  
  15.     "https://httpbin.org/ip"
  16.  
  17.     "https://httpbin.org/headers"
  18.  
  19.     "https://httpbin.org/user-agent"
  20.  
  21.  
  22.  
  23.  
  24. # 代理服務(wù)器(產(chǎn)品官網(wǎng) www.16yun.cn) 
  25.  
  26. proxyHost = "t.16yun.cn" 
  27.  
  28. proxyPort = "31111" 
  29.  
  30.  
  31.  
  32. # 代理隧道驗證信息 
  33.  
  34. proxyUser = "username" 
  35.  
  36. proxyPass = "password" 
  37.  
  38.  
  39.  
  40. proxyMeta = "http://%(user)s:%(pass)s@%(host)s:%(port)s" % { 
  41.  
  42.     "host": proxyHost, 
  43.  
  44.     "port": proxyPort, 
  45.  
  46.     "user": proxyUser, 
  47.  
  48.     "pass": proxyPass, 
  49.  
  50.  
  51.  
  52.  
  53. # 設(shè)置 http和https訪問都是用HTTP代理 
  54.  
  55. proxies = { 
  56.  
  57.     "http": proxyMeta, 
  58.  
  59.     "https": proxyMeta, 
  60.  
  61.  
  62.  
  63.  
  64. # 訪問三次網(wǎng)站,使用相同的Session(keep-alive),均能夠保持相同的外網(wǎng)IP 
  65.  
  66. s = requests.session() 
  67.  
  68.  
  69.  
  70. # 設(shè)置cookie 
  71.  
  72. cookie_dict = {"JSESSION":"123456789"
  73.  
  74. cookies = requests.utils.cookiejar_from_dict(cookie_dict, cookiejar=None, overwrite=True
  75.  
  76. s.cookies = cookies 
  77.  
  78.  
  79.  
  80. for i in range(3): 
  81.  
  82.     for url in targetUrlList: 
  83.  
  84.         r = s.get(url, proxies=proxies) 
  85.  
  86.         print r.text 

2 使用cookie登陸 

使用cookie登陸,服務(wù)器會認(rèn)為你是一個已登陸的用戶,所以就會返回給你一個已登陸的內(nèi)容。因此,需要驗證碼的情況可以使用帶驗證碼登陸的cookie解決。

  1. response_captcha = requests_session.get(url=url_login, cookies=cookies) 
  2.   
  3. response1 = requests.get(url_login) # 未登陸 
  4.   
  5. response2 = requests_session.get(url_login) # 已登陸,因為之前拿到了Response Cookie! 
  6.   
  7. response3 = requests_session.get(url_results) # 已登陸,因為之前拿到了Response Cookie! 

若存在驗證碼,此時采用response = requests_session.post(url=url_login, data=data)是不行的,做法應(yīng)該如下:

  1. response_captcha = requests_session.get(url=url_login, cookies=cookies) 
  2.   
  3. response1 = requests.get(url_login) # 未登陸 
  4.   
  5. response2 = requests_session.get(url_login) # 已登陸,因為之前拿到了Response Cookie! 
  6.   
  7. response3 = requests_session.get(url_results) # 已登陸,因為之前拿到了Response Cookie! 
  8.   

 【編輯推薦】

 

責(zé)任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2021-05-10 06:48:11

Python騰訊招聘

2020-07-10 08:24:18

Python開發(fā)工具

2018-05-16 15:46:06

Python網(wǎng)絡(luò)爬蟲PhantomJS

2018-05-16 13:50:30

Python網(wǎng)絡(luò)爬蟲Scrapy

2018-05-22 15:30:30

Python網(wǎng)絡(luò)爬蟲分布式爬蟲

2018-05-14 16:34:08

Python網(wǎng)絡(luò)爬蟲Scrapy

2011-01-10 14:41:26

2011-05-03 15:59:00

黑盒打印機

2021-07-14 09:00:00

JavaFX開發(fā)應(yīng)用

2021-09-30 18:27:38

數(shù)據(jù)倉庫ETL

2018-05-14 15:27:06

Python網(wǎng)絡(luò)爬蟲爬蟲架構(gòu)

2018-05-22 16:28:46

Python網(wǎng)絡(luò)爬蟲URL去重

2020-11-27 07:38:43

MongoDB

2017-05-18 12:45:35

數(shù)據(jù)分析數(shù)據(jù)理解數(shù)據(jù)

2021-02-26 11:54:38

MyBatis 插件接口

2011-02-22 13:46:27

微軟SQL.NET

2021-12-28 08:38:26

Linux 中斷喚醒系統(tǒng)Linux 系統(tǒng)

2011-04-21 10:32:44

MySQL雙機同步

2022-07-27 08:16:22

搜索引擎Lucene

2022-01-08 20:04:20

攔截系統(tǒng)調(diào)用
點贊
收藏

51CTO技術(shù)棧公眾號