HTML webpage getting element

Question:

Following is my script….

from bs4 import BeautifulSoup
import requests
import pandas as pd

page = requests.get("https://niftyinvest.com/max-pain/ULTRACEMCO?expiry=25JAN2023")

soup = BeautifulSoup(page.content, 'html.parser')
#print(soup.prettify)
all_tags = []
for element in soup.select('h6'):
    all_tags.append(element.text)

print(all_tags)

And I have following output….

[‘Max Painn for ULTRACEMCOn isn 7000 infon’, ‘7000 CE ‘, ‘n 159.90narrow_drop_down ni-iconn n-29.65n’, ‘Open Interest’, ‘139,700n arrow_drop_up ni-icon9,400n ‘, ‘7000 PE ‘, ‘154.90n arrow_drop_up ni-iconn 13.35’, ‘Open Interest’, ‘101,700n arrow_drop_up ni-iconn 23,100’, ‘What is Max Pain?’, ‘Calculating Max Pain’, ‘How can a trader benefit?’]

[Program finished]

Here I want output as…..

ULTRACEMCO 7000
(which is in very first line of my output)

Can anyone help plz…

Asked By: Python Learner

||

Answers:

Read the Python documentation of the sort method of a list. Check the contents of a after calling .sort().

sort(*, key: None = ..., reverse: bool = ...) -> None
sort(*, key: (int) -> SupportsRichComparison, reverse: bool = ...) -> None

Sort the list in ascending order and return None. The sort is in-place (i.e. the list itself is modified) and stable (i.e. the order of two equal elements is maintained). If a key function is given, apply it once to each list item and sort them, ascending or descending, according to their function values. The reverse flag can be set to sort in descending order.

If you want the sorted list is a separate, new list, do:

sorted_list = sorted(a)
Answered By: agtoever
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.