【发布时间】:2019-12-12 01:42:42
【问题描述】:
import urllib2
hdr={'User-Agent': 'Mozilla/5.0', 'Accept-Language': 'en-US,en;q=0.8'}
print urllib2.urlopen(urllib2.Request('https://mobile.lowvig.ag/sports', headers=hdr)).read()
当我从这两个在线解释器运行这个两行程序时,它会打印正确的 html 代码: https://repl.it/languages/python https://paiza.io/en/languages/python
但是,当我在家用 PC 上运行它时,它会打印出看起来像 cloudflare 警告页面的内容:
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js ie6 oldie" lang="en-US"> <![endif]-->
<!--[if IE 7]> <html class="no-js ie7 oldie" lang="en-US"> <![endif]-->
<!--[if IE 8]> <html class="no-js ie8 oldie" lang="en-US"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en-US"> <!--<![endif]-->
<head>
<title>Attention Required! | Cloudflare</title>
<meta name="captcha-bypass" id="captcha-bypass" />
<meta charset="UTF-8" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
<meta name="robots" content="noindex, nofollow" />
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1" />
<link rel="stylesheet" id="cf_styles-css" href="/cdn-cgi/styles/cf.errors.css" type="text/css" media="screen,projection" />
<!--[if lt IE 9]><link rel="stylesheet" id='cf_styles-ie-css' href="/cdn-cgi/styles/cf.errors.ie.css" type="text/css" media="screen,projection" /><![endif]-->
<style type="text/css">body{margin:0;padding:0}</style>
<!--[if gte IE 10]><!--><script type="text/javascript" src="/cdn-cgi/scripts/zepto.min.js"></script><!--<![endif]-->
<!--[if gte IE 10]><!--><script type="text/javascript" src="/cdn-cgi/scripts/cf.common.js"></script><!--<![endif]-->
</head>
<body>
<div id="cf-wrapper">
<div class="cf-alert cf-alert-error cf-cookie-error" id="cookie-alert" data-translate="enable_cookies">Please enable cookies.</div>
<div id="cf-error-details" class="cf-error-details-wrapper">
<div class="cf-wrapper cf-header cf-error-overview">
<h1 data-translate="challenge_headline">One more step</h1>
<h2 class="cf-subheadline"><span data-translate="complete_sec_check">Please complete the security check to access</span> mobile.lowvig.ag</h2>
</div><!-- /.header -->
<div class="cf-section cf-highlight cf-captcha-container">
<div class="cf-wrapper">
<div class="cf-columns two">
<div class="cf-column">
<div class="cf-highlight-inverse cf-form-stacked">
<form class="challenge-form" id="challenge-form" action="/cdn-cgi/l/chk_captcha" method="get">
<input type="hidden" name="s" value="b3df19c7958863448f72e934c06ae7332861f030-1564882975-1800-AcBCDHplwdTaCzOhBkp56Ja0sk/FSnXB3lJxmJpKdOTH0MYNevcFL2u/8NelatBwLBq+AfsceRViMsHQs7gnTUCvyRKSpGh4IizRs3BPQflkFl9uaScZ4CoP1yZCKYWVWrFDkwELhwE6KPGUci0e6XT1ph465Mzcryl6xtId0S0U"></input>
<script type="text/javascript" src="/cdn-cgi/scripts/cf.challenge.js" data-type="normal" data-ray="500cd664e845b615" async data-sitekey="6LfBixYUAAAAABhdHynFUIMA_sa4s-XsJvnjtgB0"></script>
<div class="g-recaptcha"></div>
<noscript id="cf-captcha-bookmark" class="cf-captcha-info">
<div><div style="width: 302px">
<div>
<iframe src="https://www.google.com/recaptcha/api/fallback?k=6LfBixYUAAAAABhdHynFUIMA_sa4s-XsJvnjtgB0" frameborder="0" scrolling="no" style="width: 302px; height:422px; border-style: none;"></iframe>
</div>
<div style="width: 300px; border-style: none; bottom: 12px; left: 25px; margin: 0px; padding: 0px; right: 25px; background: #f9f9f9; border: 1px solid #c1c1c1; border-radius: 3px;">
<textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px; height: 40px; border: 1px solid #c1c1c1; margin: 10px 25px; padding: 0px; resize: none;"></textarea>
<input type="submit" value="Submit"></input>
</div>
</div></div>
</noscript>
</form>
</div>
</div>
<div class="cf-column">
<div class="cf-screenshot-container">
<span class="cf-no-screenshot"></span>
</div>
</div>
</div><!-- /.columns -->
</div>
</div><!-- /.captcha-container -->
<div class="cf-section cf-wrapper">
<div class="cf-columns two">
<div class="cf-column">
<h2 data-translate="why_captcha_headline">Why do I have to complete a CAPTCHA?</h2>
<p data-translate="why_captcha_detail">Completing the CAPTCHA proves you are a human and gives you temporary access to the web property.</p>
</div>
<div class="cf-column">
<h2 data-translate="resolve_captcha_headline">What can I do to prevent this in the future?</h2>
<p data-translate="resolve_captcha_antivirus">If you are on a personal connection, like at home, you can run an anti-virus scan on your device to make sure it is not infected with malware.</p>
<p data-translate="resolve_captcha_network">If you are at an office or shared network, you can ask the network administrator to run a scan across the network looking for misconfigured or infected devices.</p>
<p data-translate="resolve_captcha_privacy_pass">Another way to prevent getting this page in the future is to use Privacy Pass. Check out the browser extension in the <a href="https://chrome.google.com/webstore/detail/privacy-pass/ajhmfdgkijocedmfjonnpjfojldioehi">Chrome Store</a>.</p>
</div>
</div>
</div><!-- /.section -->
<div class="cf-error-footer cf-wrapper">
<p>
<span class="cf-footer-item">Cloudflare Ray ID: <strong><snip></strong></span>
<span class="cf-footer-separator">•</span>
<span class="cf-footer-item"><span>Your IP</span>: <snip></span>
<span class="cf-footer-separator">•</span>
<span class="cf-footer-item"><span>Performance & security by</span> <a href="https://www.cloudflare.com/5xx-error-landing?utm_source=error_footer" id="brand_link" target="_blank">Cloudflare</a></span>
</p>
</div><!-- /.error-footer -->
</div><!-- /#cf-error-details -->
</div><!-- /#cf-wrapper -->
<script type="text/javascript">
window._cf_translation = {};
</script>
</body>
</html>
我想弄清楚这些环境访问此页面的方式与我的家用 PC 有何不同,以便我可以成功抓取此网站。
更新:针对两个提交的答案: 以下代码通过代理路由请求(必须从 urllib2 切换到请求),并打开相同的 cloudflare 页面(代理 IP 在底部正确显示为“您的 ip”):
import requests
hdr={'User-Agent': 'Mozilla/5.0', 'Accept-Language': 'en-US,en;q=0.8'}
proxyDict={'https': 'https://user:pass@ip:port'}
print requests.get('https://mobile.lowvig.ag/sports', headers=hdr, proxies=proxyDict).content
这让我觉得可能有一些特定于我的环境(除了我的 IP)的东西触发了这个问题。此外,我可以在普通浏览器中通过我的家庭 IP 毫无问题地查看网站。
【问题讨论】:
-
Web 服务器可能希望阻止来自无法识别 IP 的脚本请求。
-
本网站与编码无关 - 因此来自链接到解释器的 IP 地址的请求几乎不可能是答案。
-
Cloudflare 是 CDN。他们的算法判定您的流量不合法,于是 DDOS 缓解措施开始生效。
-
虽然我倾向于同意 - 我想知道为什么我能够在运行这两行脚本的新机器/ip 上复制 cloudflare 警告(但它仍然适用于解释器)跨度>
标签: python python-2.7 web-scraping urllib2 cloudflare