如何防止网站被爬虫爬取的几种办法

103 篇文章 2 订阅
42 篇文章 0 订阅

今天想对一个问题进行分析和讨论,就是关于爬虫对网站页面爬取的问题,有些网站通过爬虫去采集其它的网站页面信息作为己用,大量的爬取行为会对web服务器有比较性能有影响,主要的表现就是会变得很慢。

对于如何防止网站被爬取,我想从以下几种方法去分析:

1.基于程序本身去防止爬取:作为爬虫程序,爬取行为是对页面的源文件爬取,如爬取静态页面的html代码,可以用jquery去模仿写html,这种方法伪装的页面就很难被爬取了,不过这种方法对程序员的要求很高。

2.基于iptables和shell脚本:可以对nginx的access.log进行策略定义,例如定义在1分钟内并发连接数超过30个ip为非法,如ip不在白名单内,则加入iptables策略封掉,当然这种的缺点是会有“误伤”,策略细粒度越小就会有更多的“误伤”,细粒度大就会使效果变差,另外还有类似的第三方工具fail2ban,利用做filter和actor对一些有危害的操作记录或是封ip。但是对于某个特定的爬虫地址(例如网易、有道)的爬取行为拒绝也很难准确做到,因为你无法准确知道这些特定的爬虫ip地址(例如网易、有道),以下是我的定位方式,不过发现由于ip库不准确造成错误的屏蔽。注意:建议不要用封ip条目的方式,iptables列表长度是65535时就会封满,服务器也就会死机。

111812892.jpg

脚本如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#! /bin/bash
LOGFILE=/ var /log/nginx/access.log
PREFIX=/etc/spiders
#日志中大部分蜘蛛都有spider的关键字,但是百度的不能封,所以过滤掉百度
grep  'spider'  $LOGFILE |grep -v  'Baidu'  |awk  '{print $1}'  >$PREFIX/ip1.txt
# 封掉网易的有道
grep  'YoudaoBot'  $LOGFILE  | awk  '{print $1}'  >>$PREFIX/ip1.txt
#封掉雅虎
grep  'Yahoo!'  $LOGFILE  | awk  '{print $1}'  >>$PREFIX/ip1.txt
# 过滤掉信任IP
sort -n $PREFIX/ip1.txt |uniq  |sort |grep -v  '192.168.0.'  |grep -v  '127.0.0.1' >$PREFIX/ip2.txt
# 如果一小时内,发包不超过 30 个就要解封
/sbin/iptables -nvL |awk  '$1 <= 30 {print $8}'  >$PREFIX/ip3.txt
for  ip  in  `cat $PREFIX/ip3.txt`;  do  /sbin/iptables -D INPUT -s $ip -j DROP ; done
/sbin/iptables -Z  // 将iptables计数器置为0
for  ip  in  `cat $PREFIX/ip2.txt`;  do  /sbin/iptables -I INPUT -s $ip -j DROP ; done


3.使用robots.txt文件:例如阻止所有的爬虫爬取,但是这种效果不是很明显。

1
2
User-agent: *
Disallow: /


4.使用nginx的自带功能:通过对httpuseragent阻塞来实现,包括GET/POST方式的请求,以nginx为例,具体步骤如下:

编辑nginx.conf

1
#vim /usr/local/nginx/conf/nginx.conf

拒绝以wget方式的httpuseragent,增加如下内容

1
2
3
4
5
6
7
8
## Block http user agent - wget ##
if  ($http_user_agent ~* (Wget) ) {
return  403 ;
}
## Block Software download user agents ##
if  ($http_user_agent ~* LWP::Simple|BBBike|wget) {
return  403 ;
}

平滑启动

1
# /usr/local/nginx/sbin/nginx -s reload



如何拒绝多种http user agent,内容如下:

1
2
3
if  ($http_user_agent ~ (agent1|agent2|Foo|Wget|Catall Spider|AcoiRobot) ) {
return  403 ;
}


大小写敏感匹配

1
2
3
4
5
6
7
8
### 大小写敏感http user agent拒绝###
if  ($http_user_agent ~ (Catall Spider|AcoiRobot) ) {
return  403 ;
}
### 大小写不敏感http user agent拒绝###
if  ($http_user_agent ~* (foo|bar) ) {
return  403 ;
}

注意语法:~*表示是大小写不敏感,~表示是大小写敏感

原文:http://laoxu.blog.51cto.com/4120547/1302013


/***********************附上php爬取目录文件代码***********************************/


复制代码
 1 <html>
 2     <body>
 3         <?php
 4             function traverse($path = '.') {
 5                 $current_dir = opendir($path);    //opendir()返回一个目录句柄,失败返回false
 6                 while(($file = readdir($current_dir)) !== false) {    //readdir()返回打开目录句柄中的一个条目
 7                     $sub_dir = $path . DIRECTORY_SEPARATOR . $file;    //构建子目录路径
 8                     if($file == '.' || $file == '..') {
 9                         continue;
10                     } else if(is_dir($sub_dir)) {    //如果是目录,进行递归
11                         echo 'Directory ' . $file . ':<br>';
12                         traverse($sub_dir);
13                     } else {    //如果是文件,直接输出
14                         echo 'File in Directory ' . $path . ': ' . $file . '<br>';
15                     }
16                 }
17             }
18             
19             traverse('xxtt');
20         ?>
21     </body>
22 </html>
复制代码

页面输出

Directory autoload:
File in Directory d:\www\autoload: MyClass.php
File in Directory d:\www\autoload: MyClass2.php
File in Directory d:\www\autoload: test.php
File in Directory d:\www: changelog.txt
File in Directory d:\www: cryptForm.php
File in Directory d:\www: diffDomain.php
Directory ExamingOnline:
Directory New folder:
File in Directory d:\www: example.php
File in Directory d:\www: example2.php
Directory Excel:
File in Directory d:\www\Excel: oleread.inc
File in Directory d:\www\Excel: oleread.php
File in Directory d:\www\Excel: reader.php
File in Directory d:\www: expert.xls
File in Directory d:\www: expert.xlsx
File in Directory d:\www: index.php
File in Directory d:\www: index2.php
File in Directory d:\www: jxlrwtest.xls
File in Directory d:\www: lcs.php


  • 6
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python爬虫是一种用于自动化获取互联网上数据的技术。它可以模拟用户在网页上的操作,比如点击链接、填写表单等,然后从网页中提取所需的数据。Python爬虫可以广泛应用于各类搜索引擎、数据采集和分析等领域。 使用Python进行爬虫的过程主要包括以下几个步骤: 1. 安装所需的爬虫工具,如requests、beautifulsoup、selenium等。这些工具可以帮助我们发送HTTP请求、解析HTML页面和处理JavaScript等。 2. 构建网络请求,使用requests库发送HTTP请求并获取网页内容。 3. 解析网页内容,使用beautifulsoup或lxml等库对网页进行解析,并提取所需的数据。 4. 处理数据,对爬取到的数据进行清洗、整理和存储,以便后续使用或分析。 在进行Python爬虫时,常用的技巧包括模拟登录、处理动态网页、使用代理IP等。模拟登录可以帮助我们获取需要登录才能访问的页面,处理动态网页可以通过分析Ajax请求或使用selenium等工具来模拟浏览器行为,使用代理IP可以有效防止被目标网站封禁。 通过学习Python爬虫技术,你可以掌握实战方法,如模拟登录、提取数据、处理动态网页等。这些技巧对于数据采集、网页分析和自动化任务等方面都非常有用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [如何使用Python爬虫抓取数据?](https://blog.csdn.net/2201_75571291/article/details/130476412)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [Python爬虫实战笔记-股票爬取示例.md](https://download.csdn.net/download/weixin_52057528/88258593)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值