全部版块 我的主页
论坛 提问 悬赏 求职 新闻 读书 功能一区 悬赏大厅 求助成功区
954 2
2016-11-30
悬赏 10 个论坛币 未解决
【作者(必填)】香蕉

【文题(必填)】PYTHON

【年份(必填)】2016

【全文链接或数据库名称(选填)】这个是我的代码 得到的结果在后面,但是不是我想要的,我只想要中间的 story-heading 的内容

比如
<h3 class="story-heading">
        A Wrenching Choice for Alaska Towns in the Path of Climate Change        </h3>


我只要A Wrenching Choice for Alaska Towns in the Path of Climate Change

代码该如何修改啊??!?!!?!?!?!?!? 求帮助!


def practice_v17():
        import requests
        from bs4 import BeautifulSoup
         
        base_url = 'http://www.nytimes.com'
        r = requests.get(base_url)
        soup = BeautifulSoup(r.text)

        for name in soup.find_all('h3'):
                if 'story-heading' in  name.get('class'):
                        print (name)


practice_v17()





<h3 class="story-heading">
        AustriaÔÇÖs Far Right Sees a Prize Within Reach: The Presidency        </h3>
<h3 class="story-heading">
        Despite Climate Change Vow, China Pushes to Dig More Coal        </h3>
<h3 class="story-heading">
        Frank Bruni: The Case for Mitt Romney        </h3>
<h3 class="story-heading">
        F.D.A. Agrees to New Trials for Ecstasy as Relief for PTSD Patients        </h3>
<h3 class="story-heading">
        Uber, Seeking to Expand, Defends Itself at EuropeÔÇÖs Highest Court        </h3>
<h3 class="story-heading">
        Third Place? In Auction World, Phillips Is Making Strides        </h3>
<h3 class="story-heading">
        After DemocratsÔÇÖ Losses, Nancy Pelosi Becomes a Symbol of What Went Wrong        </h3>
<h3 class="story-heading">
        On the Runway: Samantha Cameron, From 10 Downing Street to Selfridges        </h3>
<h3 class="story-heading">
        From ÔÇÿRogue OneÔÇÖ: A Rising Star in a Galaxy Far, Far Away        </h3>
<h3 class="story-heading">
        Domestic Abuse Killings Climb as Murders Drop, Frustrating New York Officials        </h3>
<h3 class="story-heading">
        On Pro Basketball: Russell Westbrook: The Triple-Double Machine        </h3>
<h3 class="story-heading">
        Beyond ÔÇÿHamilton,ÔÇÖ for Better and for Worse        </h3>
<h3 class="story-heading">
        A Wrenching Choice for Alaska Towns in the Path of Climate Change        </h3>
<h3 class="story-heading">
        Mark Taimanov, World-Class Chess and Piano Player, Dies at 90        </h3>
<h3 class="story-heading">
        Watched All of the ÔÇÿGilmore GirlsÔÇÖ Revival? LetÔÇÖs Talk About It        </h3>
<h3 class="story-heading">
        Why Gunshot Victims Have Reason to Like the Affordable Care Act        </h3>
<h3 class="story-heading">
        Skiing the Alps, Making It My Own        </h3>
<h3 class="story-heading">
        Books of The Times: Kathleen CollinsÔÇÖs ÔÇÿWhatever Happened to Interracial Love?ÔÇÖ        </h3>
<h3 class="story-heading">
        Professor Watchlist Is Seen as Threat to Academic Freedom        </h3>
<h3 class="story-heading">
        Restaurant Review: Filipino Food Worth the Wait at Bad Saint in Washington        </h3>
<h3 class="story-heading">
        Opinion: The Thin Gene        </h3>
<h3 class="story-heading">
        360 View: An Uncertain Future for East Village Rowhouses        </h3>
<h3 class="story-heading">
        Work-Life Balance: What Donald Trump Might Do for Working-Class Families        </h3>
<h3 class="story-heading">
        Feature: The End of the Anglo-American Order        </h3>
<h3 class="story-heading">
        Driven: Video Review: A New Porsche Cayman, and Still a DriverÔÇÖs Dream        </h3>
<h3 class="story-heading">
        Die Antwoord: The Real Zef Rappers of Beverly Hills        </h3>
<h3 class="story-heading">
        Decades in the Making: Fidel CastroÔÇÖs Obituary        </h3>

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2016-12-1 12:49:23
print(name.text)试试。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2016-12-4 01:15:52
利用正则表达式处理下载下来的网页源代码,即可
import re

string ='''<h3 class="story-heading">
        AustriaÔÇÖs Far Right Sees a Prize Within Reach: The Presidency        </h3>
<h3 class="story-heading">
        Despite Climate Change Vow, China Pushes to Dig More Coal        </h3>
<h3 class="story-heading">
        Frank Bruni: The Case for Mitt Romney        </h3>
<h3 class="story-heading">
        F.D.A. Agrees to New Trials for Ecstasy as Relief for PTSD Patients        </h3>
<h3 class="story-heading">
        Uber, Seeking to Expand, Defends Itself at EuropeÔÇÖs Highest Court        </h3>
<h3 class="story-heading">
        Third Place? In Auction World, Phillips Is Making Strides        </h3>
<h3 class="story-heading">
        After DemocratsÔÇÖ Losses, Nancy Pelosi Becomes a Symbol of What Went Wrong        </h3>
<h3 class="story-heading">
        On the Runway: Samantha Cameron, From 10 Downing Street to Selfridges        </h3>
<h3 class="story-heading">
        From ÔÇÿRogue OneÔÇÖ: A Rising Star in a Galaxy Far, Far Away        </h3>
<h3 class="story-heading">
        Domestic Abuse Killings Climb as Murders Drop, Frustrating New York Officials        </h3>
<h3 class="story-heading">
        On Pro Basketball: Russell Westbrook: The Triple-Double Machine        </h3>
<h3 class="story-heading">
        Beyond ÔÇÿHamilton,ÔÇÖ for Better and for Worse        </h3>
<h3 class="story-heading">
        A Wrenching Choice for Alaska Towns in the Path of Climate Change        </h3>
<h3 class="story-heading">
        Mark Taimanov, World-Class Chess and Piano Player, Dies at 90        </h3>
<h3 class="story-heading">
        Watched All of the ÔÇÿGilmore GirlsÔÇÖ Revival? LetÔÇÖs Talk About It        </h3>
<h3 class="story-heading">
        Why Gunshot Victims Have Reason to Like the Affordable Care Act        </h3>
<h3 class="story-heading">
        Skiing the Alps, Making It My Own        </h3>
<h3 class="story-heading">
        Books of The Times: Kathleen CollinsÔÇÖs ÔÇÿWhatever Happened to Interracial Love?ÔÇÖ        </h3>
<h3 class="story-heading">
        Professor Watchlist Is Seen as Threat to Academic Freedom        </h3>
<h3 class="story-heading">
        Restaurant Review: Filipino Food Worth the Wait at Bad Saint in Washington        </h3>
<h3 class="story-heading">
        Opinion: The Thin Gene        </h3>
<h3 class="story-heading">
        360 View: An Uncertain Future for East Village Rowhouses        </h3>
<h3 class="story-heading">
        Work-Life Balance: What Donald Trump Might Do for Working-Class Families        </h3>
<h3 class="story-heading">
        Feature: The End of the Anglo-American Order        </h3>
<h3 class="story-heading">
        Driven: Video Review: A New Porsche Cayman, and Still a DriverÔÇÖs Dream        </h3>
<h3 class="story-heading">
        Die Antwoord: The Real Zef Rappers of Beverly Hills        </h3>
<h3 class="story-heading">
        Decades in the Making: Fidel CastroÔÇÖs Obituary        </h3>'''

pattern =  '<h3 class="story-heading">\n(.+)</h3>'
m = re.findall(pattern, string)
for i in m:
    print i
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群