简体   繁体   English

python flask 请求在本地主机上有效,但在远程服务器上无效

[英]python flask requests works on localhost but does not work on remote server

I'm trying to make a wallpaper page from the website "https://www.wallpaperflare.com",我正在尝试从网站“https://www.wallpaperflare.com”制作壁纸页面,

When I try to run it on localhost it always works and displays the original page of the website.当我尝试在 localhost 上运行它时,它总是可以工作并显示网站的原始页面。

But when I deploy to Heroku the page doesn't display the original page from the website, but "Error Get Request, Code 403" Which means requests don't work on that url.但是,当我部署到 Heroku 时,该页面不会显示来自网站的原始页面,而是“错误获取请求,代码 403”,这意味着请求不适用于该 url。

This is my code:这是我的代码:

@app.route("/wallpapers", methods=["GET"])
def wallpaper ():
    page = requests.get("https://www.wallpaperflare.com")
    if page.status_code == 200:
        return page.text
    else:
        return "Error Get Request, Code {}".format(page.status_code)

is there a way to solve it?有没有办法解决它?

HTTP Error code 403 means Forbidden . HTTP 错误代码 403 表示Forbidden You can read more here你可以在这里阅读更多

It means wallpaperflare.com is not allowing you to make the request.这意味着wallpaperflare.com不允许您提出请求。 This is because websites generally do not want scripts to be making requests to them.这是因为网站通常不希望脚本向它们发出请求。 Make sure to read robots.txt of a site to see it's script crawling policies.请务必阅读网站的robots.txt以查看其脚本抓取策略。 More on that here更多关于这里

It works on your local machine as it is not yet blacklisted by wallpaperflare.com它可以在您的本地计算机上运行,因为它尚未被 wallpaperflare.com 列入黑名单

Two things here:这里有两件事:

  • the user agent - unless you spoof it, the request module is going to use its own string and it's very obvious you are a bot用户代理- 除非你欺骗它,否则请求模块将使用它自己的字符串,很明显你是一个机器人
  • the IP address - your server IP address may be denied for various reasons, whereas your home IP address works just fine. IP 地址 - 您的服务器 IP 地址可能由于各种原因被拒绝,而您的家庭 IP 地址工作正常。

It is also possible that the remote site applies different policies based on the client, if you are a bot then you might be allowed to crawl a bit but rate limiting measures could apply for example.远程站点也可能会根据客户端应用不同的策略,如果您是机器人,那么您可能会被允许抓取一点,但可能会应用速率限制措施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM