You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在不使用Cookie的情况下通过代码获取Google搜索“games site:pastebin.com”的结果?

Answer

First off, Google actively blocks automated requests to its search pages—this is why you're seeing the login prompt even without cookies. Their anti-bot systems flag requests that don’t look like they’re coming from a real human using a browser. Here are your options to work around this (with varying degrees of reliability and legitimacy):

1. Mimic a real browser with request headers

The simplest fix is to add proper HTTP headers to your requests to make them appear like they’re coming from a standard web browser. Google checks for headers like User-Agent, Accept-Language, and others to verify the request source.

Python Example:

import requests

# Mimic a modern Chrome browser on Windows
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
    'Accept-Language': 'en-US,en;q=0.9',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
}

response = requests.get(
    "https://www.google.com/search?q=games+site%3Apastebin.com",
    headers=headers
)

# Verify the response (check status code and preview content)
print(response.status_code)
print(response.text[:500])  # Print first 500 characters to confirm results load

Dart Example:

import 'package:http/http.dart' as http;

void main() async {
  final headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
    'Accept-Language': 'en-US,en;q=0.9',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
  };

  final response = await http.get(
    Uri.parse("https://www.google.com/search?q=games+site%3Apastebin.com"),
    headers: headers,
  );

  print(response.statusCode);
  print(response.body.substring(0, 500));
}

Caveat: This might work temporarily, but Google’s anti-bot systems are constantly evolving. If you make too many requests too quickly, you’ll still get blocked (even with headers). To mitigate this, you could add delays between requests, rotate User-Agent strings, or use rotating proxies—but this gets complicated and is still against Google’s Terms of Service.

2. Use Google's Custom Search API (Legitimate Option)

If you want a reliable, allowed way to get Google search results programmatically, use the Custom Search JSON API. This is Google’s official method for automated search queries.

How it works:

  • You’ll need a Google Cloud API key and a Custom Search Engine (CSE) ID.
  • The API lets you specify search queries, including site-specific searches like site:pastebin.com games.
  • It returns results in JSON format, making parsing straightforward.

Note: There’s a free tier (100 queries per day), and paid tiers for higher volumes. This is the only legal and sustainable way to get Google search results via code.

Important Warning

Scraping Google’s search results directly violates their Terms of Service. Even if you get it working temporarily, Google can block your IP, change their page structure (breaking your scraper), or take other action. The Custom Search API is the only recommended approach for long-term use.

内容的提问来源于stack exchange,提问作者Akash Pattnaik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 21:52:45