楼主: lyhluai
983 2

[其他] Python 大神来帮忙啊! [推广有奖]

  • 0关注
  • 0粉丝

小学生

7%

还不是VIP/贵宾

-

威望
0
论坛币
29970 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
33 点
帖子
2
精华
0
在线时间
6 小时
注册时间
2016-3-14
最后登录
2017-6-5

楼主
lyhluai 发表于 2016-11-30 11:23:26 |AI写论文
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>

关键词:python affordable basketball Washington restaurant 数据库

沙发
马甲1号 发表于 2016-12-1 12:49:23
print(name.text)试试。
已有 1 人评分论坛币 收起 理由
admin_kefu + 5 热心帮助其他会员

总评分: 论坛币 + 5   查看全部评分

藤椅
qxzzxw 发表于 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
已有 2 人评分经验 论坛币 热心指数 收起 理由
admin_kefu + 30 + 5 热心帮助其他会员
giresse + 60 精彩帖子

总评分: 经验 + 60  论坛币 + 30  热心指数 + 5   查看全部评分

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
jg-xs1
拉您进交流群
GMT+8, 2025-12-29 01:08