【问题标题】:You don't have permission to access this resource Python webscraping您无权访问此资源 Python webscraping
【发布时间】:2020-07-05 08:14:11
【问题描述】:

我正在尝试对网站进行网络抓取,但当我这样做时,我的输出低于预期。 有什么方法可以抓取这个网站吗?

url = "https://www.mustang6g.com/forums/threads/pre-collision-alert-system.132807/"

page = requests.get(url)
soup = BeautifulSoup(page.text, 'html.parser')
print(soup)

以上代码输出如下

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">

<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access this resource.</p>
</body></html>

【问题讨论】:

  • anonymous13 - 非常感谢您的帮助请求 - 非常感谢您分享您的想法和交流想法。

标签: python web-scraping beautifulsoup


【解决方案1】:

网站服务器希望传递一个标头:

import requests

headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) '\
           'AppleWebKit/537.36 (KHTML, like Gecko) '\
           'Chrome/75.0.3770.80 Safari/537.36'}

URL = 'https://www.mustang6g.com/forums/threads/pre-collision-alert-system.132807/'


httpx = requests.get(URL, headers=headers)

print(httpx.text)

通过传递标头,我们告诉服务器我们是Mozilla:)

【讨论】:

  • Prayson - 非常感谢您的想法和答案 - 非常感谢您分享您的想法和想法交流。该代码对我们有用。
  • 很高兴能帮上忙。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-07
  • 1970-01-01
  • 2021-04-03
  • 1970-01-01
  • 2021-08-17
  • 1970-01-01
  • 2021-12-27
相关资源
最近更新 更多