簡體   English   中英

Python Beautiful Soup 使用 class 解析表

[英]Python Beautiful Soup parse a table using class

我是 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

soup.find('table') 給出以下內容

 <table style="margin: auto;">
<tbody>
<tr>
<td><a class="selected" href="">INK CHART :</a></td>
<td style="width: 220px;">
<input class="selector" id="searchbox" name="selector" onblur="clearTimeout(ctime)" onfocus="init()" onkeyup="refresh()" style="width: 100%" type="text"/>
<iframe border="0" frameborder="0" id="DivShim" style="display:none;position: absolute;"></iframe>
<div class="sbox" id="sbox" style="z-index: 10; ">
</div>
</td>
<td style="width: 120px;">
<select class="up" name="type" style="width: 100%">
<option value="can">Candle-Stick</option>
<option value="po">Point and Figure</option>
<option value="fundamentals">Fundamentals</option>
</select></td>
<td>
<input class="search_button" type="submit" value=""/>
</td>
</tr>
</tbody>
</table>

由於這不是您想要的,您需要使用 selenium 或 splash。 https://selenium-python.readthedocs.io/

該內容是通過返回 json 的 POST 請求動態添加的。 它需要通過 cookies 和標頭進行身份驗證。 更簡單的可能是使用 selenium。 通過 id 獲取元素並將其 outerHTML 傳遞給 read_html 以轉換為漂亮的表 output

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)

暫無
暫無

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

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