【问题标题】:I'm unable to get items in a list from a webpage using BeautifulSoup我无法使用 BeautifulSoup 从网页中获取列表中的项目
【发布时间】:2012-03-18 12:47:32
【问题描述】:

我最近一直在阅读 BeautifulSoup 文档以熟悉如何解析网站。所以这对我来说很新鲜。谁能告诉我为什么我无法获得网站 (http://www.news-record.com) 上列出的最新头条新闻?

代码如下:

import urllib2
import BeautifulSoup


page = urllib2.urlopen("http://www.news-record.com/")

soup = BeautifulSoup.BeautifulSoup(page)



headlines = []



headline = soup.find('a', ({"class" : "nrcTxt_headline"}))
while headline:
    url = headline.findParent('div')['id']
    headlines.append([url, headline.string])
    headline = headline.findNext('span', {'class' : "nrcTxt_headline"})


print soup.headline

这是网站中感兴趣的部分:

<div class="nrcNav_menu">
   <ul>
    <li class="nrcTxt_menu1 nrcBlk_comboModTab nrc_default nrc_active">
     <a href="#nrcMod_FP_Breaking">
      Latest Headlines
     </a>
    </li>
    <li class="nrcTxt_menu2 nrcBlk_comboModTab nrc_itemLast">
     <a href="#nrcMod_FP_MostRead">
      Most Read
     </a>
    </li>
   </ul>
  </div>
  <div id="nrcMod_FP_Breaking" class="nrcBlk_comboModPage nrc_default nrc_active">
   <h4 class="nrcTxt_modHed">
    <span class="nrcTxt_label">
     Latest Headlines
    </span>
   </h4>
   <ul class="nrcBlk_artList">
    <li class="nrcBlk_artHedOnly nrcBlk_art nrcBlk_art4">
     <a class="nrcTxt_headline" href="/content/2012/02/28/article/city_hosts_earth_day_recycling_contest">
      City hosts Earth Day recycling contest
     </a>
     <span class="nrcBlk_pubdate">
      <span class="nrc_sep">
       (
      </span>
      <!-- COLLAPSE WHITESPACE

                                -->
      <span class="nrc_val">
       3:53 pm
      </span>
      <!-- COLLAPSE WHITESPACE

                                -->
      <span class="nrc_sep">
       )
      </span>
     </span>
    </li>
    <li class="nrcBlk_artHedOnly nrcBlk_art nrcBlk_art5">
     <a class="nrcTxt_headline" href="/content/2012/02/28/article/got_bull_rockingham_deputies_seek_900_pound_beast">
      Got bull? Rockingham deputies seek 900-pound beast
     </a>

下面的代码基本上可以满足我的要求。但是文本在括号中:

[u'Daily Deal: strength coaching sessions ']
[u'Teen dating safety tips']
[u'Splitting up the family']

如果我删除代码中的括号,我会收到错误:

import urllib2
import BeautifulSoup






site = "http://www.news-record.com/"

page = urllib2.urlopen("http://www.news-record.com/")
soup = BeautifulSoup.BeautifulSoup(page)

headlines = []


for headline in soup.findAll('a', {"class" : "nrcTxt_headline"}):
    url = headline.findParent('div', {"class":"nrcNav_menu"})
    print ([headline.string])

【问题讨论】:

  • 我建议你使用 lxml 解析器,它不使用正则表达式,运行起来比 BeautifulSoup 更糟。

标签: python beautifulsoup urllib2


【解决方案1】:

您将 find 参数包装在一个元组中,并没有真正尽可能地使用生成器..

soup.find('a', ({"class" : "nrcTxt_headline"}))

你想要

soup.find('a', {"class" : "nrcTxt_headline"})

例子:

for headline in soup.find('a', {"class" : "nrcTxt_headline"}):
    url = headline.findParent('div')['id']
    headlines.append([url, headline.string])

【讨论】:

    【解决方案2】:

    试试下面的代码:

    import urllib2
    import BeautifulSoup
    
    site = "http://www.news-record.com/"
    
    page = urllib2.urlopen("http://www.news-record.com/")
    soup = BeautifulSoup.BeautifulSoup(page)
    
    headlines = []
    
    headlineList = soup.findAll('a', {"class" : "nrcTxt_headline"})
    for headline in headlineList:
        headlines.append(site+str(headline.get('href')))
    
    print headlines
    

    【讨论】:

    • 它给出的结果与我的代码相同。我正在尝试获取页面上最新标题下的 10 个标题(href):news-record.com
    • 如果你只想要链接...我编辑了上面的代码,它应该返回一个链接列表。
    猜你喜欢
    • 2018-06-01
    • 2018-06-10
    • 2020-01-28
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    • 2019-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多