python - 通过请求python仅在heroku远程机器上访问被拒绝错误
问题描述
我面临这个问题,当我从本地机器访问 url 的页面源时,它工作正常,但是当我在 heroku 机器上的代码上运行相同的代码时,它显示访问被拒绝。
我曾尝试更改标题(例如添加Referers
或更改User-Agent
),但这些解决方案都不起作用。
本地机器
~/Development/repos/eater-list master python manage.py shell 1 ↵ 12051 21:15:32
>>> from accounts.zomato import *
>>> z = ZomatoAPI()
>>> response = z.page_source(url='https://www.zomato.com/ncr/the-immigrant-cafe-khan-market-new-delhi')
>>> response[0:50]
'<!DOCTYPE html>\n<html lang="en" prefix="og: http'
>>> response[0:100]
'<!DOCTYPE html>\n<html lang="en" prefix="og: http://ogp.me/ns#" >\n<head>\n <meta charset="utf-8"
远程机器
~ $ python manage.py shell
Python 3.5.7 (default, Jul 17 2019, 15:27:27)
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from accounts.zomato import *
>>> z = ZomatoAPI()
>>> response = z.page_source(url='https://www.zomato.com/ncr/the-immigrant-cafe-khan-market-new-delhi')
>>> response
'<HTML><HEAD>\n<TITLE>Access Denied</TITLE>\n</HEAD><BODY>\n<H1>Access Denied</H1>\n \nYou don\'t have permission to access "http://www.zomato.com/ncr/the-immigrant-cafe-khan-market-new-delhi" on this server.<P>\nReference #18.56273017.1572225939.46ec5af\n</BODY>\n</HTML>\n'
>>>
ZOMATO API 代码
标头或请求版本没有变化。
class ZomatoAPI:
def __init__(self):
self.user_key = api_key
self.headers = {
'Accept': 'application/json',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/77.0.3865.90 Safari/537.36',
'user-key': self.user_key}
def page_source(self, url):
fng = requests.session()
page_source = fng.get(
url, headers=self.headers).content.decode("utf-8")
return page_source
将不胜感激一些建议。
解决方案
检查响应 HTTP 状态代码。可能是 Heroku 的 IP 被 Zomato 禁止了。这比人们想象的更普遍——像 Cloudflare 这样的服务很多时候会将常见的 IP 放在“禁止列表”中。
这是您应该寻找的有关 HTTP 状态代码的内容,以便为您提供更多上下文。
推荐阅读
- python - Python Django 类别表单创建者的问题
- css - 在 CSS3 网格布局期间 grid-row-start 是如何工作的?
- python-3.x - Python3如何从while循环中设置DataFrame
- react-native - 当用户按下回车键或搜索栏或用户停止输入 react native 时我们调用 api 的搜索功能
- c# - Unity2D:SceneManager加载下一个场景之前的延迟
- stream - Wowza Streaming Engine Manager 无法连接到 Wowza Streaming Engine 服务(http://localhost:8087)
- c - 我可以禁用或忽略 Apple 对 C 标准头文件的添加吗?
- css - 在 BrowserComponent 中禁用垂直滚动
- assembly - 在一条指令中具有与操作数相同的寄存器是否合法?
- html - 如何结束单元格直到右箭头