gpt4 book ai didi

Python Beautiful Soup 使用类解析表

转载 作者:行者123 更新时间:2023-12-01 07:04:02 27 4
gpt4 key购买 nike

我是 python 新手,尝试从页面中提取表格,但无法使用 BS4 找到该表格。你能告诉我我哪里出错了吗?

import requests
from bs4 import BeautifulSoup
website_url = requests.get('https://chartink.com/screener/copy-supertrend-negative-breakout-1103').content
soup = BeautifulSoup(website_url, 'html.parser')
print(soup.prettify())
My_table = soup.find('table',{'class':'table table-striped scan_results_table dataTable no-footer'})
My_table

最佳答案

该内容是通过返回 json 的 POST 请求动态添加的。它需要通过 cookie 和 header 进行身份验证。更简单的可能是使用 Selenium 。通过id获取元素并将其outerHTML传递给read_html以转换为漂亮的表格输出

from selenium import webdriver
import pandas as pd

d = webdriver.Chrome()
d.get('https://chartink.com/screener/copy-supertrend-negative-breakout-1103')
table = pd.read_html(d.find_element_by_id('DataTables_Table_0').get_attribute('outerHTML'))[0]
print(table)

关于Python Beautiful Soup 使用类解析表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58514877/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com