Python搜索抓取

2024-04-26 13:33:04 发布

您现在位置:Python中文网/ 问答频道 /正文

如何编写与浏览器交互的Python程序?一、 我想写一个程序,将字符串从字典(或列表)中键入(粘贴)到Google中,如果结果不是“找到0个匹配项”,那么将Google找到的第一个页面的URL复制到文本文件中。我的主要问题是我不知道如何让python在浏览器中执行任务。(如何使其粘贴字符串,“按搜索”,查看是否有任何结果…)


Tags: 字符串程序url列表键入字典粘贴google
2条回答

ajaxapi不再可用,响应指示您使用Google的自定义搜索API。在

{"responseData": null, "responseDetails": "The Google Web Search API is no longer available. Please migrate to the Google Custom Search API (https://developers.google.com/custom-search/)", "responseStatus": 403}

你不需要刮胡子。Google提供了一个可以使用的搜索api。你可以这样做:

import requests

api_url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=%s'
query = 'xxx'
query_url = api_url %query
response = requests.get(query_url).json()
results = response['responseData']['results']
if results:
   first_result_url = results[0]['url']

相关问题 更多 >