首页 > 解决方案 > Google colab:拒绝访问您无权访问此服务器上的“网站”

问题描述

我正在尝试使用漂亮的汤在 google colab 上抓取网站。我可以在我的本地机器上抓取内容,但是当我试图在云上抓取它时。我收到一个错误。

'\n访问被拒绝\n\n访问被拒绝\n \n您无权访问“ http://www.justdial.com/Bangalore/Spardha-Mithra-IAS-KAS-Coaching-Centre-Opposite-Maruthi- Medicals-Vijayanagar/080PXX80-XX80-140120184741-R6P8_BZDET ?” 在此服务器上。\n参考 #18.740f1160.1544263996.61a6bb6e\n\n\n'

当我在本地机器上运行相同的代码时,它运行良好。

import requests
import re
from bs4 import BeautifulSoup
url = 'https://www.justdial.com/Bangalore/Spardha-Mithra-IAS-KAS-Coaching-Centre-Opposite-Maruthi-Medicals-Vijayanagar/080PXX80-XX80-140120184741-R6P8_BZDET?xid=QmFuZ2Fsb3JlIEJhbmsgRXhhbSBUdXRvcmlhbHM='
res  = requests.get(url, headers  = {'User-Agent': 'Mozilla/5.0'})
soup = BeautifulSoup(res.content, 'html.parser')
print(res)

输出 :

<Response [403]>

为什么会发生这种情况,有什么办法可以摆脱它。

标签: pythonbeautifulsoupautomationgoogle-cloud-platformgoogle-colaboratory

解决方案


很确定这是服务器端速率限制。你的代码在 colab 中对我来说很好。您可以尝试使用 colab 的“重置所有运行时”功能来分配新的 VM,以排除您运行的其他笔记本代码的任何副作用。


推荐阅读