猫史档案馆


电商网站反爬策略

用户:LaicalLaical查看:0 回复:1 评论:0 创建时间:2020-08-26T17:26:46


1、电商网站反爬

电商网站的反爬策略是非常严格的。爬虫用selenium访问电商网站时会出现验证码,但是通过不了。原来是很多网站对selenium有严格检测。检测是不是$cdc_asdjflasutopfhvcZLmcfl 、navigator.webdriver等这些特俗标志。当然我们也可以通过这个在电商网站的JS中看到相关的检测代码信息:

1589270365953-efedc66a-c076-450f-a5fb-2ae74b996d23.png

在控制台下输入以下命令window.navigator.webdriver会发现和正常的浏览器打开的有所不同

1589270372233-0027c085-a8ef-4683-b7f0-09263bc04504.png

2、修改selenium

正常的浏览器都会检测到selenium。这时就需要设置开发者模式,避免目标网站检测出selenium或者修改浏览器的webdriver

3、启用代理

配置上亿牛云代理的方式

# 启动时设置上游代理服务器 # 代理服务器地址、端口、用户名、密码请替换成自己的 mitmproxy --mode=upstream:http://t.16yun.cn:31111 --upstream-auth=username:password

代码demo:

<?php  // 要访问的目标页面  $url = "http://httpbin.org/ip";  $urls = "https://httpbin.org/ip"; 
 // 代理服务器(产品官网 www.16yun.cn)  define("PROXY_SERVER", "tcp://t.16yun.cn:31111"); 
 // 代理身份信息  define("PROXY_USER", "username");  define("PROXY_PASS", "password"); 
 $proxyAuth = base喵_encode(PROXY_USER . ":" . PROXY_PASS); 
 // 设置 Proxy tunnel  $tunnel = rand(1,10000); 
 $headers = implode("\r\n", [  "Proxy-Authorization: Basic {$proxyAuth}",  "Proxy-Tunnel: ${tunnel}",  ]);  $sniServer = parse_url($urls, PHP_URL_HOST);  $options = [  "http" => [  "proxy" => PROXY_SERVER,  "header" => $headers,  "method" => "GET",  'request_fulluri' => true,  ],  'ssl' => array(  'SNI_enabled' => true, // Disable SNI for https over http proxies  'SNI_server_name' => $sniServer  )  ];  print($url);  $context = stream_context_create($options);  $result = file_get_contents($url, false, $context);  var_dump($result); 
 // 访问 HTTPS 页面  print($urls);  $context = stream_context_create($options);  $result = file_get_contents($urls, false, $context);  var_dump($result); ?>


回复

上一页1 页 / 共 1下一页
爱python的小萌新爱python的小萌新

requests-sofa~~

点赞0


评论