【问题标题】:Can I expand Google Image seach screen by scrolling down programatically?我可以通过以编程方式向下滚动来扩展 Google 图片搜索屏幕吗?
【发布时间】:2014-08-09 10:06:33
【问题描述】:

我尝试从 Google 中删除一些图片,但该网站的向下滚动扩展限制了我只能下载一定数量的图片。有没有办法通过python代码来模仿它?例如,如果可能,可能会在这种情况下使用 Machanize。

所以我需要模拟谷歌图片搜索的向下滚动扩展,以增加返回结果的数量并获取图片网址以报废。

【问题讨论】:

  • 我知道selenium 可以轻松滚动页面
  • 您是否考虑过使用Google Custom Search API 来抓取使用 Google 搜索的图像?
  • API 也是有限的。我需要为我的东西买一大包。

标签: python web-scraping google-image-search


【解决方案1】:

这可能会让你很快被禁止,但我不确定。这需要 BeautifulSoup 和 requests。

import requests
from bs4 import BeautifulSoup

s = requests.session()
s.headers.update({"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"})

URL = "https://www.google.dk/search"
images = []

def get_images(query, start):
    screen_width = 1920
    screen_height = 1080
    params = {
        "q": query,
        "sa": "X",
        "biw": screen_width,
        "bih": screen_height,
        "tbm": "isch",
        "ijn": start/100,
        "start": start,
        #"ei": "" - This seems like a unique ID, you might want to use it to avoid getting banned. But you probably still are.
    }

    request = s.get(URL, params=params)
    bs = BeautifulSoup(request.text)

    for img in bs.findAll("div", {"class": "rg_di"}):
        images.append(img.find("img").attrs['data-src'])


#Will get 400 images.
for x in range(0, 5):
    get_images("cats", x*100)

for x in images:
    print x

【讨论】:

  • @alexmulo - 谷歌不喜欢你抓取他们的服务,因为他们大多数都有 API 端点。如果您的查询速度过快或过于系统化,Google 往往会在其搜索引擎上使用验证码来打击您。另外,我编写脚本的方式和打算完成的方式可能略有不同,这可以通过 google 发现。
猜你喜欢
  • 2011-10-11
  • 2015-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多