Torrent页面解析失败

时间:2017-03-20 15:53:38

标签: python parsing beautifulsoup urllib2 torrent

我正在尝试使用rarbg.to解析BeautifulSoup的电影页面。 我正在尝试收集电影的标题。

所以我的Python代码如下:

10^4932

当我第一次运行它时,它正确打印了一个电影元素列表(表格行)。

但是当我在那之后多次尝试时,它会返回:

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 与此问题有关。

DDOS攻击或验证码是否有某种预防措施?在开发过程中,我每分钟只发出一两个请求。

2 个答案:

答案 0 :(得分:1)

我不得不向网站提出很多请求来重现这一点。看起来我的ip现在已被阻止。

考虑使用TOR或vpn之类的内容在几次尝试后更改您的IP。

答案 1 :(得分:1)

DDOS 保护不是阻止 \ 过滤。 这里的问题是他们使用其他类型的确认来确定您是否是人(如验证码)。 正如您在此处所看到的,它会将您重定向到另一个页面(人类浏览器将自动跟随您的脚本。)

现在你可能正在寻找这个问题的可能解决方案。 以下是一些:

  1. 在每个请求之前实施等待时间(您可以使用导入时间,time.sleep(秒))
  2. 使用 Selenium - 'Selenium可自动化浏览器。而已!你用这种力量做什么完全取决于你。 - 我的推荐
  3. 代理或其他身份加密解决方案。
  4. Selenium - 这是一个虚假的浏览器 - 2017年 - 我。 它有像等待EC.presence_of_element_located((By.ID, "myDynamicElement")) http://selenium-python.readthedocs.io/waits.html之类的方法 因此,您可以对其进行编程以模仿人类的行为。