gpt4 book ai didi

python - Torrent 页面解析失败

转载 作者:太空宇宙 更新时间:2023-11-04 00:38:19 28 4
gpt4 key购买 nike

我正在尝试使用 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 攻击或验证码的某种预防措施吗?在开发过程中,我每分钟左右只发出一两个请求。

最佳答案

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

现在您可能正在寻找解决此问题的可能方法。这里有一些:

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

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

关于python - Torrent 页面解析失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42908182/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com