Home > Software engineering >  how to get text from li tag and strong tag seperately?
how to get text from li tag and strong tag seperately?

Time:03-07

how to get text from li tag and strong tag seperately using bs4 python

my code till now

from bs4 import BeautifulSoup as bs
import requests

url = "https://www.modernlibrary.com/top-100/100-best-novels/"
response = requests.get(url)

soup = bs(response.content,'lxml')
data = soup.find('div',"list-100").find('ol')
new = data.find_all('div',"row")
for i in new:
    f = i.find('strong')
    g = i.find('li')
    print(g)


the output is <li ><strong>BRIDESHEAD REVISITED</strong> by Evelyn Waugh</li>

CodePudding user response:

You can apply split method to get the desired result:

from bs4 import BeautifulSoup as bs
import requests

url = "https://www.modernlibrary.com/top-100/100-best-novels/"
response = requests.get(url)

soup = bs(response.content,'lxml')
#data = soup.find('div',"list-100").find('ol')
new = soup.select('div.row > li')
for i in new:
  j=i.get_text().split('by')
  name=j[0]
  author = j[1]

  print(f'Name: {name} Author:{author}')

Output:

Name: ULYSSES  Author: James Joyce
Name: THE GREAT GATSBY  Author: F. Scott Fitzgerald
Name: A PORTRAIT OF THE ARTIST AS A YOUNG MAN  Author: James Joyce
Name: LOLITA  Author: Vladimir Nabokov
Name: BRAVE NEW WORLD  Author: Aldous Huxley
Name: THE SOUND AND THE FURY  Author: William Faulkner
Name: CATCH-22  Author: Joseph Heller
Name: DARKNESS AT NOON  Author: Arthur Koestler
Name: SONS AND LOVERS  Author: D.H. Lawrence
Name: THE GRAPES OF WRATH  Author: John Steinbeck
Name: UNDER THE VOLCANO  Author: Malcolm Lowry
Name: THE WAY OF ALL FLESH  Author: Samuel Butler
Name: 1984  Author: George Orwell
Name: I, CLAUDIUS  Author: Robert Graves
Name: TO THE LIGHTHOUSE  Author: Virginia Woolf
Name: AN AMERICAN TRAGEDY  Author: Theodore Dreiser
Name: THE HEART IS A LONELY HUNTER  Author: Carson McCullers      
Name: SLAUGHTERHOUSE-FIVE  Author: Kurt Vonnegut
Name: INVISIBLE MAN  Author: Ralph Ellison
Name: NATIVE SON  Author: Richard Wright
Name: HENDERSON THE RAIN KING  Author: Saul Bellow
Name: APPOINTMENT IN SAMARRA  Author: John O’Hara
Name: U.S.A.(trilogy)  Author: John Dos Passos
Name: WINESBURG, OHIO  Author: Sherwood Anderson
Name: A PASSAGE TO INDIA  Author: E.M. Forster
Name: THE WINGS OF THE DOVE  Author: Henry James
Name: THE AMBASSADORS  Author: Henry James
Name: TENDER IS THE NIGHT  Author: F. Scott Fitzgerald
Name: THE STUDS LONIGAN TRILOGY  Author: James T. Farrell
Name: THE GOOD SOLDIER  Author: Ford Madox Ford
Name: ANIMAL FARM  Author: George Orwell
Name: THE GOLDEN BOWL  Author: Henry James
Name: SISTER CARRIE  Author: Theodore Dreiser
Name: A HANDFUL OF DUST  Author: Evelyn Waugh
Name: AS I LAY DYING  Author: William Faulkner
Name: ALL THE KING’S MEN  Author: Robert Penn Warren
Name: THE BRIDGE OF SAN LUIS REY  Author: Thornton Wilder
Name: HOWARDS END  Author: E.M. Forster
Name: GO TELL IT ON THE MOUNTAIN  Author: James Baldwin
Name: THE HEART OF THE MATTER  Author: Graham Greene
Name: LORD OF THE FLIES  Author: William Golding
Name: DELIVERANCE  Author: James Dickey
Name: A DANCE TO THE MUSIC OF TIME (series)  Author: Anthony Powell
Name: POINT COUNTER POINT  Author: Aldous Huxley
Name: THE SUN ALSO RISES  Author: Ernest Hemingway
Name: THE SECRET AGENT  Author: Joseph Conrad
Name: NOSTROMO  Author: Joseph Conrad
Name: THE RAINBOW  Author: D.H. Lawrence
Name: WOMEN IN LOVE  Author: D.H. Lawrence
Name: TROPIC OF CANCER  Author: Henry Miller
Name: THE NAKED AND THE DEAD  Author: Norman Mailer
Name: PORTNOY’S COMPLAINT  Author: Philip Roth
Name: PALE FIRE  Author: Vladimir Nabokov
Name: LIGHT IN AUGUST  Author: William Faulkner
Name: ON THE ROAD  Author: Jack Kerouac
Name: THE MALTESE FALCON  Author: Dashiell Hammett
Name: PARADE’S END  Author: Ford Madox Ford
Name: THE AGE OF INNOCENCE  Author: Edith Wharton
Name: ZULEIKA DOBSON  Author: Max Beerbohm
Name: THE MOVIEGOER  Author: Walker Percy
Name: DEATH COMES FOR THE ARCHBISHOP  Author: Willa Cather
Name: FROM HERE TO ETERNITY  Author: James Jones
Name: THE WAPSHOT CHRONICLES  Author: John Cheever
Name: THE CATCHER IN THE RYE  Author: J.D. Salinger
Name: A CLOCKWORK ORANGE  Author: Anthony Burgess
Name: OF HUMAN BONDAGE  Author: W. Somerset Maugham
Name: HEART OF DARKNESS  Author: Joseph Conrad
Name: MAIN STREET  Author: Sinclair Lewis
Name: THE HOUSE OF MIRTH  Author: Edith Wharton
Name: THE ALEXANDRIA QUARTET  Author: Lawrence Durrell
Name: A HIGH WIND IN JAMAICA  Author: Richard Hughes
Name: A HOUSE FOR MR BISWAS  Author: V.S. Naipaul
Name: THE DAY OF THE LOCUST  Author: Nathanael West
Name: A FAREWELL TO ARMS  Author: Ernest Hemingway
Name: SCOOP  Author: Evelyn Waugh
Name: THE PRIME OF MISS JEAN BRODIE  Author: Muriel Spark
Name: FINNEGANS WAKE  Author: James Joyce
Name: KIM  Author: Rudyard Kipling
Name: A ROOM WITH A VIEW  Author: E.M. Forster
Name: BRIDESHEAD REVISITED  Author: Evelyn Waugh
Name: THE ADVENTURES OF AUGIE MARCH  Author: Saul Bellow
Name: ANGLE OF REPOSE  Author: Wallace Stegner
Name: A BEND IN THE RIVER  Author: V.S. Naipaul
Name: THE DEATH OF THE HEART  Author: Elizabeth Bowen
Name: LORD JIM  Author: Joseph Conrad
Name: RAGTIME  Author: E.L. Doctorow
Name: THE OLD WIVES’ TALE  Author: Arnold Bennett
Name: THE CALL OF THE WILD  Author: Jack London
Name: LOVING  Author: Henry Green
Name: MIDNIGHT’S CHILDREN  Author: Salman Rushdie
Name: TOBACCO ROAD  Author: Erskine Caldwell
Name: IRONWEED  Author: William Kennedy
Name: THE MAGUS  Author: John Fowles
Name: WIDE SARGASSO SEA  Author: Jean Rhys
Name: UNDER THE NET  Author: Iris Murdoch
Name: SOPHIE’S CHOICE  Author: William Styron
Name: THE SHELTERING SKY  Author: Paul Bowles
Name: THE POSTMAN ALWAYS RINGS TWICE  Author: James M. Cain
Name: THE GINGER MAN  Author: J.P. Donleavy
Name: THE MAGNIFICENT AMBERSONS  Author: Booth Tarkington
  • Related