python - RoboBrowser 的不同网站响应

标签 python python-requests robobrowser

我正在尝试检索网站的 HTML。当我在 Chrome 中查看页面时,我会看到一个包含日期和费率选项的表格。但是,当我使用 RoboBrowser 请求站点时,我看到了一个完全不同的简短 HTML 响应。我做错了什么?

我的代码:

from robobrowser import RoboBrowser
browser = RoboBrowser(parser='html.parser')

browser.open('http://www.marriott.com/reservation/availabilitySearch.mi?propertyCode=ATLRZ')

print(browser.parsed)

结果:

<!DOCTYPE doctype html>

<html>
<head><script src="/common/js/marriottCommon.js" type="text/javascript"> </script>
<meta charset="utf-8">
</meta></head>
<body>
<script>
        var xhttp = new XMLHttpRequest();
        xhttp.addEventListener("load", function(a,b,c){
          window.location.reload()
        });
        xhttp.open('GET', '/reservation/availabilitySearch.mi?istl_enable=true&istl_data', true);
        xhttp.send();
      </script>
</body>
</html>

最佳答案

尝试模仿运行 javascript 的浏览器会做什么。看起来该网站在加载时正在重新加载页面。这意味着除非您拥有正确的 cookie,否则它可能不会让您访问它。 xhr 请求可能是防止机器人程序的附加检查。因此,使用 browser.session 来保留 cookie,向 xhr 发送一个请求,然后尝试使用新的 cookie 重新加载页面。

关于python - RoboBrowser 的不同网站响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41274956/

相关文章:

python - 对使用 requests 库的 python 应用程序进行单元测试

Python RoboBrowser - 如何从此页面获取内容

python - 混合 float 和字符串的不一致数据类型推断

python - django 二进制(无源代码)部署

python - 在 Python 2.7 中将不公平的硬币转换为公平的硬币

python - 在 Tkinter 中创建超链接

Python Request.Post 到 API 不起作用

python - 统计多个子串同时出现在一个字符串中的次数

python - 使用 Python 单击网站按钮

python - 如何查找具有特定父元素的元素?