今天,我在做Python网络爬虫时(web spider),正在爬取哔哩哔哩的某一个视频,发现一个致命问题,就是单纯在headers中加入UA(user-Agent)是不够的,也就是说,我的爬虫没有足够的权限全访问此网站,所以,我的headers还少了一些东西。

headers = {#'Accept-Encoding':'','user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'
}

我首先想到的是Referer防盗链,简单的来说,防盗链就是告诉哔哩哔哩的服务器,我是从哔哩哔哩官网跳转过来的,而不是从别的地方跳转,所以,以此来蒙掉哔哩哔哩服务器,以便获取数据。

但我,找了一圈后,却并没有找到Referer防盗链,这下可就尴尬了,到是多了一些之前从未见过的数据

例如:

  1. sec-ch-ua:

    "Chromium";v="21", " Not;A Brand";v="99"

  2. sec-ch-ua-mobile:

    ?0

  3. sec-ch-ua-platform:

    "Windows

所以我怀疑,我的浏览器已经更新了,或者发生了其他错误,将我的Referer给弄没了,但其实,Referer还存在,只是我们看不到而已,所以,现在的问题,就成了

如何将Rederer重新显示出来

于是,我查阅了大量资料,观看了大量的CSDN的文章,总结出以下一点好用的方法:

1.在html的head标签中加入:

<meta name="Referrer" content="origin" />

那么我,问题又来了,我该怎么加?

一开始我毫无思绪,烦躁不已,但我当我冷静后,重新审视这个问题,我终于明白这其中奥妙。

那么,接下来,请跟着我的步骤走下去,慢慢的走,不要心急,慢慢揭开那神秘的面纱。

1。选择你要爬取的页面或者视频,在空白处右键,选择“查看网页源代码”(不同浏览器,这里会不一样。)

2.打开后,在鼠标右键,选择“网页另存为”,这样,我们在电脑中获得了一个以“.html”为后缀的html代码

3.更改后缀,将后缀".html"更改为“.txt”,以便我们添加<meta name="Referrer" content="origin" />

4.打开改好的文档。注意!!,一定要在<head>标签中加入<meta name="Referrer" content="origin" />,我们以下面某页面源代码为例:

<!-- saved from url=(0086)https://www.bilibili.com/video/BV1oY411p7Yr?vd_source=b305ecdc9ae3bd6e7c27b55794f9edd9 -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><body><div class="line-gutter-backdrop"></div><form autocomplete="off"><label class="line-wrap-control">自动换行<input type="checkbox" aria-label="自动换行"></label></form><table><tbody><tr><td class="line-number" value="1"></td><td class="line-content"><span class="html-doctype">&lt;!DOCTYPE html&gt;</span><span class="html-tag">&lt;html <span class="html-attribute-name">lang</span>="<span class="html-attribute-value">zh-Hans</span>"&gt;</span><span class="html-tag">&lt;head <span class="html-attribute-name">itemprop</span>="<span class="html-attribute-value">video</span>" <span class="html-attribute-name">itemscope</span> <span class="html-attribute-name">itemtype</span>="<span class="html-attribute-value">http://schema.org/VideoObject</span>"&gt;</span><span class="html-tag">&lt;meta <span class="html-attribute-name">name</span>="<span class="html-attribute-value">format-detection</span>"

我们需要在head标签中加入<meta name="Referrer" content="origin" />

所以,更改为以下形式:

<!-- saved from url=(0086)https://www.bilibili.com/video/BV1oY411p7Yr?vd_source=b305ecdc9ae3bd6e7c27b55794f9edd9 -->
<html><head><meta name="Referrer" content="origin" /><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><body><div class="line-gutter-backdrop"></div><form autocomplete="off"><label class="line-wrap-control">自动换行<input type="checkbox" aria-label="自动换行"></label></form><table><tbody><tr><td class="line-number" value="1"></td><td class="line-content"><span class="html-doctype">&lt;!DOCTYPE html&gt;</span><span class="html-tag">&lt;html <span class="html-attribute-name">lang</span>="<span class="html-attribute-value">zh-Hans</span>"&gt;</span><span class="html-tag">&lt;head <span class="html-attribute-name">itemprop</span>="<span class="html-attribute-value">video</span>" <span class="html-attribute-name">itemscope</span> <span class="html-attribute-name">itemtype</span>="<span class="html-attribute-value">http://schema.org/VideoObject</span>"&gt;</span><span class="html-tag">&lt;meta <span class="html-attribute-name">name</span>="<span class="html-attribute-value">format-detection</span>"

5.更改好后,在将文档后缀更改为html,双击运行后即可。

这样当我们在回到我们要爬取的页面时,在按F12打开开发者工具时,在找到我们的数据包,即可发现,Referer就出现了。

那么,当我加上Referer后,我的爬虫代码也就通过了,什么,你想看我爬哔哩哔哩的爬虫的代码??        满足你!

import requestsimport reimport jsonimport pprintimport osfilename = '哔哩哔哩//'
if not os.path.exists(filename):os.makedirs(filename)
url = 'https://www.bilibili.com/video/BV1oY411p7Yr?vd_source=b305ecdc9ae3bd6e7c27b55794f9edd9'headers = {#'Accept-Encoding':'','Referer': 'https://www.bilibili.com/video/BV1oY411p7Yr?vd_source=b305ecdc9ae3bd6e7c27b55794f9edd9','user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'
}response = requests.get(url=url,headers=headers).text
#print(response)#数据解析:html_data = re.findall('<script>window.__playinfo__=(.*?)</script>',response)[0]
#获取标题
title = re.findall('<title data-vue-meta="true">(.*?)i</title>',response)[0]
#print(html_data)json_data = json.loads(html_data)
#print(json_data)pprint.pprint(json_data)audio_url = json_data['data']['dash']['audio'][0]['baseUrl']
video_url = json_data['data']['dash']['video'][0]['baseUrl']
print(audio_url)
print(video_url)response2 = requests.get(url=audio_url,headers=headers).content
response3 = requests.get(url=video_url,headers=headers).contentwith open(filename+title+'.mp3',mode='wb') as f:f.write(response2)
with open(filename+title+'.mp4',mode='wb') as f:f.write(response3)
print("爬去完成!")

最后,如果,本文章有帮到你,那么请你送我一个免费的赞,好么QWQ(打字不易)

Python爬虫:(亲测,已解决!)解决在使用谷歌浏览器的开发者工具时,没有Referer防盗链缺失问题。相关推荐

  1. pycharm安装第三方库:Try to run this command from the system terminal. Make sure that you use the问题,亲测已解决

    pycharm安装第三方库:报错Try to run this command from the system terminal. Make sure that you use the correct ...

  2. 打开一个浏览器跳转到2345浏览器主页 这里以Google为例 亲测已解决。

    打开一个浏览器跳转到2345浏览器主页 这里以Google为例 亲测已解决. 1,打开浏览器,找到设置. 2,在设置里边找到 代理设置 点开. 3,点开常规,将方框内箭头位置的2345浏览器主页换成 ...

  3. 2022.3.1亲测有效,解决Can‘t connect to local MySQL server through socket ‘/tmp/mysql.sock‘ (2)

    1.遇到了这个报错: 2. 网上查到这样一篇博客, 感谢作者, : 亲测有效,解决Can 't connect to local MySQL server through socket '/tmp/m ...

  4. 西方哲学智慧2018网课考题(本人亲测,已满分)

    (本人亲测,已满分) 一.单选题(50题,50.0分) 1.斯宾诺莎认为笛卡尔哲学理论的缺陷是? A.缺乏完整性 B.因为怀疑而自我消解 C.二元论缺乏第一动力 D.不能解释神的存在 1.0 分 我的 ...

  5. 借贷宝是真的吗?有到账的吗?多久到账?到账了没?——笔者亲测已到账,附提款、到账截图...

    动动手指,20元人民币立即到手:http://www.cnblogs.com/mfryf/p/4754384.html 注册提现流程:下载借贷宝APP -- 打开APP点击注册 -- 输入手机号及密码 ...

  6. 亲测成功-彻底解决Eclipse无法安装插件的问题-尝试过绝大多数已有的方法后

    如果看到这句话的人可以电脑正常上网,那么就不用往下看了. 最大的问题是Eclipse MarketPlace被拦截了!!! eclipse安装插件的主要方式有三种.    -   第一种是在插件官网下 ...

  7. 亲测方案:解决HBuilder X启动提示语法助手无法访问的问题

    最近在使用HBuilder X的时候突然发现敲代码的时候没有了强大的提示,对于一个可以偷懒的功能,突然没了实属有点不习惯.于是就重启发现有个错误提示:'语法助手无法访问,正在尝试重启服务-',原来问题 ...

  8. “此Flash Player 与您的地区不相容”,谷歌高版本,亲测2019-2-28可以解决

    这是原地址,解决方法如下 翻墙后才是打开的正确的Adobe的官网下载地址:https://get.adobe.com/cn/flashplayer/ 这里下载的Flash Player版本经过安装后问 ...

  9. 亲测有效!解决macOS Big Sur系统Parallels Desktop 16的USB无法使用和无法联网问题

    小编亲测!不需要什么启动器!!!按教程就可以解决!!此版本Parallels Desktop 16修复MacOS 11 big sur 下的无法联网.融合模式问题!USB功能也已经修复!!16.1.1 ...

最新文章

  1. struct2(四)编写Struct2 的Action
  2. pip3 install face_recognition
  3. 一天1个机器学习知识点(二)
  4. Microsoft SQL server 2008 安装未取得权限操作
  5. Java柏林算法,柏林噪音 - 我做错了什么?
  6. OnLineML:时序数据挖掘
  7. Llinux 磁盘配额的搭建和常规问题解答
  8. java符号%3e%3e是什么意思,终于找到了!有了它你就可以读懂字节码了!
  9. vue render函数_Vue原理解析(一):Vue到底是什么?
  10. oracle练习(mldn视频课程)四
  11. 大数据新闻推送你怎么看_“大数据”新闻推送中存在的不足与改进
  12. aardio - API调用分析
  13. vue导出js中的函数_js中的函数
  14. IDEA一致卡在build时间过长问题处理
  15. unity的ui跟随鼠标移动
  16. 支持DoH的DNS服务器,谷歌公共DNS正式支持DoH加密 更安全并且不影响速度
  17. markdown多级列表
  18. 北航周号益:从“鹦鹉”到“乌鸦”,AI的本质是探索通用智能的可能性
  19. 联合国维和女战士,巾帼不让须眉 | 经济学人全球早报精选
  20. Windows Server 2008文件服务器

热门文章

  1. 微信客户管理方式及如何微信客户管理
  2. ps图层锁定后如何解锁
  3. Xshell和Xftp使用(非商业用途可以免费使用啦)
  4. 【游戏设计模式】之三 状态模式、有限状态机 Unity版本实现
  5. Python项目:外星人入侵(汇总)
  6. 读书笔记:《群论彩图版》
  7. switch基本用法
  8. vue生成自定义二维码样式
  9. mongodb mysql 知乎_为什么 MongoDB 索引选择B-树,而 Mysql 索引选择B+树(精干总结)...
  10. 被手机“绑架”又不能完全指望手机的柔宇科技,上市之后会是一片坦途吗?