【问题标题】:Python - Getting A Page's Complete HTML Via Url / Request ERRORPython - 通过 Url / Request ERROR 获取页面的完整 HTML
【发布时间】:2016-07-13 03:13:03
【问题描述】:

我正在尝试获取此页面的 html:

 url = 'http://www.metacritic.com/movie/oslo-august-31st/critic-reviews'

我正在尝试使用请求来获取它:

 oslo = requests.get(url)

但他们似乎知道我正在以这种方式访问​​它以及当我打开它时 我得到的文件:

\n\n\n403 Forbidden\n\n\n

Error 403 Forbidden

\n

Forbidden

\n

Guru Meditation:

\n

XID: 961167012

\n
\n

清漆缓存服务器

\n\n\n

除了手动复制和粘贴每个页面中的每个 html 之外,还有其他方法可以访问 html 吗?

【问题讨论】:

  • 一些网站查看“用户代理”标头或其他标头,以判断请求是否来自网络爬虫。如果某些网站认为您是爬虫,他们会拒绝您的请求。你要发送什么标头?

标签: python html url web-scraping python-requests


【解决方案1】:

您需要指定 User-Agent header 才能获得 200 响应:

import requests

url = 'http://www.metacritic.com/movie/oslo-august-31st/critic-reviews'

response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'})
print(response.status_code)

【讨论】:

    猜你喜欢
    • 2019-01-16
    • 1970-01-01
    • 2022-06-28
    • 2020-08-03
    • 1970-01-01
    • 2011-01-15
    • 1970-01-01
    • 2019-09-24
    • 1970-01-01
    相关资源
    最近更新 更多