从网站上抓取 table 并存储为 pandas

Scrape a table from a website and store as pandas

在Python中,我想抓取网站中的table(这是日本期权交易信息),并将其存储为pandas数据框。

该网站是 here, and you need to click "Options Quotes" in order to access the page where I want to scrape the table. The final URL is https://svc.qri.jp/jpx/english/nkopm/ 但您无法直接访问此页面。

这是我的尝试:

pd.read_html("https://svc.qri.jp/jpx/english/nkopm/")
...HTTPError: HTTP Error 400: Bad Request

所以我想我需要添加一个用户代理。这是我的另一个尝试:

url = "https://svc.qri.jp/jpx/english/nkopm/"
pd.read_html(requests.get(url, headers={'User-agent': 'Mozilla/5.0'}).text)
...ValueError: No tables found

再次尝试

import urllib
url = 'https://svc.qri.jp/jpx/english/nkopm/'
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
response = opener.open(url)
tables = pd.read_html(response.read(), attrs={"class":"price-table"})[0]
...HTTPError: HTTP Error 400: Bad Request

我知道如何使用 pandas,因此不必首先将其导入到整洁的数据框中。我只需要先在 pandas 中导入 table,但我不确定为什么我什至无法阅读该页面。如有任何帮助,我们将不胜感激!

顺便说一句,如果您单击中间列中的灰色箭头,

它将像这样添加另一行。

点击这些按钮即可全部打开和关闭。

如果我也可以导入这些行就好了,但这不是必须的。

正在阅读 pandas 函数的文档 read_html 它说

Read HTML tables into a list of DataFrame objects.

因此函数需要 html table 形式的结构化输入。我实际上无法访问您链接到的网站,但我猜它会给您返回整个网站。

您需要以结构化格式提取数据,以便 pandas 理解它。你需要刮掉它。有很多工具可以做到这一点,其中一个流行的工具是 BeautifulSoup.

Tl;dr:所以您需要做的是使用 requests 下载网站,将其传递给 BeautifulSoup,然后使用 BeautifulSoup 以结构化格式提取数据.


更新后的答案:

请求返回 400 的原因似乎是因为该网站需要一些额外的 headers - 我只是将我的浏览器所做的请求转储到请求中并且它有效!

import requests

headers = {
    'Connection': 'keep-alive',
    'Cache-Control': 'max-age=0',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'Sec-Fetch-Site': 'cross-site',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-User': '?1',
    'Sec-Fetch-Dest': 'document',
    'Referer': 'https://www.jpx.co.jp/english/markets/index.html',
    'Accept-Language': 'de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7,it;q=0.6,la;q=0.5',
}

response = requests.get('https://svc.qri.jp/jpx/english/nkopm/', headers=headers, cookies=cookies)

根据 Ahmad's 的回答,您快完成了:

获得 table 所需的全部内容是:

import requests
import pandas as pd

headers = {
    'Connection': 'keep-alive',
    'Cache-Control': 'max-age=0',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'Sec-Fetch-Site': 'cross-site',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-User': '?1',
    'Sec-Fetch-Dest': 'document',
    'Referer': 'https://www.jpx.co.jp/english/markets/index.html',
    'Accept-Language': 'de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7,it;q=0.6,la;q=0.5',
}

response = requests.get('https://svc.qri.jp/jpx/english/nkopm/', headers=headers)
table = pd.read_html(response.text, attrs={"class": "price-table"})[0]
print(table)

这输出:

                                                  CALL  ...                                                PUT
                                       Settlement09/18  ...                                    Settlement09/18
0                                                    2  ...                                               3030
1    Delta  Gamma  Theta  Vega  0.0032  0.0000  -0....  ...              Delta  Gamma  Theta  Vega  -  -  -  -
2                                                Delta  ...                                                NaN
3                                               0.0032  ...                                                NaN
4                                                Delta  ...                                                NaN
..                                                 ...  ...                                                ...