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
4

1 に答える 1

1

JavaScript を使用してより多くのコンテンツをロードするため、ここでは urllib-parser コンボを使用できません。これを行うには、(javascript をサポートする) 完全なブラウザー エミュレーターが必要です。これまでSeleniumを使用したことはありませんが、これが可能であり、 Python バインディングがあると聞いたことがあります。

ただし、非常に予測可能な形式を使用していることがわかりました

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
于 2012-08-03T18:32:53.730 に答える