2012-07-25 83 views
2

所以我想从visual.ly中抓取可视化,但是现在我不明白“显示更多”按钮是如何工作的。截至目前,我的代码将获取图像链接,图像旁边的文本以及页面的链接。我想知道“显示更多”按钮的功能,因为我将尝试循环使用页面数量。截至目前,我不知道如何通过每一个单独循环。任何想法,我如何可以循环,并继续获得比他们最初显示的更多的图像?我需要帮助网络抓取

from BeautifulSoup import BeautifulSoup 
import urllib2 
import HTMLParser 
import urllib, re 

counter = 1 
columnno = 1 
parser = HTMLParser.HTMLParser() 

soup = BeautifulSoup(urllib2.urlopen('http://visual.ly/?view=explore& type=static#v2_filter').read()) 

image = soup.findAll("div", attrs = {'class': 'view-mode-wrapper'}) 

if columnno < 4: 
    column = image[0].findAll("div", attrs = {'class': 'v2_grid_column'}) 
    columnno += 1 
else: 
    column = image[0].findAll("div", attrs = {'class': 'v2_grid_column last'}) 

visualizations = column[0].findAll("div", attrs = {'class': '0 v2_grid_item viewmode-item'}) 

getImage = visualizations[0].find("a") 

print counter 

print getImage['href'] 

soup1 = BeautifulSoup(urllib2.urlopen(getImage['href']).read()) 

theImage = soup1.findAll("div", attrs = {'class': 'ig-graphic-wrapper'}) 

text = soup1.findAll("div", attrs = {'class': 'ig-content-right'}) 

getText = text[0].findAll("div", attrs = {'class': 'ig-description right-section first'}) 

imageLink = theImage[0].find("a") 

print imageLink['href'] 

print getText 

for row in image: 
    theImage = image[0].find("a") 

    actually_download = False 
    if actually_download: 
     filename = link.split('/')[-1] 
     urllib.urlretrieve(link, filename) 

counter += 1 
+1

你已经安装了浏览器中的Web开发工具栏?我觉得这对于形象数据,按钮动作,链接等等的可视化(双​​关不打算)是非常有用的。 – Lenna 2012-07-25 18:58:03

+0

如果打印链接指向正确的资源?这将是调试的第一步。 – 2012-07-25 19:05:36

+0

不,我没有网络开发工具栏,除非你的意思是萤火虫? – user1497050 2012-07-25 19:16:13

回答

1

您不能在这里使用urllib分析器组合,因为它使用JavaScript来加载更多的内容。为了做到这一点,你需要一个完整的强制浏览器模拟器(支持javascript)。我从来没有使用过Selenium,但我听说它这样做,以及具有python binding

然而,我发现,它使用了一个非常明确的形式

http://visual.ly/?page=<page_number> 

其GET请求。也许更简单的方法是去

<div class="view-mode-wrapper">...</div> 

来解析数据(使用上面的url格式)。毕竟,ajax请求必须去一个位置。

那么你可以做

for i in xrange(<whatever>): 
    url = r'http://visual.ly/?page={pagenum}'.format(pagenum=i) 
    #do whatever you want from here