Issue
I'm trying to search for specific data on a page. I'm able to gather all the data from the page with:
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser') #html.parser')
results = soup.find_all('tr', {'class':"parent"})
It provides a number of lines that look like the following:
<tr class="parent" data-row='{"call":{"symbol":"BTCC 231215C7.00","class_symbol":"BTCC","root_symbol":"BTCC","underlying_symbol":"BTCC.B","expiry_date":"2023-12-15","strike_price":7,"instrument_type":0,"last_price":0.15,"volume":0,"bid_price":0.11,"ask_price":0.15,"bid_size":25,"ask_size":50,"net_change":0,"settlement_price":0,"open_interest":235,"is_option":1,"is_weekly":0,"last_close_price":0.15,"open_price":0,"high_price":0,"low_price":0,"nb_trades":0,"volatility":36.842},"put":{"symbol":"BTCC 231215P7.00","class_symbol":"BTCC","root_symbol":"BTCC","underlying_symbol":"BTCC.B","expiry_date":"2023-12-15","strike_price":7,"instrument_type":0,"last_price":2.52,"volume":0,"bid_price":2.08,"ask_price":2.32,"bid_size":25,"ask_size":25,"net_change":0,"settlement_price":0,"open_interest":0,"is_option":1,"is_weekly":0,"last_close_price":2.52,"open_price":0,"high_price":0,"low_price":0,"nb_trades":0,"volatility":71.286}}' id="BTCC-20231215-700" tabindex="0">
<td class="expiry_date" data-sort="2023-12-15">December 15, 2023</td>
<td class="call bid_price" data-sort="0.11">0.11</td>
<td class="call ask_price" data-sort="0.15">0.15</td>
<td class="call last_price" data-sort="0.15">0.15</td>
<td class="call net_change" data-sort="0">0</td>
<td class="call open_interest" data-sort="235">235</td>
<td class="call volume" data-sort="0">0</td>
<td class="strike_price" data-sort="7.0000">7.00</td>
<td class="put bid_price" data-sort="2.08">2.08</td>
<td class="put ask_price" data-sort="2.32">2.32</td>
<td class="put last_price" data-sort="2.52">2.52</td>
<td class="put net_change" data-sort="0">0</td>
<td class="put open_interest" data-sort="0">0</td>
<td class="put volume" data-sort="0">0</td>
</tr>
<tr class="parent" data-row='{"call":{"symbol":"BTCC 250117C5.00","class_symbol":"BTCC","root_symbol":"BTCC","underlying_symbol":"BTCC.B","expiry_date":"2025-01-17","strike_price":5,"instrument_type":0,"last_price":1.05,"volume":20,"bid_price":0.86,"ask_price":1.13,"bid_size":17,"ask_size":10,"net_change":0.17,"settlement_price":0,"open_interest":832,"is_option":1,"is_weekly":0,"last_close_price":0.88,"open_price":1.05,"high_price":1.06,"low_price":1.05,"nb_trades":3,"volatility":26.595},"put":{"symbol":"BTCC 250117P5.00","class_symbol":"BTCC","root_symbol":"BTCC","underlying_symbol":"BTCC.B","expiry_date":"2025-01-17","strike_price":5,"instrument_type":0,"last_price":1.49,"volume":0,"bid_price":1.13,"ask_price":1.38,"bid_size":10,"ask_size":17,"net_change":0,"settlement_price":0,"open_interest":678,"is_option":1,"is_weekly":0,"last_close_price":1.49,"open_price":0,"high_price":0,"low_price":0,"nb_trades":0,"volatility":62.77}}' id="BTCC-20250117-500" tabindex="0">
<td class="expiry_date" data-sort="2025-01-17">January 17, 2025</td>
<td class="call bid_price" data-sort="0.86">0.86</td>
<td class="call ask_price" data-sort="1.13">1.13</td>
<td class="call last_price" data-sort="1.05">1.05</td>
<td class="call net_change up" data-sort="0.17">0.17</td>
<td class="call open_interest" data-sort="832">832</td>
<td class="call volume" data-sort="20">20</td>
<td class="strike_price" data-sort="5.0000">5.00</td>
<td class="put bid_price" data-sort="1.13">1.13</td>
<td class="put ask_price" data-sort="1.38">1.38</td>
<td class="put last_price" data-sort="1.49">1.49</td>
<td class="put net_change" data-sort="0">0</td>
<td class="put open_interest" data-sort="678">678</td>
<td class="put volume" data-sort="0">0</td>
</tr>
However I'm unable to select the specific line.
I tried to get data from line so tried to narrow it down using the following code:
for result in results:
name = result.find('tr', attrs={'symbol':"BTCC 250117C5.00"})
unfortunately it doesn't find the line I'm looking for :(
Once the line is selected I would like to retrieve the "bid_price" and "ask_price" for that specific symbol.
Solution
You can select <tr>
that contains the symbol in data-row=
attribute. Then select the correct cells:
# html_doc contains the HTML snippet from the question
soup = BeautifulSoup(html_doc, 'html.parser')
row = soup.select_one('tr[data-row*="BTCC 250117C5.00"]')
bid_price = row.select_one('.bid_price').text
ask_price = row.select_one('.ask_price').text
print(f'{bid_price=} {ask_price=}')
Prints:
bid_price='0.86' ask_price='1.13'
Answered By - Andrej Kesely
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.