【问题标题】:python requests not getting full pagepython请求没有得到整页
【发布时间】:2020-06-10 11:29:54
【问题描述】:

"""这是我的代码"""

import requests
from bs4 import BeautifulSoup
import random
from selenium import webdriver
url ="http://www.yopmail.com/en/?smith"
request = requests.get(url)
soup = BeautifulSoup(request.text, 'html5lib')
print(soup)

"""它返回这个输出"""

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
</head>
<body onload="document.getElementById('f').submit();">
<form action="." id="f" method="post">
<input id="yp" name="yp" type="hidden" value="XAQHlAwL5ZwL1ZQZlAGH3ZGV"/>
<input id="login" name="login" type="hidden" value="smith"/>
<input id="id" name="id" type="hidden" value=""/>
</form>
<noscript><br/><br/>  <strong>Your browser does not support javascript or it may be disabled</strong></noscript>

</body></html>

"""我想要整个 SRC 代码而不是这个"""

【问题讨论】:

标签: python web beautifulsoup request screen-scraping


【解决方案1】:

发生这种情况是因为请求在执行 Javascript 之前获取了源。 您可以安装 requests-html 并从 requests_html 导入 HTMLSession。 支持的功能:

  • 完整的 JavaScript 支持!
  • CSS 选择器(又名 jQuery 样式,感谢 PyQuery)。
  • XPath 选择器,适合胆小的人。
  • 模拟用户代理(类似于真正的网络浏览器)。
  • 自动跟踪重定向。
  • 连接 - 池和 cookie 持久性。
  • 您熟悉和喜爱的请求体验,具有神奇的解析功能 能力。
  • 异步支持

例子:

pip install requests-html

from requests_html import HTMLSession
from requests_html import AsyncHTMLSession

url2search = "https://******"
session = HTMLSession()
r = session.get(url2search)

为 JS 渲染为:

r.html.render()

注意,第一次运行 render() 方法时,它会将 Chromium 下载到你的主目录(例如 ~/.pyppeteer/)。这只会发生一次。您可能还需要安装一些 Linux 软件包才能使 pyppeteer 正常工作。

有关this link的更多详细信息。

【讨论】:

    【解决方案2】:

    我宁愿把它写成评论而不是答案,因为我只是给你一个提示,但我没有足够的声誉来写 cmets。所以这是我的两分钱:

    注意线条

    <body onload="document.getElementById('f').submit();">
    <form action="." id="f" method="post">
    

    在您的 HTML 源代码中。这可能是一个非常基本的保护措施,可以防止像您打算做的那样进行抓取尝试,并且将您对 requests.get 的使用更改为 requests.post 可能就足够了;包括更改类似 GET 的参数

    /?史密斯

    在 URL 中改为 POST 参数。

    不过,您之后可能会遇到更多需要您能够使用 JavaScript 的代码。在这种情况下,请检查 Basu_C 的另一个答案。

    【讨论】:

    • 我尝试使用 requests.post (url) ,但我得到了相同的结果。
    猜你喜欢
    • 2014-11-14
    • 2017-04-04
    • 2019-07-07
    • 2016-12-23
    • 2014-11-30
    • 2015-06-20
    • 2013-01-17
    • 2020-02-09
    • 1970-01-01
    相关资源
    最近更新 更多