我正在尝试使用 beautiful soup 在 google colab 上抓取网站。我能够在本地计算机上抓取内容,但是当我尝试在云上抓取内容时。我收到错误。
'\nAccess Denied\n\nAccess Denied\n \nYou don\'t have permission to access "http://www.justdial.com/Bangalore/Spardha-Mithra-IAS-KAS-Coaching-Centre-Opposite-Maruthi-Medicals-Vijayanagar/080PXX80-XX80-140120184741-R6P8_BZDET?" on this server.\nReference #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 的“重置所有运行时”功能来分配新的虚拟机,以排除您运行的其他笔记本代码带来的任何副作用。
关于python - 谷歌合作实验室: Access Denied You don't have permission to access "website" on this server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53681507/