- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在解析一个包含许多超链接的网页。能够使用 beautifulsoup 通过查找 <a>
中的所有“href”来提取超链接标签。接下来,我试图获取所有这些链接的“标题”,这些链接显示为 <a>
的标签内容。标签。 [HTML 新手,我认为这个尾随描述称为标签内容,对吧?] 下面是一个示例。字符串“哪种教室环境适合您?”需要提取。感谢您的帮助。
<a href="https://www.nytimes.com/video/education/100000005108366/classroom-waldorf-montessori-public.html">
Which Classroom Setting Suits You?
</a>
代码
import requests
from bs4 import BeautifulSoup
import sys
import codecs
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
url = 'https://www.nytimes.com'
r = requests.get(url)
html_as_string = r.text
soup = BeautifulSoup(html_as_string, 'html.parser')
for link in soup.find_all('a'):
print soup.a.string ##Prints "LEARN MORE"
print soup.a.text ##Prints "LEARN MORE"
print (link.get('text'))##prints "None"
最佳答案
代码:
from bs4 import BeautifulSoup
data = '''
<a href="https://www.nytimes.com/video/education/100000005108366/classroom-waldorf-montessori-public.html">
Which Classroom Setting Suits You?
</a>
'''
bs = BeautifulSoup(data, 'html.parser')
href = bs.find('a').text
print(href)
输出:
Which Classroom Setting Suits You?
这是我对问题进行第二次编辑后的代码:目前,该脚本从网站中提取所有 href 链接并打印其文本。您需要优化脚本以专注于您想要解析的特定 href 链接。
这是我对问题进行第二次编辑后的代码:
import requests
from bs4 import BeautifulSoup
url = 'https://www.nytimes.com'
r = requests.get(url)
html_as_string = r.text
soup = BeautifulSoup(html_as_string, 'html.parser')
for link in soup.find_all('a'):
print link.text
输出:
LEARN MORE »
Skip to content
Skip to navigation
中文 (Chinese)
Español
Today’s Paper
Video
World
U.S.
Politics
N.Y.
Business
Business
Opinion
Opinion
Tech
Science
Health
Sports
Sports
Arts
Arts
Style
Style
Food
Food
Travel
Magazine
T Magazine
Real Estate
Trump Allows for Cheaper, Less Regulated Health Plans
Comments
1
Open the door to relaxing A.C.A. rules on coverage.
2
Stop sending department officials to enrollment events.
3
Shut down the A.C.A. website for periods of time.
4
Cut funding to groups that help people enroll.
5
Slash spending on advertising and promotion.
Here Are 11 Ways Trump Is Undermining Obamacare
Trump Warns Puerto Rico: Federal Help Can’t Stay ‘Forever’
Comments
Obliterated: One of the Few Tropical Rain Forests in the U.S.
U.S. Will Withdraw From Unesco, Citing ‘Anti-Israel Bias’
What’s It Like to Cover Trump? Our Reporters Answer Questions at 6:30 P.M. Eastern
California Fires Burn ‘Faster Than Firefighters Can Run’
Comments
‘Thank God We Left’: Californians Describe Escapes
Satellite Images Show 1,800 Buildings Destroyed by Fire in Santa Rosa
The Daily 360
1:32
Sifting Through the Charred Ruins
More in
The Daily 360 »
Weinstein Company Has Been Aware of Payouts Since 2015
Comments
Facing Down the Network That Protected Weinstein
Comments
How Did NBC Miss Out on a Weinstein Exposé?
Palestinian Factions Sign Deal Paving Way for Joint Rule
Comments
The Bronx’s Quiet, Brutal War With Opioids
Comments
California Today: A Rising Death Toll From Wildfires
Comments
Your Friday Briefing
Audio
Listen to ‘The Daily’
Apple Podcasts
Radio Public
Stitcher
Smarter Living
What to Know About Having a Microwedding
How Would That Couch Look at Home? Check Your Phone
Comments
Journalism that matters. More essential than ever.
Subscribe to the Times
From the Magazine
Philip Pullman Returns to His Fantasy World
Comments
U.S. Woman and Family Freed From Afghan Militants
Comments
South Carolina Sidesteps the Law, and the Poor Go to Jail
Comments
Protesters in N.F.L. May Have an Ally: Labor Law
Comments
Different Ads, Different Ethnicities, Same Car
Genes for Skin Color Rebut Notions of Race, Study Finds
Comments
Citing Supreme Court, Judge Questions Menendez Case
Comments
Leader of Philippines Threatens to Expel E.U. Diplomats
Qatari Businessman Accused of Bribing Ex-FIFA Chief
Yankees Complete Comeback From 0-2 to Reach A.L.C.S.
Seychelles Reports a Case of Plague
Fed Still Puzzled by Inflation, but Rate Increase Is on Track
Morning Briefing: Australia Edition
Morning Briefing: Asia Edition
Morning Briefing: Europe Edition
California Today
Morning Briefing
Got a confidential news tip?
Learn more
Aussie Rules: Readers Respond
Vegemite, the Beloved Australian Spread, Goes Upmarket
Australia’s New Offer to Refugees: A Different Detention Center
She Is New Yorkier Than You
Olympic Tales From a 105-Year-Old Baron
Art Review: Collecting Strokes of Genius
Opinion
Mr. Trump, I Live in South Korea. You’re Scaring Me.
The Trumps, The Poodle, The Sex Scandal
Comments
Blow: Trump’s War Games
Kristof: North Korea Fears
Leonhardt: Falling Behind
Rosenthal: Trump’s Latest Outrage Against Puerto Rico
Stephens: Weinstein Enablers
Texas Should Not Execute Robert Pruett Tonight
Editorial: Trump Alone Can Order a Nuclear Strike
Edsall: Democrats Play Checkers, Trump Plays Chess
Greenhouse: Church Over State on Contraception
Has America Forgotten the Virgin Islands?
Congress’s Un-American, Bad-for-Israel Agenda
From Amazon, a Change That Hurts Authors
Ben Shapiro’s Hollow Bravery
What Makes Eminem’s Anti-Trump Rap Different
Getting Help Where It’s Needed in Puerto Rico
New York Times Recipe Commenters (Politely) Spill Their Guts
Smelly and Dirty and, in Bombing Trial, Suddenly a Star
Times Insider »
New York Times Recipe Commenters (Politely) Spill Their Guts
The Crossword »
Play Today’s Puzzle
The Crossword »
Play Today’s Puzzle
Wordplay »
timesvideo
explore all videos »
Television
‘Dynasty’ or ‘Dallas’? Squaring Off
Opinion
You Are Special! Now Stop Being Different
Science
Mountain Lions Sometimes Share Food
Food
Our Best Pizza Recipes
Opinion
Getting Help Where It’s Needed in Puerto Rico
Mind
High Blood Pressure Tied to Later Dementia
Travel
What Martha Stewart Can’t Travel Without
Opinion
Publishing’s Unfair Gray Market
Live
How to Stop Bleeding and Save a Life
Television
Review: Netflix’s ‘Mindhunter’
Opinion
Has America Forgotten the Virgin Islands?
N.Y. / Region
Lawsuits Seek Access to College Websites
World »
For Dignity and Development, East Africa Curbs Used Clothes Imports
Tech and Real Estate Lift the Fortunes of China’s Richest
Palestinian Factions, Hamas and Fatah, Sign Unity Deal
Business Day »
Sheryl Sandberg Says Facebook Will Hand Over More Data on Russia-Linked Ads
Different Ads, Different Ethnicities, Same Car
Kobe Steel Problems May Be More Widespread, Raising Fears on High-Speed Rail
Opinion »
Linda Greenhouse: On Contraception, It’s Church Over State
Editorial: Mr. Trump Alone Can Order a Nuclear Strike. Congress Can Change That.
Charles M. Blow: Trump’s War Games
U.S. »
We’re Tracking Ways Trump Is Undermining Obamacare. Here Are 11.
Another Victim of Hurricane Maria: Puerto Rico’s Treasured Rainforest
California Wildfires Burn ‘Faster Than Firefighters Can Run’
Technology »
Sheryl Sandberg Says Facebook Will Hand Over More Data on Russia-Linked Ads
State of the Art: The Frightful Five Want to Rule Entertainment. They Are Hitting Limits.
The Shift: We Asked Facebook 12 Questions About the Election, and Got 5 Answers
Arts »
Critic’s Notebook: Harvey Weinstein Is Gone. But Hollywood Still Has a Problem.
Rose McGowan’s Twitter Account Locked After Posts About Weinstein
‘Dynasty’ or ‘Dallas’? Squaring Off in the Lily Pond
Politics »
Foiled in Congress, Trump Signs Order to Undermine Obamacare
U.S. Will Withdraw From Unesco, Citing Its ‘Anti-Israel Bias’
Trump’s Warning on Limits to U.S. Aid in Puerto Rico Adds ‘Insult to Injury,’ Mayor Says
Fashion & Style »
Unbuttoned: Fashion’s Gossip Addiction
With...Tom Hanks: Hollywood’s Most Decent Fella on Weinstein, Trump and History
Social Q’s: What Happens When a Will is Unsigned? (It’s Not Fun)
Movies »
All the Films of Studio Ghibli, Ranked
Critic’s Notebook: Harvey Weinstein Is Gone. But Hollywood Still Has a Problem.
Rose McGowan’s Twitter Account Locked After Posts About Weinstein
New York »
The Bronx’s Quiet, Brutal War With Opioids
Cy Vance Defends Decision Not to Pursue Case Against Harvey Weinstein
College Websites Must Accommodate Disabled Students, Lawsuits Say
Sports »
Yankees 5, Indians 2 | Yankees win series, 3-2: Didi Gregorius Leads Yankees Past Indians and Into A.L.C.S.
P.S.G.’s Nasser al-Khelaifi Accused of Bribing Ex-FIFA Chief Valcke
N.F.L. Week 6 Picks: Chiefs Over Steelers; Packers Over Vikings
Theater »
‘Brilliant,’ 41 and Lost to AIDS: The Theater World Asks Why
She Was About to Quit Acting. Then She Got the Role of a Lifetime.
Review: A Richard III Who Steps Straight Out of the Land of Id
Science »
Matter: Genes for Skin Color Rebut Dated Notions of Race, Researchers Say
ScienceTake: How Kangaroo Rats Escape Rattlesnakes
Trilobites: Solitary Pumas Turn Out to Be Mountain Lions Who Lunch
Obituaries »
Vincent La Selva, Dauntless Founder of a People’s Opera Troupe, Dies at 88
Michel Jouvet, Who Unlocked REM Sleep’s Secrets, Dies at 91
Y.A. Tittle, Quarterback Who Led Giants to 3 Title Games, Dies at 90
Television »
‘Dynasty’ or ‘Dallas’? Squaring Off in the Lily Pond
Review: Netflix’s ‘Mindhunter’ Is More Chatter Than Splatter
Ask a Showrunner: ‘Mr. Robot’ Season 3 Premiere: Sam Esmail on Parallel Universes and President Trump
Health »
In New Test for Obamacare, Iowa Seeks to Abandon Marketplace
Seychelles Reports a Case of Plague
Phys Ed: What Half-Marathons Teach Us About Running a Marathon
Travel »
36 Hours: 36 Hours in Aarhus, Denmark
Personal Journeys: Seeking Solitude in Japan’s Mountain Monasteries
Carry-On: What Martha Stewart Can’t Travel Without
Books »
American Beauties: Riding Shotgun on Martha Gellhorn’s Brave and Comic Adventures
Books of The Times: In Ron Chernow’s ‘Grant,’ an American Giant’s Makeover Continues
Feature: Philip Pullman Returns to His Fantasy World
Education »
On Campus: Campus Hate Lives on the Internet. Administrators Need to Catch Up.
Which Classroom Setting Suits You?
Your Money: A Game to Help Students Pay the Right Price for College
Food »
The Pour: Five Common Wine Myths, Debunked
Hungry City: Traditional Ethiopian Food in a Cosmopolitan Setting
Mike Isabella, Washington’s Restless Restaurateur
Magazine »
Feature: Philip Pullman Returns to His Fantasy World
Feature: In Northern Minnesota, Two Economies Square Off: Mining vs. Wilderness
Letter of Recommendation: Letter of Recommendation: Dunking
Real Estate »
The Hunt: Seeking the Peace of the Suburbs
On the Market: Homes for Sale in New York and Connecticut
On the Market: Homes for Sale in New York City
The Upshot »
A Bump Stock Ban Is Popular With the Public. But Experts Have Their Doubts.
The N.F.L. Is Now One of the Most Divisive Brands in the U.S.
Quality of Mercy: The Word May Be Toxic, but Amnesty Is Everywhere
Times Insider »
Why Tech is Starting to Make Me Uneasy
Times Insider: New York Times Recipe Commenters (Politely) Spill Their Guts
Smelly and Dirty and, in Bombing Trial, Suddenly a Star
Automobiles »
Wheels: For Electric Car Owners, ‘Range Anxiety’ Gives Way to ‘Charging Time Trauma’
Kobe Steel’s Falsified Data Is Another Blow to Japan’s Reputation
Different Ads, Different Ethnicities, Same Car
T Magazine »
Home and Work: A Paris Boudoir Where Everything’s Meant to Be Touched
Slinky Dresses, Meet Slouchy Trousers
The Illustrated Interview: Gloria Vanderbilt
Real Estate »
International Rents Per Square Foot
Comments
Search for Homes for Sale or Rent
Mortgage Calculator
Most Emailed
Most Viewed
Trending
Recommended for you
Go to Home Page »
The New York Times
World
U.S.
Politics
N.Y.
Business
Tech
Science
Health
Sports
Education
Obituaries
Today's Paper
Corrections
Today's Opinion
Op-Ed Columnists
Editorials
Op-Ed Contributors
Letters
Sunday Review
Video: Opinion
Today's Arts
Art & Design
Books
Dance
Movies
Music
N.Y.C. Events Guide
Television
Theater
Video: Arts
Automobiles
Crossword
Food
Education
Fashion & Style
Health
Jobs
Magazine
N.Y.C. Events Guide
Real Estate
T Magazine
Travel
Weddings & Celebrations
Reader Center
Classifieds
Tools & Services
N.Y.C. Events Guide
Multimedia
Photography
Video
NYT Store
Times Journeys
Subscribe
Manage My Account
NYTCo
Home Delivery
Digital Subscriptions
Crossword
Email Newsletters
Alerts
Gift Subscriptions
Corporate Subscriptions
Education Rate
Mobile Applications
Replica Edition
© 2017 The New York Times Company
Contact Us
Work With Us
Advertise
Your Ad Choices
Privacy
Terms of Service
Terms of Sale
Site Map
Help
Site Feedback
Subscriptions
View Mobile Version
关于python - 使用 BeautifulSoup 解析 HTML 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46714375/
大家好, 我看到了来自 java 项目中的 jsp 页面。 想问一下这些html标签有什么区别。 请多多指教。 示例代码如下: 最佳答案 使用struts-html标签库,其中只是普
我有一个页面,我正在从电子邮件中读取 HTML。 有时,来自电子邮件的文本包含 HTML 和 CSS,它完全改变了我的页面样式。 我不希望我的页面样式因此受到影响。我如何严格阅读特定 div(框)内的
我知道有类似的问题,但我想对我的特定代码进行一些输入。 我有一个图像,我将其切成 9 块,并创建了一个 3x3 HTML 表来显示它。 但是我的表在行之间有空格,但在列之间没有空格。我没有使用任何 C
编辑:Waylan 的回答成功了!谢谢! 我正在尝试压缩文档的 .html 文件以发送给客户。目标是获得与浏览实际网站相同的体验。 打开 .html 文件时,单击的任何链接都会转到父文件夹,而不是特定
编辑:Waylan 的回答成功了!谢谢! 我正在尝试压缩文档的 .html 文件以发送给客户。目标是获得与浏览实际网站相同的体验。 打开 .html 文件时,单击的任何链接都会转到父文件夹,而不是特定
这是 question 的扩展.我正在尝试解析嵌入在 Blogger 博客的 XML 备份中的 HTML 片段,并用 InDesign 标签重新标记它们。 Blogger 并未对其任何帖子的 HTML
我知道在 html 中元素之间的换行符被视为空格,但我认为当您尝试使用响应式布局时这非常可怕。 例如,这里我们有预期和正确的行为,但要获得它,我必须删除元素之间的 html 中的换行符: https:
我正在尝试将文本文件显示为 html。我正在使用 ionic 。我正在发送一个 html 格式的响应,但在一个文本文件中发送到配置文件页面。它在 .ts 页面的变量名中。 @Component({
假设我有一个 html 文档: test 我想在浏览器中显示该代码。然后我会创建类似的东西: <html>test<html> 为了在中间制作 gubbins,我有一个函数
HTML 元素和 HTML 标签有什么区别?渲染有什么区别吗?使用标签或元素时有什么特殊注意事项吗? 最佳答案 是一个标签,特别是一个开始标签 也是一个标签,一个结束标签 This is a para
我有这个表格的模态形式。该表正在填充大量数据,但我不想分页。相反,我想以模式形式降低表格的高度并为表格添加溢出。下面是我的代码,但它不起作用。 请问我该如何实现? CSS #table{
我记得有一个 Linux 命令可以从给定的 URL 返回 HTML 代码。您可以将 URL 作为此命令的参数,然后返回 HTML 代码,而不是在浏览器中输入 URL。 哪个命令执行此操作? 最佳答案
我有一个 html 页面,我想在其中包含另一个有很多链接的 html 页面。我能够使用 iframe 实现它,但我希望 iframe 内的页面具有与原始页面相同的文本和链接颜色属性,我不想要滚动条,我
我正在使用 HTML 写一本书。如果我把它写在一个 html 文件中,整个代码就会变长,所以我想将每一章保存到不同的文件中,然后将它们加载到主 html 中。我的意思是有像 chapter1.html
在显示之前,我必须将一个网站重定向到另一个网站。我试过使用 .htaccess,但它给我带来了问题。我也使用过 javavscript 和 meta,但在加载我要从中传输的页面之前它不起作用。帮助?
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
如何打印“html”标签,包括“”?如何在不使用文本区域和 Javascript 的情况下对任何标签执行此操作? 最佳答案 使用HTML character references : <html
我需要将 Ruby on Rails 应用程序中的 html.slim 文件转换为 html.erb。有什么简单的方法吗?我尝试了 Stack Overflow 和其他网站中列出的许多选项。但对我没有
这个问题在这里已经有了答案: Is it necessary to write HEAD, BODY and HTML tags? (6 个答案) 关闭 8 年前。 我在 gitHub 上找到了这个
如果不允许通过 JavaScript 进行额外的 DOM 操作,我正在寻找可以加载外部资源的元素列表。我正在尝试使用 HTML 查看器托管来自第三方的电子邮件,当发生这种情况时,我需要删除任何自动加载
我是一名优秀的程序员,十分优秀!