如何使用urllib或请求在Python中进行Google搜索 [英] How to Google in Python Using urllib or requests

查看:70
本文介绍了如何使用urllib或请求在Python中进行Google搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python 3中使用Google进行搜索的正确方法是什么?我已经尝试过requestsurllib用于Google页面.当我只是res = requests.get("https://www.google.com/#q=" + query)时,返回的HTML与我在Safari中检查Google页面时使用的HTML相同. urllib也会发生同样的情况.当我使用Bing时,也会发生类似的情况.我熟悉AJAX.但是,现在似乎已贬值.

What is the proper way to Google something in Python 3? I have tried requests and urllib for a Google page. When I simply res = requests.get("https://www.google.com/#q=" + query) that doesn't come back with the same HTML as when I inspect the Google page in Safari. The same happens with urllib. A similar thing happens when I use Bing. I am familiar with AJAX. However, it seems that that is now depreciated.

推荐答案

在python中,如果您未在http请求中手动指定用户代理标头,则python将默认为您添加,Google可以检测到它,并且可能禁止它.

In python, if you do not specify the user agent header in http requests manually, python will add for you by default which can be detected by Google and may be forbidden by it.

如果有帮助,请尝试以下操作.

Try the following if it can help.

import urllib
yourUrl = "post it here"
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}  
req = urllib.request.Request(yourUrl, headers = headers)
page = urllib.request.urlopen(req)

这篇关于如何使用urllib或请求在Python中进行Google搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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