gpt4 book ai didi

python-3.x - python 请求库在亚马逊 aws 中不起作用

转载 作者:行者123 更新时间:2023-12-03 16:56:37 24 4
gpt4 key购买 nike

我正在尝试以下代码:

import requests

headers = {
'authority': 'www.nseindia.com',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36 OPR/72.0.3815.320',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'sec-fetch-site': 'none',
'sec-fetch-mode': 'navigate',
'sec-fetch-user': '?1',
'sec-fetch-dest': 'document',
'accept-language': 'en-GB,en;q=0.9',
}

nse = requests.Session()
x = nse.get("https://www.nseindia.com/", headers=headers)

print(x.text)
以下代码在我的电脑上运行,但是当我将它放入 aws 时它没有响应。
我也查了ping https://www.nseindia.com/这是工作。
requests 适用于谷歌等其他网站,但不适用于 aws 上的这个特定网站。
在 EC2 中:
Python 3.8.5 (default, Jul 28 2020, 12:59:40) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> headers = {
... 'authority': 'www.nseindia.com',
... 'upgrade-insecure-requests': '1',
... 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36 OPR/72.0.3815.320',
... 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
... 'sec-fetch-site': 'none',
... 'sec-fetch-mode': 'navigate',
... 'sec-fetch-user': '?1',
... 'sec-fetch-dest': 'document',
... 'accept-language': 'en-GB,en;q=0.9',
... }
>>> nse = requests.Session()
>>> nse.get("https://www.nseindia.com/", headers=headers)
最后一行没有输出。
在我的电脑中:
Python 3.8.5 (default, Jul 28 2020, 12:59:40) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> headers = {
... 'authority': 'www.nseindia.com',
... 'upgrade-insecure-requests': '1',
... 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36 OPR/72.0.3815.320',
... 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
... 'sec-fetch-site': 'none',
... 'sec-fetch-mode': 'navigate',
... 'sec-fetch-user': '?1',
... 'sec-fetch-dest': 'document',
... 'accept-language': 'en-GB,en;q=0.9',
... }
>>> nse = requests.Session()
>>> nse.get("https://www.nseindia.com/", headers=headers)
<Response [200]>
>>>
检测到问题:
在EC2
ping www.nseindia.com
PING www.nseindia.com (23.9.215.115) 56(84) bytes of data.
64 bytes from a23-9-215-115.deploy.static.akamaitechnologies.com (23.9.215.115): icmp_seq=1 ttl=51 time=1.07 ms
64 bytes from a23-9-215-115.deploy.static.akamaitechnologies.com (23.9.215.115): icmp_seq=2 ttl=51 time=1.09 ms
电脑输入
ping www.nseindia.com
PING www.nseindia.com (23.35.32.140) 56(84) bytes of data.
64 bytes from a23-35-32-140.deploy.static.akamaitechnologies.com (23.35.32.140): icmp_seq=1 ttl=57 time=65.8 ms
64 bytes from a23-35-32-140.deploy.static.akamaitechnologies.com (23.35.32.140): icmp_seq=2 ttl=57 time=61.5 ms
64 bytes from a23-35-32-140.deploy.static.akamaitechnologies.com (23.35.32.140): icmp_seq=3 ttl=57 time=73.1 ms
ping 不同的IP。

最佳答案

ping 后得到不同的 IP 因为 www.nseindia.com通过 akamai 交付给您CDN。所以你在ping 不同的边缘位置无论您是从家庭/工作还是从 AWS 服务器执行此操作。
更何况,IP address ranges of AWS公开 .因此,网站明确阻止 AWS 连接以防止抓取、攻击或其他不需要的访问的情况并不少见。因此似乎 nseindia 正在阻止 所有这些 AWS IP 地址。这是一个已知问题,如所示 herehere举些例子。
解决方案不是使用 AWS 或其他流行的供应商(nseindia 也阻止其他供应商)。你可以试试 代理您的 AWS 请求 可能是通过一些商业 VPN、家庭/工作网络或未列入黑名单的东西。可悲的是,这是一种试探性的方法。但您还必须考虑绕过这些限制的潜在法律/道德问题。

关于python-3.x - python 请求库在亚马逊 aws 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65121785/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com