我无法使用BeautifulSoup从网页列表中获取项目

时间:2012-02-29 01:10:33

标签: python beautifulsoup urllib2

我最近一直在阅读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])

2 个答案:

答案 0 :(得分:0)

请尝试以下代码:

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

答案 1 :(得分:0)

你将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])