首页 > 解决方案 > 使用python抓取html表

问题描述

我正在尝试从网站上抓取表格,但我得到了 NULL。

我怎样才能拿到桌子?我究竟做错了什么?

import requests
from bs4 import BeautifulSoup

html = "https://traderslounge.in/implied-volatility-rank-nse-fno-stocks/" #link that has to be scrapped

response = requests.get(url) # before we feed it to request to parse 

response.status_code
soup = BeautifulSoup(response.text, 'html.parser')

table = soup.find_all("th")
list_of_rows = []
for row in table.findAll("td"):
    list_of_cells = []
    for cell in row.findAll(["th","td"]):
        text = cell.text
        print(text)
        list_of_cells.append(text)
        list_of_rows.append(list_of_cells)

for item in list_of_rows:
    print(' '.join(item))

标签: pythonhtmlweb-scraping

解决方案


该站点的表格内容是从外部 API 检索的:

https://traderslounge.in/FNO/ivrank/ivranktable.txt

您可以使用以下方法获得结果:

import requests

r = requests.get('https://traderslounge.in/FNO/ivrank/ivranktable.txt')

print(r.json()["data"])

推荐阅读