簡體   English   中英

從網站上抓取一張表格並存儲為 Pandas

[英]Scrape a table from a website and store as pandas

在 Python 中,我想抓取網站中的表格(它是日本期權交易信息),並將其存儲為 Pandas 數據框。

該網站在這里,您需要單擊“選項報價”才能訪問我要抓取表格的頁面。 最終 URL 是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 中導入表格,但我不確定為什么我什至無法閱讀該頁面。 任何幫助,將不勝感激!

順便說一句,如果您單擊中間列中的灰色箭頭, 在此處輸入圖片說明 ,

它會像這樣添加另一行。 在此處輸入圖片說明

並且可以通過單擊這些按鈕來打開和關閉它。 在此處輸入圖片說明

如果我也可以導入這些行就好了,但這並不是必須的。

閱讀它說的熊貓函數read_html的文檔

將 HTML 表讀入 DataFrame 對象列表。

因此,該函數需要 html 表格形式的結構化輸入。 我實際上無法訪問您要鏈接的網站,但我猜它會給您一個完整的網站。

您需要以結構化格式提取數據,以便 Pandas 理解它。 你需要刮它。 有很多工具可以做到這一點,其中一個流行的是BeautifulSoup

Tl; dr:所以您需要做的是下載帶有requests的網站,將其傳遞給BeautifulSoup ,然后使用BeautifulSoup以結構化格式提取數據。


更新的答案:

請求返回400的原因似乎是因為該網站需要一些額外的標頭 - 我只是將瀏覽器所做的請求轉儲到請求中,它可以正常工作!

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)

根據艾哈邁德的回答,你快到了:

你只需要得到這張桌子:

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
..                                                 ...  ...                                                ...

試圖從網站上抓取一張桌子<div tags< div><div id="text_translate"><p> 我正在嘗試刮這張桌子<a href="https://momentranks.com/topshot/account/mariodustice?limit=250" rel="nofollow noreferrer">https://momentranks.com/topshot/account/mariodustice?limit=250</a></p><p> 我試過這個:</p><pre> import requests from bs4 import BeautifulSoup url = 'https://momentranks.com/topshot/account/mariodustice?limit=250' page = requests.get(url) soup = BeautifulSoup(page.content, 'lxml') table = soup.find_all('table', attrs={'class':'Table_tr__1JI4P'})</pre><p> 但它返回一個空列表。 有人可以就如何解決這個問題提供建議嗎?</p></div></div>

[英]Trying to scrape a table from a website with <div tags

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM