我正在尝试以下代码:
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/,它正在工作。
请求是为其他网站工作,如谷歌,但不工作这个特定的网站上的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在PC机中
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切换到不同的IP。
发布于 2020-12-05 09:02:54
您在ping之后得到不同的IP,因为www.nseindia.com是通过阿卡迈 CDN传递给您的。因此,无论您是在家里/工作还是在AWS服务器上执行此操作,您都会选择不同的边缘位置。
更重要的是,AWS的IP地址范围是公开知道的。因此,网站明确阻止AWS连接、防止刮伤、攻击或其他不必要的访问并不少见。因此,似乎nseindia阻塞了所有这些AWS地址。这是一个已知的问题,例如这里和这里。
解决方案是不使用AWS或其他流行的nor提供商(nseindia也阻止其他提供商)。您可以尝试通过一些商业虚拟专用网、家庭/工作网络或一些未列入黑名单的代理您的AWS请求。可悲的是,这是一种尝试和观望的方法。但你也必须考虑绕过这些限制的潜在法律/道德问题。
发布于 2020-12-12 04:09:47
服务器的位置是什么?nseindia有可能不允许印度境外的ip地址。尝试使用任何VPN或使用浏览器cookie伪造您的位置(不道德)。此外,Session()已被取消。您可以使用sessions.Session()
https://stackoverflow.com/questions/65121785
复制相似问题