偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

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

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

[[351342]]

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

1 使用表單登陸

這種情況屬于post請(qǐng)求,即先向服務(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. # 要訪問(wèn)的目標(biāo)頁(yè)面 
  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. # 代理隧道驗(yàn)證信息 
  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訪問(wèn)都是用HTTP代理 
  54.  
  55. proxies = { 
  56.  
  57.     "http": proxyMeta, 
  58.  
  59.     "https": proxyMeta, 
  60.  
  61.  
  62.  
  63.  
  64. # 訪問(wèn)三次網(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ù)器會(huì)認(rèn)為你是一個(gè)已登陸的用戶,所以就會(huì)返回給你一個(gè)已登陸的內(nèi)容。因此,需要驗(yàn)證碼的情況可以使用帶驗(yàn)證碼登陸的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) # 已登陸,因?yàn)橹澳玫搅薘esponse Cookie! 
  6.   
  7. response3 = requests_session.get(url_results) # 已登陸,因?yàn)橹澳玫搅薘esponse Cookie! 

若存在驗(yàn)證碼,此時(shí)采用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) # 已登陸,因?yàn)橹澳玫搅薘esponse Cookie! 
  6.   
  7. response3 = requests_session.get(url_results) # 已登陸,因?yàn)橹澳玫搅薘esponse Cookie! 
  8.   

 【編輯推薦】

 

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

2021-05-10 06:48:11

Python騰訊招聘

2020-07-10 08:24:18

Python開(kāi)發(fā)工具

2011-01-10 14:41:26

2025-05-07 00:31:30

2011-05-03 15:59:00

黑盒打印機(jī)

2018-05-16 15:46:06

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

2021-09-30 18:27:38

數(shù)據(jù)倉(cāng)庫(kù)ETL

2018-05-22 15:30:30

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

2018-05-16 13:50:30

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

2018-05-14 16:34:08

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

2021-07-14 09:00:00

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

2018-05-14 15:27:06

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

2018-05-22 16:28:46

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

2011-02-22 13:46:27

微軟SQL.NET

2021-02-26 11:54:38

MyBatis 插件接口

2021-12-28 08:38:26

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

2017-05-18 12:45:35

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

2020-11-27 07:38:43

MongoDB

2011-04-21 10:32:44

MySQL雙機(jī)同步

2022-03-14 14:47:21

HarmonyOS操作系統(tǒng)鴻蒙
點(diǎn)贊
收藏

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