python - 我需要帮助网络抓取

标签 python python-2.7 web-scraping

所以我想从 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

最佳答案

你不能在这里使用 urllib-parser 组合,因为它使用 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

关于python - 我需要帮助网络抓取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11656659/

相关文章:

python - 使用 BeautifulSoup 查找标签并评估它是否符合某些标准

python - Python如何知道函数的结尾在哪里?

Python Tkinter 只读文本字段

python - 谷歌抓取 href 值

python - 在 scrapy 中启用 deltafetch

python - python 中描述符概念的行为(令人困惑)

python - 如何知道 matplotlib 安装启用的功能?

python - 有没有办法用python自动化/脚本视频编辑?

python - 通过 Web 客户端连接到服务器时出现 SSL 握手错误

python - 确定一个字符串可能是一个日期时间对象