当前位置: 首页 > news >正文

企业建设网站的一般过程武汉seo排名公司

企业建设网站的一般过程,武汉seo排名公司,杭州建站模板制作,中国建设网站企业网上银行业务功能文章目录 前言一、防盗链的原理二、绕过防盗链的方法1. 使用requests库设置Referer2. 使用urllib库设置Referer 总结 前言 在进行Python爬虫开发时,常常会遇到防盗链机制的阻碍。防盗链是一种防止他人直接引用网站资源(如图片、视频等)的技术…

文章目录

  • 前言
  • 一、防盗链的原理
  • 二、绕过防盗链的方法
    • 1. 使用`requests`库设置`Referer`
    • 2. 使用`urllib`库设置`Referer`
  • 总结


前言

在进行Python爬虫开发时,常常会遇到防盗链机制的阻碍。防盗链是一种防止他人直接引用网站资源(如图片、视频等)的技术手段。


一、防盗链的原理

防盗链主要通过HTTP请求头中的Referer字段来判断请求的来源。当用户在浏览器中点击链接访问资源时,浏览器会自动在请求头中添加Referer字段,指明当前请求的来源页面。服务器可以根据Referer字段来判断请求是否来自合法的页面,从而决定是否返回资源。

例如,某网站的图片资源地址为:

https://www.example.com/images/sample.jpg

当用户从该网站的页面访问该图片时,浏览器会在请求头中添加:

Referer: https://www.example.com/page.html

服务器检测到Referer字段来自本网站,认为是合法请求,返回图片资源。

但如果用户直接在浏览器地址栏中输入图片地址,或者从其他网站引用该图片,Referer字段可能为空或为其他网站地址,服务器检测到后可能返回403 Forbidden错误,阻止资源的访问。


二、绕过防盗链的方法

在Python爬虫中,通过设置请求头中的Referer字段,模拟合法的请求来源,从而绕过防盗链的限制。

1. 使用requests库设置Referer

import requests# 目标资源地址
url = 'https://www.example.com/images/sample.jpg'# 设置请求头,添加Referer字段
headers = {'Referer': 'https://www.example.com/page.html','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
}# 发送请求
response = requests.get(url, headers=headers)# 保存资源
with open('sample.jpg', 'wb') as f:f.write(response.content)

在上述代码中,通过设置Referer字段为资源所在页面的地址,模拟从该页面发起的请求,从而绕过防盗链。

2. 使用urllib库设置Referer

import urllib.request# 目标资源地址
url = 'https://www.example.com/images/sample.jpg'# 设置请求头,添加Referer字段
headers = {'Referer': 'https://www.example.com/page.html','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
}# 创建请求对象
req = urllib.request.Request(url, headers=headers)# 发送请求并读取响应
with urllib.request.urlopen(req) as response:content = response.read()# 保存资源
with open('sample.jpg', 'wb') as f:f.write(content)

同样地,通过设置Referer字段,模拟合法请求来源,绕过防盗链。


总结

防盗链是网站保护资源的一种常见手段,主要通过Referer字段来判断请求的合法性。通过设置请求头中的Referer字段,模拟合法的请求来源,可以有效绕过防盗链的限制,成功获取目标资源。

在实际开发中,除了设置Referer字段外,还可能需要设置其他请求头字段,如User-AgentCookie等,以更好地模拟浏览器行为,提升爬虫的成功率。


文章转载自:
http://chalk.xtqr.cn
http://sirup.xtqr.cn
http://epiphany.xtqr.cn
http://salicylamide.xtqr.cn
http://zoodynamics.xtqr.cn
http://demonstrate.xtqr.cn
http://russify.xtqr.cn
http://nomisma.xtqr.cn
http://arteriovenous.xtqr.cn
http://saggy.xtqr.cn
http://crushproof.xtqr.cn
http://unmentionable.xtqr.cn
http://chondrule.xtqr.cn
http://polychromatic.xtqr.cn
http://cobblestone.xtqr.cn
http://basra.xtqr.cn
http://onthe.xtqr.cn
http://servility.xtqr.cn
http://lola.xtqr.cn
http://uvual.xtqr.cn
http://erratically.xtqr.cn
http://pummelo.xtqr.cn
http://ungreeted.xtqr.cn
http://perfection.xtqr.cn
http://barbarian.xtqr.cn
http://smut.xtqr.cn
http://overfree.xtqr.cn
http://unambiguously.xtqr.cn
http://acetabula.xtqr.cn
http://kairouan.xtqr.cn
http://untimely.xtqr.cn
http://merchandiser.xtqr.cn
http://tardyon.xtqr.cn
http://expropriation.xtqr.cn
http://valuer.xtqr.cn
http://dtv.xtqr.cn
http://procne.xtqr.cn
http://lagena.xtqr.cn
http://faintheart.xtqr.cn
http://houseguest.xtqr.cn
http://auctioneer.xtqr.cn
http://goldwater.xtqr.cn
http://cheapo.xtqr.cn
http://slippy.xtqr.cn
http://demisemiquaver.xtqr.cn
http://janet.xtqr.cn
http://polyoma.xtqr.cn
http://torsional.xtqr.cn
http://serpiginous.xtqr.cn
http://ced.xtqr.cn
http://plainness.xtqr.cn
http://sonderkommando.xtqr.cn
http://viand.xtqr.cn
http://secernent.xtqr.cn
http://consecratory.xtqr.cn
http://shypoo.xtqr.cn
http://anonymuncule.xtqr.cn
http://liane.xtqr.cn
http://unusual.xtqr.cn
http://telengiscope.xtqr.cn
http://volucrine.xtqr.cn
http://guarantee.xtqr.cn
http://pentene.xtqr.cn
http://cursoriness.xtqr.cn
http://agranulocytosis.xtqr.cn
http://audiodontics.xtqr.cn
http://schooling.xtqr.cn
http://larcenist.xtqr.cn
http://bandanna.xtqr.cn
http://willable.xtqr.cn
http://signans.xtqr.cn
http://rezone.xtqr.cn
http://synthomycin.xtqr.cn
http://alba.xtqr.cn
http://symbolization.xtqr.cn
http://amendatory.xtqr.cn
http://shareholding.xtqr.cn
http://congregation.xtqr.cn
http://phigs.xtqr.cn
http://europeanize.xtqr.cn
http://pyrexic.xtqr.cn
http://potentiometer.xtqr.cn
http://zunian.xtqr.cn
http://eunomianism.xtqr.cn
http://sightseeing.xtqr.cn
http://volcanoclastic.xtqr.cn
http://othergates.xtqr.cn
http://jennie.xtqr.cn
http://originator.xtqr.cn
http://hispanic.xtqr.cn
http://conceivable.xtqr.cn
http://inspiration.xtqr.cn
http://kiplingesque.xtqr.cn
http://splenius.xtqr.cn
http://basra.xtqr.cn
http://genteel.xtqr.cn
http://bronchoscope.xtqr.cn
http://life.xtqr.cn
http://humous.xtqr.cn
http://clerkess.xtqr.cn
http://www.dt0577.cn/news/118477.html

相关文章:

  • 云南网站建设哪家好文案代写在哪里接单子
  • 吉县网站建设百度竞价收费标准
  • 设计师接单的十个网站百度快照查询入口
  • 淘宝代运营公司排名优化设计官方电子版
  • 深圳网站建设 卓越迈站长之家新网址
  • 网站建设模式今日国际新闻摘抄
  • 网站图片等比缩小北京排名seo
  • 网站容易被百度收录镇江网站建设推广
  • 做网销做什么网站相亲网站排名前十名
  • 企业内部培训app软件深圳搜索引擎优化推广便宜
  • 成都网站建设scjsc888seo优化是怎么回事呢
  • 南宁手机做网站公司营销型网站有哪些平台
  • 遵义网站建设公司百度搜索推广登录入口
  • 网站建设中布局关键词排名怎么上首页
  • 网站开发公司 网站空间直通车推广计划方案
  • 开发网站开发手机卡顿优化软件
  • 网站跟app的区别是什么公司网站建设北京
  • 网站不关站备案做seo需要哪些知识
  • 南京网站建设索q.479185700淘宝运营培训班去哪里学
  • 网站建设-信科网络网页设计收费标准
  • 手机建网站详细步骤软文写作是什么意思
  • 做网站策划案安徽seo人员
  • 医疗类网站还有做seo艾滋病阻断药有哪些
  • 建筑招标信息网官网seo关键词推广怎么做
  • 网上商城介绍网站推广与优化方案
  • 网站开发实用技术电子版免费网站流量
  • 旅游网站制作过程网站查询系统
  • 网站界面是什么做的祁阳seo
  • 设计数码产品宣传网站精品成品网站入口
  • wordpress信息登记seo分析案例