【问题标题】:Torrent page parsing failsTorrent 页面解析失败
【发布时间】: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...&lt;br/&gt;If you are stuck on this page disable your browser addons 与问题有关。

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

【问题讨论】:

  • 阅读 TOS... 您不得,并且您保证并同意您不会做或促进以下任何行为... (7) 使用任何机器人、蜘蛛、网络爬虫、其他自动设备或手动过程来复制我们的网页、种子或未经我们事先明确书面许可而包含的其他内容rarbg.to/useragreement.php

标签: python parsing beautifulsoup urllib2 torrent


【解决方案1】:

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

现在您可能正在寻找解决此问题的可能方法。 以下是一些:

  1. 在每个请求之前实现 wait 时间(可以使用 import time , time.sleep(seconds))
  2. 使用 Selenium - 'Selenium 自动化浏览器。而已!你用这种力量做什么完全取决于你。 - 我的建议
  3. 代理或其他身份加扰解决方案。

Selenium - 这是一个假浏览器 - 2017 - ME。 它有等到EC.presence_of_element_located((By.ID, "myDynamicElement"))http://selenium-python.readthedocs.io/waits.html等方法 因此,您可以对其进行编程以模仿人类行为。

【讨论】:

  • 谢谢。我大量使用了硒,但我不想使用它。我正在整理一个 POC 宠物项目。没有什么重大或有害的。我会研究代理解决方案,即使我认为这有点矫枉过正,因为我只会每天或每天运行一两次脚本。
  • 是的,我知道那种感觉,我也讨厌使用它,不是因为它不好,而是感觉它不像脚本。你不是在制造机器人,而是在制造人类。
【解决方案2】:

我不得不向网站提出很多请求才能重现此内容。看来我的 ip 现在被屏蔽了。

考虑使用 TOR 之类的东西,或尝试几次后使用 vpn 来更改您的 IP。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    • 2019-07-22
    • 2014-04-05
    • 2011-03-28
    • 2019-09-27
    相关资源
    最近更新 更多