【问题标题】:Web Scraping Table into Pandas DataframeWeb 抓取表到 Pandas 数据框
【发布时间】:2020-09-16 11:16:36
【问题描述】:

我是使用 Pandas 的初学者。但我想在这里获取 Nvidia 网站上的 G-Sync 游戏监视器表:https://www.nvidia.com/en-us/geforce/products/g-sync-monitors/specs/ 并将其转换为 Pandas for Python 中的数据帧。

我尝试做的第一件事是

import pandas as pd
df = pd.read_html('https://www.nvidia.com/en-us/geforce/products/g-sync-monitors/specs/')

但这似乎不起作用。我收到 ValueError: No tables found

然后我试着做

import requests
import lxml.html as lh
page = requests.get('https://www.nvidia.com/en-us/geforce/products/g-sync-monitors/specs/')

但不知何故我得到 ContentDecodingError: ('Received response with content-encoding: gzip, but failed to decode it.', error('Error -3 while decompressing data: wrong header check')).

如果有人能解释为什么前两种方法不起作用以及如何将表格实际放入数据框中,那将非常有帮助。谢谢!

【问题讨论】:

  • 看起来所有数据都在 XHR 中可用:nvidia.com/content/dam/en-zz/Solutions/geforce/… 你可以requests.get() 然后json.loads() 内容用作 python 字典
  • 查看此评论“该表在页面 html 中不存在,它在页面的其余部分之后异步加载。Pandas 不会等待页面加载 java 内容。您可能需要一些像 Selenium 这样的自动化来在尝试解析页面之前加载页面“来自这篇文章:stackoverflow.com/questions/53398785/…

标签: python pandas web-scraping


【解决方案1】:

数据通过json请求动态加载。

此脚本将 json 数据加载到数据框中并打印出来:

import re
import json
import pandas as pd

url = 'https://www.nvidia.com/en-us/geforce/products/g-sync-monitors/specs/'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:76.0) Gecko/20100101 Firefox/76.0'}

html_txt = requests.get(url, headers=headers).text

json_url =  'https://www.nvidia.com' + re.search(r"'url': '(.*?)'", html_txt).group(1)

data = requests.get(json_url, headers=headers).json()

# uncomment this to print all data:
# print(json.dumps(data, indent=4))

def fn(x):
    out = []
    for v in x:
        if isinstance(v, dict):
            out.append(v['en'])
        else:
            out.append(v)
    return out

df = pd.json_normalize(data['data'], max_level=0).apply(fn)
print(df)

打印:

                  type manufacturer      model  hdr     size lcd type        resolution variable refresh rate range variable overdrive variable refresh input    driver needed
0      G-SYNC ULTIMATE         Acer    CP7271K  Yes       27      IPS    3840x2160 (4K)                     1-144Hz                Yes           Display Port              N/A
1      G-SYNC ULTIMATE         Acer        X27  Yes       27      IPS    3840x2160 (4K)                     1-144Hz                Yes           Display Port              N/A
2      G-SYNC ULTIMATE         Acer        X32  Yes       32      IPS    3840x2160 (4K)                     1-144Hz                Yes           Display Port              N/A
3      G-SYNC ULTIMATE         Acer        X35  Yes       35       VA  3440x1440 (WQHD)                     1-200Hz                Yes           Display Port              N/A
4      G-SYNC ULTIMATE         Asus       PG65  Yes       65       VA    3840x2160 (4K)                     1-144Hz                Yes           Display Port              N/A
..                 ...          ...        ...  ...      ...      ...               ...                         ...                ...                    ...              ...
159  G-SYNC Compatible           LG    2020 ZX  Yes   77, 88     OLED    7680x4320 (8K)                    40-120Hz                 No                   HDMI  445.51 or newer
160  G-SYNC Compatible          MSI   MAG251RX  Yes     24.5      IPS   1920x1080 (FHD)                    48-240Hz                 No           Display Port  441.66 or newer
161  G-SYNC Compatible        Razer  Raptor 27  Yes       27      IPS   2560x1440 (QHD)                    48-144Hz                 No           Display Port  431.60 or newer
162  G-SYNC Compatible      Samsung       CRG5   No       27       VA   1920x1080 (FHD)                    48-240Hz                 No           Display Port  430.86 or newer
163  G-SYNC Compatible    ViewSonic      XG270   No       27      IPS   1920x1080 (FHD)                    48-240Hz                 No           Display Port  441.41 or newer

[164 rows x 11 columns]

【讨论】:

  • 感谢您的回答。你能更详细地解释每个部分的作用吗?对不起,如果它可能是微不足道的,但我不太了解 html 和 json 的东西。
猜你喜欢
  • 2019-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-22
  • 1970-01-01
  • 2017-12-22
  • 2022-01-24
相关资源
最近更新 更多