向Google搜索发起GET请求

14

我正在尝试从谷歌获取带有搜索结果的HTML。例如,通过发送GET请求到:

https://www.google.ru/?q=1111

但是如果在浏览器中一切正常,当我尝试使用curl或在Google中查看源代码时,只会看到一些JavaScript代码,没有搜索结果。这是什么类型的保护?我该怎么办?


您可以通过删除curl用户代理来欺骗系统。 - user2284570
搜索结果在https://www.google.ru/search?q=1111。https://www.google.ru/?q=1111是一个Google搜索主页,搜索框位于中心位置。请尝试查看https://www.google.ru/search?q=1111的页面源代码。 - ilyazub
4个回答

11

现在您需要使用Google搜索API来进行GET请求。

所有其他方法已被阻止。


2
问题是它必须针对特定的网站制作。 - user2284570
1
注意:此操作超出免费请求次数,将产生额外费用。 - jasonleonhard

3
你的问题中提到的页面是带有输入字段的Google搜索页面。

Screenshot of https://www.google.ru/?q=1111

搜索结果页面是这个:
https://www.google.ru/search?q=1111

轮换代理和用户代理,并延迟类似请求,以获取更少的封禁次数从Google搜索结果页面获取HTML。
或者使用SerpApi来访问HTML和从中提取的数据。它有一个免费试用期。
curl -s 'https://serpapi.com/search?q=coffee'

输出

{
  // Omitted

  "organic_results": [
    {
      "position": 1,
      "title": "Coffee - Wikipedia",
      "link": "https://en.wikipedia.org/wiki/Coffee",
      "displayed_link": "en.wikipedia.org › wiki › Coffee",
      "snippet": "Coffee is a brewed drink prepared from roasted coffee beans, the seeds of berries from certain Coffea species. When coffee berries turn from green to bright red ...",
      "sitelinks": {
        "expanded": [
          {
            "title": "History",
            "link": "https://en.wikipedia.org/wiki/History_of_coffee",
            "snippet": "The history of coffee dates back to the 15th century, and possibly ..."
          },
          {
            "title": "International Coffee Day",
            "link": "https://en.wikipedia.org/wiki/International_Coffee_Day",
            "snippet": "International Coffee Day (1 October) is an occasion that is ..."
          },
          {
            "title": "List of coffee drinks",
            "link": "https://en.wikipedia.org/wiki/List_of_coffee_drinks",
            "snippet": "Milk coffee - Nitro cold brew coffee - List of coffee dishes - ..."
          },
          {
            "title": "Portal:Coffee",
            "link": "https://en.wikipedia.org/wiki/Portal:Coffee",
            "snippet": "Coffee is a brewed drink prepared from roasted coffee beans, the ..."
          },
          {
            "title": "Coffee bean",
            "link": "https://en.wikipedia.org/wiki/Coffee_bean",
            "snippet": "A coffee bean is a seed of the Coffea plant and the source for ..."
          },
          {
            "title": "Geisha",
            "link": "https://en.wikipedia.org/wiki/Geisha_(coffee)",
            "snippet": "Geisha coffee, sometimes referred to as Gesha coffee, is a type of ..."
          }
        ],
        "list": [
          {
            "date": "Color‎: ‎Black, dark brown, light brown, beige"
          }
        ]
      },
      "rich_snippet": {
        "bottom": {
          "detected_extensions": {
            "introduced_th_century": 15
          },
          "extensions": [
            "Introduced‎: ‎15th century",
            "Color‎: ‎Black, dark brown, light brown, beige"
          ]
        }
      },
      "cached_page_link": "https://webcache.googleusercontent.com/search?q=cache:U6oJMnF-eeUJ:https://en.wikipedia.org/wiki/Coffee+&cd=2&hl=sv&ct=clnk&gl=se",
      "related_pages_link": "https://www.google.se/search?gl=se&hl=sv&q=related:https://en.wikipedia.org/wiki/Coffee+coffee&sa=X&ved=2ahUKEwjJ9p2p_KXuAhVlRN8KHf22D8wQHzABegQIAhAJ"
    }
  },

  // ...
}

免责声明:我在SerpApi工作。


报告商业广告 - M22

2
为了帮您更好地解决问题,这里提供一些额外的答案。首先,只要您不通过获取数据损害谷歌服务(比如DoS攻击),爬取谷歌是完全合法的。而且方法并没有被封锁,只是不那么简单。
速度取决于使用的方法,不必非常缓慢。如果需要的话,您可以在一分钟内爬取数万个关键词页面。
此处有一个更好的答案:从Google结果中抓取数据是否可行? 使用curl遇到的问题确实来自于保护措施,谷歌不允许自动访问,并且它有非常复杂的检测算法。它们包括简单的用户代理检查(这就是直接阻止你的原因)以及人工智能,试图检测不寻常的查询或相关查询。

1
你可以在浏览器中加载它,然后通过Javascript抓取结果。
或者你可以使用Google API,但如果每天请求超过100次,则需要付费。

1
你的方法很快会被阻挡。谷歌将展示一个“我们想确保你不是机器人…”的屏幕,你必须解决验证码才能继续搜索。 - Brian Smith
@BrianSmith,是的,当然会。但仅限于所有页面中的一次。 - UndeadDragon
@John 每个查询只有一次机会(在结果出现之前),然后在您点击页面时将不再出现验证码。每个查询都是如我所说的。 - UndeadDragon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接