Python 搜索抓取 [英] Python search scraping

查看:25
本文介绍了Python 搜索抓取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写与浏览器交互的 Python 程序?IE.我想编写一个程序,将字典(或列表)中的字符串键入(粘贴)到 Google 中,如果结果不是找到 0 个匹配项",则复制 Google 找到的(比如说)第一页的 URL(到文本文件).我的主要问题是我不知道如何让 python 在浏览器中做事.(如何让它粘贴字符串,按搜索",看看有没有结果...)

How do I write a Python program that interacts with the browser? I.e. I want to write a program that types (pastes) strings form a dictionary (or a list) into Google and if the result is not "0 matches found" then copies the URL of the (say) first page that Google finds (into a text file). My main problem is that I don't know how to make python do things within the browser. (how to make it paste the strings, "press search", see whether there are any results...)

推荐答案

您无需刮擦.Google 提供了一个您可以使用的搜索 API.你可以这样做:

You don't need to scrape. Google provides a search api that you can use. You can do something like this:

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']

这篇关于Python 搜索抓取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆