【发布时间】:2019-08-28 05:14:35
【问题描述】:
我正在尝试抓取这个名为 whoscored.com 的网站,这是我用来抓取它的特定页面的简单代码。
import requests
import pandas as pd
from bs4 import BeautifulSoup
headers = {'User-Agent':
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like
Gecko) Chrome/47.0.2526.106 Safari/537.36'}
page =
"https://www.whoscored.com/Teams/13/RefereeStatistics/England-Arsenal"
pageTree = requests.get(page, headers=headers)
pageSoup = BeautifulSoup(pageTree.content, 'lxml')
print(pageSoup)
代码运行良好,但返回的内容如下 -
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<title>404 - File or directory not found.</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-
serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;}
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;}
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;}
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-
family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-
top:8px;padding:10px;position:relative;}
-->
</style>
</head>
<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
<div class="content-container"><fieldset>
<h2>404 - File or directory not found.</h2>
<h3>The resource you are looking for might have been removed, had its name
changed, or is temporarily unavailable.</h3>
</fieldset></div>
</div>
<script type="text/javascript">
//<![CDATA[
(function() {
var _analytics_scr = document.createElement('script');
_analytics_scr.type = 'text/javascript'; _analytics_scr.async = true;
_analytics_scr.src = '/_Incapsula_Resource?
SWJIYLWA=719d34d31c8e3a6e6fffd425f7e032f3&ns=1&cb=1578388490';
var _analytics_elem = document.getElementsByTagName('script')[0];
_analytics_elem.parentNode.insertBefore(_analytics_scr, _analytics_elem);
})();
// ]]>
</script></body>
</html>
如您所见,它返回 404 - file or directory not found 或 The resource you are looking for might have been removed, had its name
changed, or is temporarily unavailable.最后还有一堆我不太熟悉的错误。
我有一些想法为什么会发生这种情况。也许有 JavaScript(我在最后看到)或者是由于网站的某种对策。但是,我想确切地知道问题出在哪里,我能做些什么来解决这个问题,并确保我得到了我试图从page 中抓取的数据——顺便说一下,这是整个表. 我从在这里阅读类似问题中得到的一点是我需要使用 Selenium,但我不确定如何使用。任何帮助将不胜感激。
我处于空闲状态。我的 Python 版本是 37(64 位),我的电脑是 64 位的。
【问题讨论】:
-
当我在网络浏览器中从脚本打开你的网址时,我也会收到错误 404 - 所以你的网址是错误的。
-
实际的 url 是 whoscored.com/Teams/13/RefereeStatistics/England-Arsenal,但似乎确实有机器人措施,所以请检查网站关于抓取的条款和条件。
-
我将编辑问题。我在复制 URL 道歉时可能犯了一个错误。尽管如此,它仍然会产生同样的问题
标签: python-3.x web-scraping html-table beautifulsoup