Python 请求。403 禁地

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38489386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:02:49  来源:igfitidea点击:

Python requests. 403 Forbidden

pythonpython-requests

提问by Толкачёв Иван

I needed to parse a site, but i got an error 403 Forbidden. Here is a code:

我需要解析一个站点,但出现错误 403 Forbidden。这是一个代码:

url = 'http://worldagnetwork.com/'
result = requests.get(url)
print(result.content.decode())

Its output:

它的输出:

<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx</center>
</body>
</html>

Please, say what the problem is.

请说是什么问题。

回答by Alberto Garcia-Raboso

It seems the page rejects GETrequests that do not identify a User-Agent. I visited the page with a browser (Chrome) and copied the User-Agentheader of the GETrequest (look in the Network tab of the developer tools):

该页面似乎拒绝GET未标识User-Agent. 我使用浏览器(Chrome)访问了该页面并复制User-AgentGET请求的标题(查看开发人员工具的网络选项卡):

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]-->
# ...

回答by Hansimov

Just add to Alberto's answer:

只需添加阿尔贝托的答案:

If you still get a 403 Forbiddenafter adding a user-agent, you may need to add more headers, such as referer:

如果403 Forbidden添加a后仍然得到 a user-agent,则可能需要添加更多标题,例如referer

headers = {
    'User-Agent': '...',
    'referer': 'https://...'
}

The headers can be found in the Network > Headers > Request Headersof the Developer Tools. (Press F12to toggle it.)

标题可以Network > Headers > Request Headers在开发者工具的 中找到。(按下F12以切换它。)

回答by Aleksandar

If You are the server's owner/admin, and the accepted solution didn't work for You, then try disabling CSRF protection (link to an SO answer).

如果您是服务器的所有者/管理员,并且接受的解决方案对您不起作用,请尝试禁用 CSRF 保护(链接到 SO 答案)

I am using Spring (Java), so the setup requires You to make a SecurityConfig.javafile containing:

我正在使用 Spring (Java),因此设置要求您制作一个SecurityConfig.java包含以下内容的文件:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure (HttpSecurity http) throws Exception {
        http.csrf().disable();
    }
    // ...
}

回答by Srinu Mareti

Try using:

尝试使用:

import requests

requests.get(url, auth=('username','password'))