python - 使用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))
解决方案
该站点的表格内容是从外部 API 检索的:
https://traderslounge.in/FNO/ivrank/ivranktable.txt
您可以使用以下方法获得结果:
import requests
r = requests.get('https://traderslounge.in/FNO/ivrank/ivranktable.txt')
print(r.json()["data"])
推荐阅读
- java - MongoDB 动态行号创建
- reactjs - 在 props.history.push 获取 react typescript 和 react router dom 的克隆错误
- javascript - 当用户使用网络浏览器时,如何使用 react native 获取用户访问的所有 URL?
- android - 安卓开发教程。Linux 上的 Android Studio。启动模拟器崩溃或挂起,然后冻结计算机,我需要硬重启
- php - 按存在于另一个数组或集合中的属性值对集合进行排序
- laravel-migrations - 当 create table 语句中存在 autoIncrement、Unique key 和 Forgein key 时,Laravel 迁移失败
- python - 如何在列标题末尾查找和替换子字符串
- c - 在 C 中打开多个目录
- json - 如何以可扩展的方式使用 PostgreSQL 用模式表示 JSON 数据?
- c# - Is there a way to use a class as an array? (Unity C#)