首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在美丽的汤中从桌子上提取标签

如何在美丽的汤中从桌子上提取标签
EN

Stack Overflow用户
提问于 2022-05-03 14:53:03
回答 1查看 294关注 0票数 1

这是我的漂亮汤代码。

代码语言:javascript
复制
  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]

输出

得到td后,我想访问'a‘标签,并从'a’标签中提取标题。由于没有类或id,我如何从'a‘标签中获得标题?

愿望输出:

两个城市的故事

智人:人类简史

还有所有..。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-03 15:20:02

它们是title的属性值。所以您可以调用.get('title')来获取该数据点。

代码语言:javascript
复制
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)

输出:

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72101322

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档