how to extract title of ;a' tag from a table (td) in beautifulsoup

Viewed 164

This is my beautifulsoup code.

  topics_url = 'https://www.goodreads.com/review/list/47437459?page=1&ref=nav_mybooks'
  response = requests.get(topics_url)
  page_content = response.text
  doc = BeautifulSoup(page_content, 'html.parser')
  table_title = doc.find_all('td', {'class' : 'field title'})
  table_title[:5]

output enter image description here

After getting td I want to access 'a' tag and extract the title from 'a' tag. Since there is no class or id how can I get title from 'a' tag?

desire output:

A tale of two cities

Sapiens: A Brief History of Humankind

and all....

1 Answers

They are attribute values of title . So you can invoke .get('title') to grab that data point

import requests
from bs4 import BeautifulSoup

topics_url = 'https://www.goodreads.com/review/list/47437459?page=1&ref=nav_mybooks'
response = requests.get(topics_url)
page_content = response.text
doc = BeautifulSoup(page_content, 'html.parser')
for table_title in doc.select('#books tbody tr'):
  t= table_title.select_one('td.field.title div a').get('title')
  print(t)

Output:

A Tale of Two Cities
Sapiens: A Brief History of Humankind 
Wings of Fire: An Autobiography       
Maktub
Mindset: The New Psychology of Success
The Travels of Ibn Battutah
After Dark
Norwegian Wood
Never Let Me Go
Why We Sleep: Unlocking the Power of Sleep and Dreams
Uttaradhikar
Behind the Beautiful Forevers: Life, Death, and Hope in a Mumbai Undercity
Cloud Atlas
Hillbilly Elegy: A Memoir of a Family and Culture in Crisis
Outliers: The Story of Success
The Black Swan: The Impact of the Highly Improbable
Related