【发布时间】:2017-03-20 15:53:37
【问题描述】:
我正在尝试使用 BeautifulSoup 解析来自 rarbg.to 的电影页面。 我正在收集电影的片名。
所以我在 Python 中的代码如下:
import urllib2
from bs4 import BeautifulSoup
url = "https://rarbg.to/torrents.php?category=movies"
hdr = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive',
}
req = urllib2.Request(url, headers=hdr)
try:
page = urllib2.urlopen(req)
except urllib2.HTTPError, e:
print e.fp.read()
# Get all the HTML page
raw_content = page.read()
# print raw_content #debug
# Pass the html page to BeautifulSoup
soup = BeautifulSoup(raw_content)
print soup #debug
movie_titles = soup.find_all("tr","lista2")
print movie_titles
当我第一次运行它时,它正确打印了电影元素列表(表格行)。
但是当我在那之后多次尝试时,它会返回:
<html><head>
</head>
<body>
<style type="text/css">a,abbr,acronym,address,applet,article,aside,audio,b,big,blockquote,body,canvas,caption,center,cite,code,dd,del,details,dfn,div,dl,dt,em,fieldset,figcaption,figure,footer,form,h1,h2,h3,h4,h5,h6,header,hgroup,html,i,iframe,img,ins,kbd,label,legend,li,mark,menu,nav,object,ol,p,pre,q,s,samp,section,small,span,strike,strong,sub,summary,sup,table,tbody,td,tfoot,th,thead,time,tr,tt,u,ul,var,video{margin:0;padding:0;border:0;outline:0;font:inherit;vertical-align:baseline}article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section{display:block}body{line-height:1}ol,ul{list-style:none}blockquote,q{quotes:none}blockquote:after,blockquote:before,q:after,q:before{content:'';content:none}ins{text-decoration:none}del{text-decoration:line-through}table{border-collapse:collapse;border-spacing:0}
body {
background: #000 url("//dyncdn.me/static/20/img/bknd_body.jpg") repeat-x scroll 0 0 !important;
font: 400 8pt normal Tahoma,Verdana,Arial,Arial !important;
}
.button {
background-color: #3860bb;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
cursor: pointer;
text-transform: none;
overflow: visible;
}
.content-rounded {
background: #fff none repeat scroll 0 0 !important;
border-radius: 3px;
color: #000 !important;
padding: 20px;
width:961px;
}
</style><div align="center" style="margin-top:20px;padding-top:20px;color: #000 !important;">
<div class="content-rounded" style="color: #000 !important;">
<img src="//dyncdn.me/static/20/img/logo_dark_nodomain2_optimized.png"/><br/>Please wait while we try to verify your browser...<br/>If you are stuck on this page disable your browser addons<br/><img src="//dyncdn.me/static/20/img/loading_flat.gif"/>
</div>
</div>
<script>
var w = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
var h = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
var days = 7;
var date = new Date();
var name = 'sk';
var value_sk = 'iqcdg1oe63';
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = ";expires="+date.toGMTString();
document.cookie = name+"="+value_sk+expires+"; path=/";
if(w < 100 || h < 100) {
window.location.href = "/threat_defence.php?defence=nojc&r=54677187";
} else {
if(!document.domain) { var ref_cookie = ''; } else { var ref_cookie = document.domain; }
setTimeout(function(){
window.location.href = "/threat_defence.php?defence=2&sk="+value_sk+"&ref_cookie="+ref_cookie+"&r=74070547";
}, 3000);
}
</script>
</body></html>
[]
Process finished with exit code 0
据我所知,Please wait while we try to verify your browser...<br/>If you are stuck on this page disable your browser addons 与问题有关。
DDOS 攻击或验证码是某种预防措施吗?在开发过程中,我每分钟左右只发出一两个请求。
【问题讨论】:
-
阅读 TOS... 您不得,并且您保证并同意您不会做或促进以下任何行为... (7) 使用任何机器人、蜘蛛、网络爬虫、其他自动设备或手动过程来复制我们的网页、种子或未经我们事先明确书面许可而包含的其他内容。 rarbg.to/useragreement.php
标签: python parsing beautifulsoup urllib2 torrent