python-3.x - 为什么我在使用 Pandas 时收到 HTTP 403 错误?
问题描述
想要从特定电子竞技网站上的表格中获取数据,我似乎很挣扎。
有人告诉我 pandas 库只需几行代码就可以帮助我实现这一目标。
import pandas as pd
tables = pd.read_html ('https://www.hltv.org/stats/teams/matches/5752/Cloud9')
print(tables[0])
我尝试编辑它以使我的工作但我没有成功。
import pandas as pd
from urllib.request import Request, urlopen
req = Request('https://www.hltv.org/stats/teams/matches/5752/Cloud9', headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
tables = pd.read_html ('https://www.hltv.org/stats/teams/matches/5752/Cloud9)
print(tables[0])
我被引导相信这可能是我正在寻找的解决方案,或者类似的解决方案,但是当我尝试以这种方式解决问题时,我没有成功。
"Traceback (most recent call last):
File "C:\Users\antho\OneDrive\Documents\Python\tables clloud9.py", line 6, in <module>
webpage = urlopen(req).read()
File "C:\Users\antho\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\antho\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 531, in open
response = meth(req, response)
File "C:\Users\antho\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Users\antho\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 569, in error
return self._call_chain(*args)
File "C:\Users\antho\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
File "C:\Users\antho\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden"
我现在想要的只是拉动链接上的表格。
解决方案
import pandas as pd
from urllib.request import Request, urlopen
req = Request('https://www.hltv.org/stats/teams/matches/5752/Cloud9', headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
tables = pd.read_html ('https://www.hltv.org/stats/teams/matches/5752/Cloud9') #here was the err
print(tables[0])
推荐阅读
- autodesk-forge - 查看器以 404 状态响应。上传/翻译模型没有问题,但查看器 api 中的访问被阻止
- scala - 如何在两个数组列中找到共同元素?
- python - 如何收集所有“子”字典键?
- android - bindService 将客户端限制到特定的应用程序
- asp.net-core - 在给定 HttpContext 的情况下,从 AuthorizationHandler 为查询参数调用默认模型绑定器
- java - 如何将示例部分中的黄瓜数据表转换为 Map
使用java - c++ - 黑白图像转换为数组并返回?
- amazon-web-services - 如何获取 EKS 集群的 URL?
- php - 按 ID 排序 PHP 下拉菜单,而不是按字母顺序
- soa - 编译导入的 jar 文件时出现 SOA-MDS 错误