吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4472|回复: 7
收起左侧

[Python 转载] python爬虫遇到【安全检查! | 百度云加速】的解决方案

[复制链接]
漁滒 发表于 2020-5-25 22:26
本帖最后由 aiai 于 2020-5-25 22:31 编辑

首先假设我们还不知道网站有百度云加速检查,先直接获取。网址因某些原因屏蔽,但是不影响整体思路
[Python] 纯文本查看 复制代码
    shareurl = 'https://************/**************************'
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
    }
    response = httpx.get(url=shareurl, headers=headers)
    print(response.text)

查看打印的结果,可以看到有【安全检查! | 百度云加速】
[JavaScript] 纯文本查看 复制代码
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js ie6 oldie" lang="en-US"> <![endif]-->
<!--[if IE 7]>    <html class="no-js ie7 oldie" lang="en-US"> <![endif]-->
<!--[if IE 8]>    <html class="no-js ie8 oldie" lang="en-US"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en-US"> <!--<![endif]-->
<head>
<title>安全检查! | 百度云加速</title>
<meta charset="UTF-8" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
<meta name="robots" content="noindex, nofollow" />

那么通过打印的结果,要做的就很明显了,需要获取一些参数,以及验证码,最后一齐请求
首先是响应头中的参数
[Python] 纯文本查看 复制代码
cookie = response.headers['set-cookie'].split(';')[0]
ray = response.headers['cf-ray'].split('-')[0]

然后是响应体的参数
[Python] 纯文本查看 复制代码
            posturl = '/'.join(shareurl.split('/')[:3])+html.unescape(re.findall('(?<=action=").+?(?=")', response.text)[0])
            r = re.findall('(?<=value=").+?(?=")', response.text)[0]

最后还需要一个验证码,这里的pub参数多次抓包发现是不变的,所以就直接写死
首先获取一个用于获取验证码图片的参数session
[Python] 纯文本查看 复制代码
            url = 'https://captcha.su.baidu.com/session_cb?pub=377e4907e1a3b419708dbd00df9e8f79'
            headers = {
                'Host': 'captcha.su.baidu.com',
                'Referer': shareurl,
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
            }
            response = httpx.get(url, headers=headers).text
            session = response.split('"')[-2]

此时通过session以及前面的pub可以获得验证码图片,保存到本地再手动输入
[Python] 纯文本查看 复制代码
            url = 'https://captcha.su.baidu.com/image?session='+session+'&pub=377e4907e1a3b419708dbd00df9e8f79'
            response = httpx.get(url, headers=headers).content
            with open('验证码.jpg', 'wb') as f:
                f.write(response)
            yanzhengma = input('请输入同目录下的验证码:')

最后构造请求头和请求体,发出请求即可得到目标网页数据
[Python] 纯文本查看 复制代码
            headers = {
                'content-type': 'application/x-www-form-urlencoded',
                'cookie': cookie,
                'referer': shareurl,
                'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
            }
            data = {
                'r': r,
                'id': ray,
                'captcha_challenge_field': session,
                'manual_captcha_challenge_field': yanzhengma,
            }
            response = httpx.post(posturl, headers=headers, data=data)
            print(response.text)

再次查看打印的内容,获取正确
TIM截图20200525222547.jpg

附上完整代码
[Python] 纯文本查看 复制代码
            shareurl = 'https://************/**************************'
            headers = {
                'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
            }
            response = httpx.get(shareurl, headers=headers)
            cookie = response.headers['set-cookie'].split(';')[0]
            ray = response.headers['cf-ray'].split('-')[0]
            posturl = '/'.join(shareurl.split('/')[:3])+html.unescape(re.findall('(?<=action=").+?(?=")', response.text)[0])
            r = re.findall('(?<=value=").+?(?=")', response.text)[0]
            url = 'https://captcha.su.baidu.com/session_cb?pub=377e4907e1a3b419708dbd00df9e8f79'
            headers = {
                'Host': 'captcha.su.baidu.com',
                'Referer': shareurl,
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
            }
            response = httpx.get(url, headers=headers).text
            session = response.split('"')[-2]
            url = 'https://captcha.su.baidu.com/image?session='+session+'&pub=377e4907e1a3b419708dbd00df9e8f79'
            response = httpx.get(url, headers=headers).content
            with open('验证码.jpg', 'wb') as f:
                f.write(response)
            yanzhengma = input('请输入同目录下的验证码:')
            headers = {
                'content-type': 'application/x-www-form-urlencoded',
                'cookie': cookie,
                'referer': shareurl,
                'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
            }
            data = {
                'r': r,
                'id': ray,
                'captcha_challenge_field': session,
                'manual_captcha_challenge_field': yanzhengma,
            }
            response = httpx.post(posturl, headers=headers, data=data)
            print(response.text)


免费评分

参与人数 4吾爱币 +4 热心值 +4 收起 理由
画中画。 + 1 + 1 谢谢@Thanks!
nightring + 1 + 1 哈哈 天书纯属支持大佬
yuhan694 + 1 + 1 我很赞同!
ymhld + 1 + 1 我很赞同!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

苏雨轩 发表于 2020-5-25 23:41
完全看不懂啊,大佬太多
拉玛西亚 发表于 2020-5-25 23:48
浅吻深拥 发表于 2020-5-25 23:53
大佬的教程需要一点水平啊。代码是不是没有导包,httpx报错,会连接超时或者set-cookie报错,不错,继续努力
涛之雨 发表于 2020-5-25 23:53
emmm手动输入验证码的话是不是有点降低效率?
还是只有第一次需要,后面带参数就不需要了?
 楼主| 漁滒 发表于 2020-5-26 00:11
浅吻深拥 发表于 2020-5-25 23:53
大佬的教程需要一点水平啊。代码是不是没有导包,httpx报错,会连接超时或者set-cookie报错, ...

导包写在最上面,忘记加上去了
 楼主| 漁滒 发表于 2020-5-26 00:17
涛之雨 发表于 2020-5-25 23:53
emmm手动输入验证码的话是不是有点降低效率?
还是只有第一次需要,后面带参数就不需要了?

手动输入验证码确实降低了效率,但是首先解决了第一重获取目标网页数据的问题。自动识别验证码可以考虑接入百度AI的通用文字识别来自动识别。最后一次的post好像会返回set-cookie,后面的请求带上这个cookie应该在一段时间内无需再输入验证码。
Nonoby 发表于 2022-2-17 17:04
大佬。我采集网站也是遇到被拦截,不知道怎么突破,能给点思路吗
https://www.youzy.cn/tzy/search/majors/homepage
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-27 08:05

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表