wordpress 重新生成,手机优化网站建设,WordPress+百度+主动,曲靖做网站的公司文章目录 一、基于 Cookie 的模拟登录二、基于 JWT 模拟登入三、账号池四、基于 Cookie 模拟登录爬取实战五、基于JWT 的模拟登录爬取实战六、构建账号池 很多情况下#xff0c;网站的一些数据需要登录才能查看#xff0c;如果需要爬取这部分的数据#xff0c;就需要实现模拟… 文章目录 一、基于 Cookie 的模拟登录二、基于 JWT 模拟登入三、账号池四、基于 Cookie 模拟登录爬取实战五、基于JWT 的模拟登录爬取实战六、构建账号池 很多情况下网站的一些数据需要登录才能查看如果需要爬取这部分的数据就需要实现模拟登入的一些机制模拟登录现在主要分为两种方式一种是基于 Session 和 Cookie 的模拟登入一种是基于 JWTJson Web Token的模拟登录。
对于第一种模式打开网页后模拟登录服务器会返回带有 Set-Cookie 字段的响应头客户端会生成对应的 Cookie其中保存着与 SessionID 相关的信息之后发送给服务器的请求都会携带这个生成的 Cookie。服务器接收到请求后会根据 Cookie 中保存的 SessionID 找到对应的 Session同时效验 Cookie 里的相关信息如果当前 Session 是有效的并且效验成功服务器就判断当前用户已经登录返回请求的页面信息所以这种模式的核心是获取客户端登录后生成的 Cookie
对于第二种模式是因为现在有很多的网站采取的开发模式是前后端分离模式所以使用 JWT 进行登录效验越来越普遍在请求数据时服务器会效验请求中携带的 JWT 是否有效如果有效就返回正常的数据所以这种模式其实就是获取 JWT
一、基于 Cookie 的模拟登录
如果要使用爬虫实现基于 Session 和 Cookie 的模拟登录最为主要的是要维护好 Cookie 的信息因为爬虫相当于客户端的浏览器
如果在浏览器中登录了自己的账号可以直接把网页中的 Cookie 复制给爬虫就相当于手动在浏览器中登录如果让爬虫完全自动化操作可以直接使用爬虫模拟登录过程这个过程基本上就是一个 POST 请求用爬虫把用户名密码等信息提交给服务器服务器会返回一个 Set-Cookie 字段我们只需要将该字段保存下来然后提交给爬虫请求就好如果 POST 请求难以构造我们可以使用自动化工具来模拟登录例如使用 SeleniumPlaywright 来发送请求然后获取 Cookie 进行发送请求
二、基于 JWT 模拟登入
JWT 的字符串就是用户访问的凭证所以模拟登录只需要做到以下几步
模拟登录操作例如拿着用户名和密码信息请求登录接口获取服务器返回的结果这个结果中通常包含 JWT 信息将其保存下来即可之后发送给服务器的请求均携带 JWT在 JWT 不过期的情况下通常能正常访问和执行操作携带方式多种多样因网站而异如果 JWT 过期了可能需要再次做第一步重新获取 JWT
三、账号池
如果爬虫要求爬取的数据量比较大或者爬取速度比较快网站又有单账号并发限制或者访问状态检测等反爬虫手段我们的账号可能就无法访问网站或者面临封号的风险
这时我们建立一个账号池进行分流用多个账号随机访问网站或爬取数据这样能大幅提高爬虫的并发量降低被封号的风险例如准备 100 个账号将这 100 个账号都模拟登录并保存对应的 Cookie 和 JWT每次都随机抽取一个来访问账号多所以每个账号被选取的概率就小也就避免了单账号并发量过大的问题从而降低封号风险
四、基于 Cookie 模拟登录爬取实战
目标网址Scrape | Movie 账号admin 密码admin
这里由于登入请求构造并没有涉及到加密过程因此我们可以直接构造 requests 请求来执行请求仔细分析后可以发现登入请求返回的状态码是 302同时登入完毕后页面自动发生了跳转因此在使用 requests 够着 post 请求的时候需要将 allow_redirects 参数设置为 False
import requests
import parselheaders {Accept: text/html,application/xhtmlxml,application/xml;q0.9,image/avif,image/webp,image/apng,*/*;q0.8,application/signed-exchange;vb3;q0.7,Accept-Language: zh-CN,zh;q0.9,en;q0.8,en-GB;q0.7,en-US;q0.6,Cache-Control: max-age0,Connection: keep-alive,Content-Type: application/x-www-form-urlencoded,Origin: https://login2.scrape.center,Referer: https://login2.scrape.center/login,Sec-Fetch-Dest: document,Sec-Fetch-Mode: navigate,Sec-Fetch-Site: same-origin,Sec-Fetch-User: ?1,Upgrade-Insecure-Requests: 1,User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0,sec-ch-ua: Not/A)Brand;v8, Chromium;v126, Microsoft Edge;v126,sec-ch-ua-mobile: ?0,sec-ch-ua-platform: Windows,
}data {username: admin,password: admin,
}response requests.post(https://login2.scrape.center/login, headersheaders, datadata, allow_redirectsFalse)# 得到 cookies
cookies response.cookies.get_dict()# 将获取到的 cookie 放入 requests 的 get 请求中
response requests.get(https://login2.scrape.center/, cookiescookies, headersheaders)# 解析网页数据
selector parsel.Selector(response.text)
names selector.xpath(//*[idindex]/div[1]/div[1]/div/div/div/div[2]/a/h2/text()).getall()# 打印名字
print(names)# [霸王别姬 - Farewell My Concubine,
# 这个杀手不太冷 - Léon,
# 肖申克的救赎 - The Shawshank Redemption,
# 泰坦尼克号 - Titanic,
# 罗马假日 - Roman Holiday,
# 唐伯虎点秋香 - Flirting Scholar,
# 乱世佳人 - Gone with the Wind,
# 喜剧之王 - The King of Comedy,
# 楚门的世界 - The Truman Show,
# 狮子王 - The Lion King]在这里我们首先获得了 cookies然后又手动要将 cookies 放入到后续的请求之中这里我们可以构建 Session 请求来自动化添加 cookies如下
import requests
import parselheaders {Accept: text/html,application/xhtmlxml,application/xml;q0.9,image/avif,image/webp,image/apng,*/*;q0.8,application/signed-exchange;vb3;q0.7,Accept-Language: zh-CN,zh;q0.9,en;q0.8,en-GB;q0.7,en-US;q0.6,Cache-Control: max-age0,Connection: keep-alive,Content-Type: application/x-www-form-urlencoded,Origin: https://login2.scrape.center,Referer: https://login2.scrape.center/login,Sec-Fetch-Dest: document,Sec-Fetch-Mode: navigate,Sec-Fetch-Site: same-origin,Sec-Fetch-User: ?1,Upgrade-Insecure-Requests: 1,User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0,sec-ch-ua: Not/A)Brand;v8, Chromium;v126, Microsoft Edge;v126,sec-ch-ua-mobile: ?0,sec-ch-ua-platform: Windows,
}data {username: admin,password: admin,
}# 构建 session
session requests.Session()# 发送登入请求
response session.post(https://login2.scrape.center/login, headersheaders, datadata, allow_redirectsFalse)# 无需手动配置 cookies发送页面请求
response session.get(https://login2.scrape.center/, headersheaders)# 解析网页数据
selector parsel.Selector(response.text)
names selector.xpath(//*[idindex]/div[1]/div[1]/div/div/div/div[2]/a/h2/text()).getall()# 打印名字
print(names)# [霸王别姬 - Farewell My Concubine,
# 这个杀手不太冷 - Léon,
# 肖申克的救赎 - The Shawshank Redemption,
# 泰坦尼克号 - Titanic,
# 罗马假日 - Roman Holiday,
# 唐伯虎点秋香 - Flirting Scholar,
# 乱世佳人 - Gone with the Wind,
# 喜剧之王 - The King of Comedy,
# 楚门的世界 - The Truman Show,
# 狮子王 - The Lion King]这里是对于请求未涉及到加密或者带有验证码的网站如果涉及到加密但是又不会解密我们可以使用自动化工具来获取 Cookie这里以 Playwright 为例子
import requests
import parsel
from playwright.sync_api import sync_playwrightdef get_cookies():with sync_playwright() as playwright:cookies {}browser playwright.chromium.launch(headlessTrue)context browser.new_context()page context.new_page()page.goto(https://login2.scrape.center/login)page.locator(input[nameusername]).fill(admin)page.locator(input[namepassword]).fill(admin)page.locator(input[typesubmit]).click()cookieList context.cookies()context.close()browser.close()for cookie in cookieList:cookies[cookie[name]] cookie[value]return cookiesheaders {Accept: text/html,application/xhtmlxml,application/xml;q0.9,image/avif,image/webp,image/apng,*/*;q0.8,application/signed-exchange;vb3;q0.7,Accept-Language: zh-CN,zh;q0.9,en;q0.8,en-GB;q0.7,en-US;q0.6,Cache-Control: max-age0,Connection: keep-alive,Content-Type: application/x-www-form-urlencoded,Origin: https://login2.scrape.center,Referer: https://login2.scrape.center/login,Sec-Fetch-Dest: document,Sec-Fetch-Mode: navigate,Sec-Fetch-Site: same-origin,Sec-Fetch-User: ?1,Upgrade-Insecure-Requests: 1,User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0,sec-ch-ua: Not/A)Brand;v8, Chromium;v126, Microsoft Edge;v126,sec-ch-ua-mobile: ?0,sec-ch-ua-platform: Windows,
}cookies get_cookies()
response requests.get(https://login2.scrape.center/, cookiescookies, headersheaders)# 解析网页数据
selector parsel.Selector(response.text)
names selector.xpath(//*[idindex]/div[1]/div[1]/div/div/div/div[2]/a/h2/text()).getall()# 打印名字
print(names)# [霸王别姬 - Farewell My Concubine,
# 这个杀手不太冷 - Léon,
# 肖申克的救赎 - The Shawshank Redemption,
# 泰坦尼克号 - Titanic,
# 罗马假日 - Roman Holiday,
# 唐伯虎点秋香 - Flirting Scholar,
# 乱世佳人 - Gone with the Wind,
# 喜剧之王 - The King of Comedy,
# 楚门的世界 - The Truman Show,
# 狮子王 - The Lion King]五、基于JWT 的模拟登录爬取实战
目标网址Scrape | Movie 账号admin 密码admin
import requestsheaders {Accept: application/json, text/plain, */*,Accept-Language: zh-CN,zh;q0.9,en;q0.8,en-GB;q0.7,en-US;q0.6,Connection: keep-alive,Content-Type: application/json;charsetUTF-8,Origin: https://login3.scrape.center,Sec-Fetch-Dest: empty,Sec-Fetch-Mode: cors,Sec-Fetch-Site: same-origin,User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0,sec-ch-ua: Not/A)Brand;v8, Chromium;v126, Microsoft Edge;v126,sec-ch-ua-mobile: ?0,sec-ch-ua-platform: Windows,
}json_data {username: admin,password: admin,
}# 这里使用 json 而不是使用 data 传参是因为 headers 中的 Content-Type 接受的是 application/json 数据
response requests.post(https://login3.scrape.center/api/login, headersheaders, jsonjson_data
)# 获取 token 构建 headers 中的 Authorization
token response.json()[token]
Authorization jwt token
headers {Accept: application/json, text/plain, */*,Accept-Language: zh-CN,zh;q0.9,en;q0.8,en-GB;q0.7,en-US;q0.6,Authorization: Authorization,Connection: keep-alive,Sec-Fetch-Dest: empty,Sec-Fetch-Mode: cors,Sec-Fetch-Site: same-origin,User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0,sec-ch-ua: Not/A)Brand;v8, Chromium;v126, Microsoft Edge;v126,sec-ch-ua-mobile: ?0,sec-ch-ua-platform: Windows,
}params {limit: 18,offset: 0,
}# 直接使用 get 请求获取数据
response requests.get(https://login3.scrape.center/api/book, paramsparams, headersheaders
)print(response.json())# {
# count: 9200,
# results: [
# {
# id: 34473697,
# name: R数据科学实战工具详解与案例分析,
# authors: [刘健, 邬书豪],
# cover: None,
# }
# ],
# }在这里由于使用的是 JWT响应头中并不会返回一个 Set-Cookies 参数因此使用 Session 来完成 JWT 是没有效果的只能单个请求进行构建
六、构建账号池