如何刮表并找出特定列中最大数对应的条目?

2024-06-25 06:09:33 发布

您现在位置:Python中文网/ 问答频道 /正文

如何从“https://www.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?symbolCode=-9999&symbol=BANKNIFTY&symbol=BANKNIFTY&instrument=OPTIDX&date=-&segmentLink=17&segmentLink=17”中刮表

然后找出“put”下的最大“OI”,最后在该行中为特定的最大OI设置相应的条目

打印行之前到达:

import urllib2
from urllib2 import urlopen
import bs4 as bs

url = 'https://www.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?symbolCode=-9999&symbol=BANKNIFTY&symbol=BANKNIFTY&instrument=OPTIDX&date=-&segmentLink=17&segmentLink=17'

html = urllib2.urlopen(url).read()
soup = bs.BeautifulSoup(html,'lxml')
table = soup.find('div',id='octable')
rows = table.find_all('tr')
for row in rows:
print row.text

Tags: httpsimportcomlivechainwwwurllib2symbol
1条回答
网友
1楼 · 发布于 2024-06-25 06:09:33

必须迭代<td>中的所有<tr>。您可以使用一堆for循环来实现这一点,但是使用list comprehension更简单。仅使用此项:

oi_column = [
    float(t[21].text.strip().replace('-','0').replace(',',''))
    for t in (t.find_all('td') for t in tables.find_all('tr'))
    if len(t) > 20
]

要迭代表的所有<td>中的所有<tr>,请仅选择包含20个以上项的行(不包括最后一行),并执行文本替换或任何与您的要求匹配的操作,此处将文本转换为浮动

整个代码是:

from bs4 import BeautifulSoup
import requests

url = 'https://www.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?symbolCode=-9999&symbol=BANKNIFTY&symbol=BANKNIFTY&instrument=OPTIDX&date=-&segmentLink=17&segmentLink=17'

response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")

tables = soup.find("table", {"id":"octable"})

oi_column = [
    float(t[21].text.strip().replace('-','0').replace(',',''))
    for t in (t.find_all('td') for t in tables.find_all('tr'))
    if len(t) > 20
]
#column to check
print(oi_column)

print("max value : {}".format(max(oi_column)))
print("index of max value : {}".format(oi_column.index(max(oi_column)))) 

#the row at index
root = tables.find_all('tr')[2 + oi_column.index(max(oi_column))].find_all('td')
row_items = [
    (
        root[1].text.strip(),
        root[2].text.strip()
        #etc... select index you want to extract in the corresponding rows
    )
]
print(row_items)

您可以找到另一个示例来废弃这样的表here

相关问题 更多 >