2014-02-18 46 views
1

此代码不打印公司名单作为reqiured。 它没有达到第一个标签内 如果我在第一个标签内写入“print'文字'”,它不会打印它。 BeautifulSoup正在为不同的网站编写不同的代码。 任何建议为什么它不起作用?标签里面的beautifulsoup无法正常工作

from bs4 import BeautifulSoup 
import urllib 
request = urllib.urlopen('http://www.stockmarketsreview.com/companies_sp500/') 
html = request.read() 
request.close() 
soup = BeautifulSoup(html) 
for tags in soup.find_all('div', {'class':'mainContent'}): 
    for row in tags.find_all('tr'): 
     for column in row.find_all('td'): 
      print column.text 
+0

此代码对我的作品.. – Totem

+0

也许检查您的缩进实际代码。当你运行这个时,你会得到什么? – Totem

+0

你是否在使用'lxml'作为解析器?某些版本的lxml与某些版本的基础libxml在解析某些HTML时遇到了麻烦。 –

回答

0

我BeautifulSoup 3,这似乎正常工作:

import BeautifulSoup as BS 
import urllib 
request = urllib.urlopen('http://www.stockmarketsreview.com/companies_sp500/') 
html = request.read() 
request.close() 
soup = BS.BeautifulSoup(html) 

try: 
    tags = soup.findAll('div', attrs={'class':'mainContent'}) 
    print '# tags = ' + str(len(tags)) 
    for tag in tags: 
     try:   
     tables = tag.findAll('table') 
     print '# tables = ' + str(len(tables)) 
     for table in tables:    
      try: 
       rows = tag.findAll('tr') 
       for row in rows: 
        try: 
        columns = row.findAll('td') 
        for column in columns: 
         print column.text 
        except: 
        e = 1 
        # print 'Caught error getting td tag under ' + str(row) 
        # This is okay since some rows have <th>, not <td> 
      except: 
       print 'Caught error getting tr tag under ' + str(table) 
     except: 
     print 'Caught error getting table tag under ' + str(tag) 
except: 
    print 'Caught error getting div tag' 

我相信你需要更换 '的findAll' 与 'find_all'。

输出看起来是这样的: enter image description here

+0

我使用了你的代码,但是它打印出'#tags = 0'。我想我的系统上可能没有安装使用此代码的网站。但是我为其他网站使用了这种类型的代码。任何建议我需要安装以运行此代码 – Kundan

+0

也许BS4与3.2.1的差别比我想象的要大......我在运行时添加了脚本输出的图片。我看到'#tags = 1',然后是您想要解析的公司列表。 – bornruffians