驗證碼
1. 模擬登錄的意義
- 爬取基於某些用戶的用戶信息, 獲取cookie
2. 雲打碼平台實現流程
1) 對攜帶驗證碼的頁面數據進行抓取
2) 可以將頁面數據中驗證碼進行解析,驗證碼圖片下載到本地
3) 以將驗證碼圖片提交給三方平台進行識別,返回驗證碼圖片上的數據值
4) 基於登錄按鈕發起post請求(處理參數)
- 雲打碼平台:
- 1) 在官網中進行注冊(普通用戶和開發者用戶)
- 2) 登錄開發者用戶:
- 1) 實例代碼的下載(開發文檔-> 調用實例及最新的DLL-> PythonHTTP實例下載)
- 2) 創建一個軟件: 我的軟件-> 添加新的軟件
- 3) 使用示例代碼中的源碼文件中的代碼進行修改,讓其識別驗證碼圖片中的數據值
代理 IP
1. 什么是代理?
代理就是第三方代替本體處理相關事務。
2. 為什么需要使用代理
一些網站會有相應的反爬蟲措施,例如很多網站會檢測某一段時間某個IP的訪問次數,如果訪問頻率太快以至於看起來不像正常訪客,它可能就會會禁止這個IP的訪問。所以我們需要設置一些代理IP,每隔一段時間換一個代理IP,就算IP被禁止,依然可以換個IP繼續爬取。
1. 代理服務器
- 快代理
- 西祠代理
- http://www.goubanjia.com/
2. 匿名度
- 透明: 對方服務器知道你使用了代理ip, 也知道你的真實ip
- 匿名: 知道你使用了代理ip, 不知道你的真實ip
- 高匿: 兩者都不知道
3. 類型
- http: 只可以發送http請求
- https: 只可以發送https請求
cookie的應用和處理
1. cookie 概念
當用戶通過瀏覽器首次訪問一個域名時,訪問的web服務器會給客戶端發送數據,以保持web服務器與客戶端之間的狀態保持,這些數據就是cookie。
2. 處理 cookie 的方式
- 1) 手動處理: 在headers 中添加 cookie 鍵值對
- 2) 自動處理: 會話對象Session, 該對象可以像requests模塊一樣進行網絡請求的發送(get, post), session進行的請求發送可以自動攜帶和處理cookie.(自動保存cookie)
圖片懶加載
懶加載概念:
圖片懶加載是一種網頁優化技術。圖片作為一種網絡資源,在被請求時也與普通靜態資源一樣,將占用網絡資源,而一次性將整個頁面的所有圖片加載完,將大大增加頁面的首屏加載時間。為了解決這種問題,通過前后端配合,使圖片僅在瀏覽器當前視窗內出現時才加載該圖片,達到減少首屏圖片請求數的技術就被稱為“圖片懶加載”。
網站一般如何實現圖片懶加載技術呢?
在網頁源碼中,在img標簽中首先會使用一個“偽屬性”(通常使用src2,original......)去存放真正的圖片鏈接而並非是直接存放在src屬性中。當圖片出現到頁面的可視化區域中,會動態將偽屬性替換成src屬性,完成圖片的加載。
實例
人人網進行登陸校驗
import requests
from lxml import etree
from urllib import request
from CodeClass import YDMHttp
def get_code_text(file_path, code_type):
username = 'yanzhi'
password = 'abcd1234'
appid = 7597
appkey = 'a0e4bfd76d74cbf4dc5758fc4e760f08'
filename = file_path
codetype = code_type
timeout = 30
if (username == 'username'):
print('請設置好相關參數再測試')
else:
# 初始化
yundama = YDMHttp(username, password, appid, appkey)
uid = yundama.login()
print('uid: %s' % uid)
balance = yundama.balance()
print('balance: %s' % balance)
cid, result = yundama.decode(filename, codetype, timeout)
print('cid: %s, result: %s' % (cid, result))
return result
url = 'http://www.renren.com/'
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36"
}
page_text = requests.get(url=url, headers=headers).text
# 解析出驗證碼圖片地址
tree = etree.HTML(page_text)
code_img_url = tree.xpath('//*[@id="verifyPic_login"]/@src')[0]
request.urlretrieve(url=code_img_url, filename='./code.jpg')
# 使用打碼平台, 識別驗證碼
code_text = get_code_text('./code.jpg', 2004)
# 模擬登錄
login_url = 'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=2019401948420'
data = {
"email": "18247804718",
"icode": code_text,
"origURL": "http://www.renren.com/home",
"password": "4205897ac7aa5e9bc57eba70775e02b34796",
"rkey": "ad1eb852f18356e260bb63c33c67ca5a",
"f": "http%3A%2F%2Fwww.renren.com%2F970680347",
}
response = requests.post(url=login_url, headers=headers, data=data)
print(response.status_code)
page_texts = response.text
print(page_texts)
利用不同的代理ip進行訪問
import requests
import random
header_list = [
# 火狐
{
"user-agent": "Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"},
# 谷歌
{
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11"}
]
# 不同的代理IP
proxy_list = [
{"http": "112.115.57.20:3128"},
{'http': '121.41.171.223:3128'}
]
# 隨機獲取UA和代理IP
header = random.choice(header_list)
proxy = random.choice(proxy_list)
url = 'http://www.baidu.com/s?ie=UTF-8&wd=ip'
# 參數3:設置代理
response = requests.get(url=url,headers=header,proxies=proxy)
with open('daili.html', 'wb') as fp:
fp.write(response.content)
# 切換成原來的IP
requests.get(url, proxies={"http": ""})
基於cookie的案例分析: 爬取www.xueqiu.com
import requests
import json
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36"
}
url = 'https://xueqiu.com/'
# 自動獲取cookie, cookie會自動存儲到session中
session = requests.Session()
c_url = 'https://xueqiu.com/v4/statuses/public_timeline_by_category.json?since_id=-1&max_id=-1&count=10&category=-1'
# 攜帶cookie進行請求發送
dic_json = session.get(url=c_url, headers=headers).json()
for dic in dic_json['list']:
data = json.loads(dic["data"])
detail_url = "https://xueqiu.com" + data["target"]
print(detail_url)