【问题标题】:Error when requesting page with requests.get python使用 requests.get python 请求页面时出错
【发布时间】:2020-06-26 21:25:31
【问题描述】:

我正在尝试获取至尊主页的 html 来解析它。 这是我正在尝试的:

from bs4 import BeautifulSoup

all_page = requests.get('https://www.supremenewyork.com/index', headers = {
    'Upgrade-Insecure-Requests': '1',
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'
    }).text
all_page_html = BeautifulSoup(all_page,'html.parser')
print(all_page_html)

但我得到的不是 html,而是这个响应:

<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"/><meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible"/><title>Supreme</title><meta content="Supreme. The official website of Supreme. EST 1994. NYC." name="description"/><meta content="telephone=no" name="format-detection"/><meta content="on" http-equiv="cleartype"/><meta content="notranslate" name="google"/><meta content="app-id=664573705" name="apple-itunes-app"/><link href="//www.google-analytics.com" rel="dns-prefetch"/><link href="//ssl.google-analytics.com" rel="dns-prefetch"/><link href="//d2flb1n945r21v.cloudfront.net" rel="dns-prefetch"/><script src="https://www.google.com/recaptcha/api.js">async defer</script><meta content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no" id="viewport" name="viewport"/><link href="//d17ol771963kd3.cloudfront.net/assets/application-2000eb9ad53eb6df5a7d0fd8c85c0c03.css" media="all" rel="stylesheet"/><script \
e.t.c

这是一种障碍还是我错过了什么?我什至添加了请求的标头,但我仍然得到这种类型的响应而不是正常响应。

【问题讨论】:

  • 上面写着&lt;!DOCTYPE html&gt;&lt;html ...&gt; 所以我会说它 HTML。
  • 但是检查所需页面的html代码它是不同的
  • 这是什么错误?
  • 不是错误,我的意思是它返回另一个东西
  • 如果我在网络浏览器中打开 https://www.supremenewyork.com/index 并查看页面源代码,我会得到您在问题中显示的内容。

标签: python python-3.x python-requests


【解决方案1】:

嗯,这实际上就是页面的样子。它是说它是运行着一些 css 和 javascript 的 HTML 页面,那么你应该使用“检查元素”来搜索你想要抓取的元素,也许写下它们存储的类以便更容易地找到它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-30
    • 1970-01-01
    • 2020-03-28
    相关资源
    最近更新 更多