【问题标题】:Scraping the spot rate from the nse website从 nse 网站抓取即期汇率
【发布时间】:2021-02-01 05:40:18
【问题描述】:

我是网络抓取的新手,想从 NSE 抓取期权链(货币衍生品)表。我在这个问题中找到了类似的代码:Webscraping NSE Options prices using Python BeautifulSoup, regarding encoding correction 但它没有提取表格上方的值(69.4598)。我想要在图像中突出显示的这个值,但我不确定如何提取它并在 python 中对其进行编码。我附上了网页截图以及 google 检查元素窗口。

import requests
import pandas as pd
from bs4 import BeautifulSoup

Base_url = ("https://www.nseindia.com/live_market/dynaContent/live_watch/fxTracker/optChainDataByExpDates.jsp")

page = requests.get(Base_url)

soup = BeautifulSoup(page.content, 'html.parser')
table_it = soup.find_all(class_="opttbldata")
spot = soup.find(id = "td")
table_cls_1 = soup.find_all(id = "octable")
col_list = []

for mytable in table_cls_1:
    table_head = mytable.find('thead')

    try:
        rows = table_head.find_all('tr')
        for tr in rows:
            cols = tr.find_all('th')
            for th in cols:
                er = th.text
                ee = er.encode('utf8')
                col_list.append(ee)
    except:
        print('no thread')
        
col_list_fnl = [e for e in col_list if e not in ('CALLS', 'PUTS', 'Chart', '\xc2\xa0')]

table_cls_2 = soup.find(id = "octable")
all_trs = table_cls_2.find_all('tr')
req_row = table_cls_2.find_all('tr')

new_table = pd.DataFrame(index=range(0,len(req_row)-3),columns = col_list_fnl)

row_marker = 0

for row_number, tr_nos in enumerate(req_row):
    if row_number <= 1 or row_number == len(req_row)-1:
        continue # To insure we only choose non empty rows

    td_columns = tr_nos.find_all('td')

    # Removing the graph column
    select_cols = td_columns[1:22]
    cols_horizontal = range(0,len(select_cols))

    for nu, column in enumerate(select_cols):

        utf_string = column.get_text()
        utf_string = utf_string.strip('\n\r\t": ')
        tr = utf_string.encode('utf8')

        new_table.iloc[row_marker,[nu]] = tr

    row_marker += 1

print(new_table)

【问题讨论】:

    标签: python python-3.x web-scraping beautifulsoup


    【解决方案1】:

    您可以获取所有&lt;td&gt; 标签并遍历这些标签以查看包含子字符串'REFERENCE RATE - FBIL' 的位置。它实际上出现了 4 次,所以把它放到一个列表中,然后打印出元素。

    import requests
    import pandas as pd
    from bs4 import BeautifulSoup
    
    Base_url = ("https://www.nseindia.com/live_market/dynaContent/live_watch/fxTracker/optChainDataByExpDates.jsp")
    
    page = requests.get(Base_url)
    
    soup = BeautifulSoup(page.text, 'html.parser')
    tds = soup.find_all('td')
    
    refRates = [ each.find('strong').text for each in tds if 'REFERENCE RATE - FBIL' in each.text ]
    print (refRates[0])
    

    输出:

    print (refRates[0])
    69.4598
    

    【讨论】:

    • @DevanshiRuparel 如果解决了,请务必接受解决方案
    【解决方案2】:

    由于BeautifulSoup 具有使用伪选择器的能力,您始终可以利用这一优势使您的脚本更加健壮。查看下面的实现:

    import requests
    from bs4 import BeautifulSoup
    
    url = "https://www.nseindia.com/live_market/dynaContent/live_watch/fxTracker/optChainDataByExpDates.jsp"
    
    page = requests.get(url)
    soup = BeautifulSoup(page.text,'html.parser')
    item = soup.select_one("div:contains('REFERENCE RATE') > strong").text
    print(item)
    

    此时输出:

    69.4222
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-02
      • 1970-01-01
      • 1970-01-01
      • 2014-07-06
      • 2018-09-01
      相关资源
      最近更新 更多