Python 请求.403 禁地 [英] Python requests. 403 Forbidden
本文介绍了Python 请求.403 禁地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要解析一个站点,但我收到了 403 Forbidden 错误.这是一个代码:
I needed to parse a site, but i got an error 403 Forbidden. Here is a code:
url = 'http://worldagnetwork.com/'
result = requests.get(url)
print(result.content.decode())
它的输出:
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx</center>
</body>
</html>
请说出问题所在.
推荐答案
似乎页面拒绝了未标识 User-Agent
的 GET
请求.我使用浏览器 (Chrome) 访问了该页面并复制了 GET
请求的 User-Agent
标头(查看开发人员工具的网络选项卡):
It seems the page rejects GET
requests that do not identify a User-Agent
. I visited the page with a browser (Chrome) and copied the User-Agent
header of the GET
request (look in the Network tab of the developer tools):
import requests
url = 'http://worldagnetwork.com/'
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
result = requests.get(url, headers=headers)
print(result.content.decode())
# <!doctype html>
# <!--[if lt IE 7 ]><html class="no-js ie ie6" lang="en"> <![endif]-->
# <!--[if IE 7 ]><html class="no-js ie ie7" lang="en"> <![endif]-->
# <!--[if IE 8 ]><html class="no-js ie ie8" lang="en"> <![endif]-->
# <!--[if (gte IE 9)|!(IE)]><!--><html class="no-js" lang="en"> <!--<![endif]-->
# ...
这篇关于Python 请求.403 禁地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文