- 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/
我一直在使用 AJAX 从我正在创建的网络服务中解析 JSON 数组时遇到问题。我的前端是一个简单的 ajax 和 jquery 组合,用于显示从我正在创建的网络服务返回的结果。 尽管知道我的数据库查
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我在尝试运行 Android 应用程序时遇到问题并收到以下错误 java.lang.NoClassDefFoundError: com.parse.Parse 当我尝试运行该应用时。 最佳答案 在这
有什么办法可以防止etree在解析HTML内容时解析HTML实体吗? html = etree.HTML('&') html.find('.//body').text 这给了我 '&' 但我想
我有一个有点疯狂的例子,但对于那些 JavaScript 函数作用域专家来说,它看起来是一个很好的练习: (function (global) { // our module number one
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
我需要编写一个脚本来获取链接并解析链接页面的 HTML 以提取标题和其他一些数据,例如可能是简短的描述,就像您链接到 Facebook 上的内容一样。 当用户向站点添加链接时将调用它,因此在客户端启动
在 VS Code 中本地开发时,包解析为 C:/Users//AppData/Local/Microsoft/TypeScript/3.5/node_modules/@types//index而不是
我在将 json 从 php 解析为 javascript 时遇到问题 这是我的示例代码: //function MethodAjax = function (wsFile, param) {
我在将 json 从 php 解析为 javascript 时遇到问题 这是我的示例代码: //function MethodAjax = function (wsFile, param) {
我被赋予了将一种语言“翻译”成另一种语言的工作。对于使用正则表达式的简单逐行方法来说,源代码过于灵活(复杂)。我在哪里可以了解更多关于词法分析和解析器的信息? 最佳答案 如果你想对这个主题产生“情绪化
您好,我在解析此文本时遇到问题 { { { {[system1];1;1;0.612509325}; {[system2];1;
我正在为 adobe after effects 在 extendscript 中编写一些代码,最终变成了 javascript。 我有一个数组,我想只搜索单词“assemble”并返回整个 jc3_
我有这段代码: $(document).ready(function() { // }); 问题:FB_RequireFeatures block 外部的代码先于其内部的代码执行。因此 who
背景: netcore项目中有些服务是在通过中间件来通信的,比如orleans组件。它里面服务和客户端会指定网关和端口,我们只需要开放客户端给外界,服务端关闭端口。相当于去掉host,这样省掉了些
1.首先贴上我试验成功的代码 复制代码 代码如下: protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
什么是 XML? XML 指可扩展标记语言(eXtensible Markup Language),标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言。 你可以通过本站学习 X
【PHP代码】 复制代码 代码如下: $stmt = mssql_init('P__Global_Test', $conn) or die("initialize sto
在SQL查询分析器执行以下代码就可以了。 复制代码代码如下: declare @t varchar(255),@c varchar(255) declare table_cursor curs
前言 最近练习了一些前端算法题,现在做个总结,以下题目都是个人写法,并不是标准答案,如有错误欢迎指出,有对某道题有新的想法的友友也可以在评论区发表想法,互相学习🤭 题目 题目一: 二维数组中的
我是一名优秀的程序员,十分优秀!