python - 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]>
为什么会发生这种情况,有什么办法可以摆脱它。
解决方案
很确定这是服务器端速率限制。你的代码在 colab 中对我来说很好。您可以尝试使用 colab 的“重置所有运行时”功能来分配新的 VM,以排除您运行的其他笔记本代码的任何副作用。
推荐阅读
- javascript - 为什么此代码引用不同的结果
- javascript - 如何获得 jupyterlab 的 javascript 依赖项的明确列表
- python - python中蛋白质序列的一种热编码
- javascript - 可以让ejs文件渲染,我使用节点
- swift - 如何将故事板界面连接到 SwiftUI (macOS) AppDelegate?
- python - 在 Python 中将文本文件转换为 PDF
- javascript - 风格和哈巴狗设置可变
- r - 首先在 geom_point 中绘制 NA 值
- r - 绘制由 R 中的变量指定的函数
- postgresql - postgres“交易空闲”13小时