【问题标题】:scraping google web results not working抓取谷歌网络结果不起作用
【发布时间】:2015-12-04 23:06:41
【问题描述】:

为什么以下内容无法抓取 google 的搜索结果?

尝试打开响应并抛出 HTTPError 时失败。我查看了其他问题,据我所知,我已经正确完成了编码等。

我知道我没有包括捕捉错误等,这只是一个缩小版本。

def scrape_google(query):

    url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&"
    headers = {'User-Agent': 'Mozilla/5.0'}
    search = urllib.parse.urlencode({'q': " ".join(term for term in query)})
    b_search = search.encode("utf-8")
    response = urllib.request.Request(url, b_search, headers)
    page = urllib.request.urlopen(response)

【问题讨论】:

  • 该网址应该在浏览器中工作吗?我得到一个 404。
  • http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=searchterm 为我工作。我不小心在帖子中放了一个. 而不是/,但我正确使用了这个并且我已经纠正了错误

标签: python python-3.x web-scraping beautifulsoup bs4


【解决方案1】:

它不起作用,因为该 URL 的返回是 JSON 格式。如果您使用该 URL 并输入如下搜索词:

http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=bingo

您将以 JSON 格式返回结果,这不是 beautifulsoup 设置的处理方式。 (但比刮要好很多)

{"responseData": 
     {"results":
   [{"GsearchResultClass":"GwebSearch","unescapedUrl":"http://www.pogo.com/games/bingo-luau","url":"http://www.pogo.com/games/bingo-

//etc

编辑添加:

使用请求:

url = ('http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=bingo')
resp = requests.get(url)
print(resp.content)

生成:

b'{"responseData": {"results":[{"GsearchResultClass":"GwebSearch","unescapedUrl":"http://www.pogo.com/games/b...
//etc    

【讨论】:

  • 您可能会发现使用requests 库更容易。
  • requests 是一个更好的选择。要读取 json 数据,您可以调用 response.json(),它更短更甜
【解决方案2】:
  • 正如人们在 cmets 中所说,requests 库(以及(如果需要)与 beautifulsoup 结合使用)更好。我回答了关于抓取谷歌搜索结果here的问题。

  • 或者,您可以使用来自 SerpApi 的第三方 Google Organic Results API。这是一个免费试用的付费 API。

查看playground 进行测试。

要集成的代码(说你想抓取标题、摘要和链接):

import os
from serpapi import GoogleSearch

params = {
    "engine": "google",
    "q": "best lasagna recipe ever",
    "api_key": os.getenv("API_KEY"),
}

search = GoogleSearch(params)
results = search.get_dict()

for result in results["organic_results"]:
   print(f"Title: {result['title']}\nSummary: {result['snippet']}\nLink: {result['link']}")

输出:

Title: The BEST Lasagna Recipe Ever! | The Recipe Critic
Summary: How to Make The BEST Classic Lasagna Ever. Sauté meat then simmer with bases and seasonings: In a large skillet over medium high heat add the olive oil and onion. Cook lasagna noodles: In a large pot, bring the water to a boil. Mix cheeses together: In medium sized bowl add the ricotta cheese, parmesan, and egg.
Link: https://therecipecritic.com/lasagna-recipe/

Title: The Most Amazing Lasagna Recipe - The Stay At Home Chef
Summary: The Most Amazing Lasagna Recipe is the best recipe for homemade Italian-style lasagna. The balance ... This recipe is so good—it makes the kind of lasagna people write home about! ... Hands down absolutely the best lasagna recipe ever!
Link: https://thestayathomechef.com/amazing-lasagna-recipe/

免责声明,我为 SerpApi 工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多