获取数据表

Fetching Data Tables

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)

我使用了两种不同的代码从网站获取数据 table,数据是免费提供的。但是每次我 运行 我的程序都会收到以下错误 'urllib.error.HTTPError: HTTP Error 403: Forbidden'。此外,这些链接似乎在浏览器中运行良好。知道如何解决这个问题吗?

PS:数据无需认证即可查看。

我不确定服务器为什么会引发 301,但通常不鼓励直接对这样的高级请求使用 urllib。您应该改用 requests 包。

等同于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