【爬虫】如何解决爬虫爬取图片时遇到百度安全验证的问题?即页面上没有显示图片的源地址,没有img标签,只有div标签

大家好,我是好学的小师弟。这周工作中我在爬虫爬取百度图片的时候 遇到了一个问题,即爬取百度图片的时候,打印爬取的百度图片页面,打印出来的text正文中只有div标签,没有想要下载图片的img标签和图片src原图片地址。如下所示:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title>百度安全验证</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0">
    <meta name="format-detection" content="telephone=no, email=no">
    <link rel="shortcut icon" href="https://www.baidu.com/favicon.ico" type="image/x-icon">
    <link rel="icon" sizes="any" mask href="https://www.baidu.com/img/baidu.svg">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
    <link rel="stylesheet" href="https://ppui-static-wap.cdn.bcebos.com/static/touch/css/api/mkdjump_0635445.css" />
</head>
<body>
    <div class="timeout hide">
        <div class="timeout-img"></div>
        <div class="timeout-title">网络不给力,请稍后重试</div>
        <button type="button" class="timeout-button">返回首页</button>
    </div>
    <div class="timeout-feedback hide">
        <div class="timeout-feedback-icon"></div>
        <p class="timeout-feedback-title">问题反馈</p>
    </div>
<script src="https://wappass.baidu.com/static/machine/js/api/mkd.js"></script>
<script src="https://ppui-static-wap.cdn.bcebos.com/static/touch/js/mkdjump_fbb9952.js"></script>
</body>
</html>

后来发现了,这是百度的反爬虫机制所导致的。察觉到你是爬虫而不是人为下载。

 <head>
    <meta charset="utf-8">
    <title>百度安全验证</title>

 思路:一般我们都是用requests发起get请求,有的时候我们还会加个请求头。但是这个很容易被反爬虫所查获,所以我们尽量要装的像一点,把自己伪装成一个"真人"

header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36'}  #header里面只有一个user-agent太少了

解决方法:打开F12,这里以谷歌浏览器为例

把这四个键值对也给copy下来,加到你之前写的请求头header中

'Cookie':'',#cookie你先自己登录百度帐号就有了
'Accept':'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
'Accept-Encoding':'gzip, deflate, br',
'Accept-Language':'zh-CN,zh;q=0.9'

 即应该写成下面这样

header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36',
        'Cookie':'',#cookie需要你先自己在浏览器登录百度账号,再按f12就有了
        'Accept':'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
        'Accept-Encoding':'gzip, deflate, br',
        'Accept-Language':'zh-CN,zh;q=0.9'
        }

这样你就能成功获取页面信息,接着进行正则表达式,找到图片源地址了

完整代码

import re
import requests
header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36',
        'Cookie':'',#自己先在浏览器登录百度账号
        'Accept':'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
        'Accept-Encoding':'gzip, deflate, br',
        'Accept-Language':'zh-CN,zh;q=0.9'
        }
url='https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&sf=1&fmq=&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&fm=index&pos=history&word=%E5%A4%B4%E5%83%8F'
html=requests.get(url,headers=header)
html.encoding='utf8'
print(html.text)

新人创作不易,觉得不错的看官,点个赞吧,么么哒!!!

转载注明出处!

爬取所有子页面的网址以及它们里面高清图片的下载地址,通常需要使用网络爬虫技术,结合HTML解析库如Python的BeautifulSoup或者lxml。这里是一个简化的步骤说明: 1. **获取页面网址**: - 使用requests库发送HTTP请求,访问主页面并抓取其链接列表。 ```python import requests from bs4 import BeautifulSoup def get_sub_pages(url): response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') links = [a['href'] for a in soup.find_all('a', href=True) if is_valid_link(a['href'])] return links def is_valid_link(link): # 判断是否为有效的子页面链接 # ... (添加判断逻辑) pass main_url = "your_main_page_url" sub_urls = get_sub_pages(main_url) ``` 2. **解析子页面获取高清图片下载地址**: - 对每个子页面URL,再次发送请求并解析HTML。 - 使用BeautifulSoup或其他库找出包含图片标签(如`<img>`),特别是那些`src`属性指向高清图片的。 ```python def get_image_links(url): img_links = [] response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') high_def_img_tags = soup.find_all('img', {'src': re.compile(r'.*\.jpg|\.png|\.jpeg')}) for tag in high_def_img_tags: img_links.append(tag['src']) return img_links high_res_links = [get_image_links(sub_url) for sub_url in sub_urls] ``` 请注意,这只是一个基本框架,实际操作中可能需要处理更复杂的网站结构,比如登录验证、反爬虫机制等。此外,对于图片下载地址的提取,可能需要额外处理,例如从CDN链接跳转到实际文件地址。
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

好学的小师弟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值