获取数据表

2024-05-16 13:45:30 发布

您现在位置:Python中文网/ 问答频道 /正文

import urllib.request
with urllib.request.urlopen('https://pakstockexchange.com/stock2/index_new.php?section=research&page=show_price_table_new&symbol=ABOT') as response:
        html=respnse.read()

import pandas as pd
df=pd.read_html('https://pakstockexchange.com/stock2/index_new.php?section=research&page=show_price_table_new&symbol=ABOT')
print(df)

我使用了两种不同的代码从一个网站获取数据表,因为数据是免费的。但每次我运行程序时都会出现以下错误'urllib.error.HTTPError:HTTP错误403:禁止'。此外,这些链接在浏览器中似乎运行良好。你知道怎么解决这个问题吗?你知道吗

PS:无需身份验证即可查看数据。你知道吗


Tags: httpsimportcomnewindexrequestshowpage
1条回答
网友
1楼 · 发布于 2024-05-16 13:45:30

我不清楚服务器为什么要发出301,但一般来说,不鼓励直接使用urllib来处理这样的高级别请求。您应该改用^{}包。你知道吗

等价的requests获取:

r = requests.get("https://pakstockexchange.com/stock2/index_new.php?section=research&page=show_price_table_new&symbol=ABOT")

很好用。你知道吗

r.status_code == 200
True

相关问题 更多 >