如何从搜索查询中检索谷歌网址

2024-09-28 23:05:00 发布

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

所以我尝试创建一个Python脚本,它将获取一个搜索词或查询,然后在google上搜索该词。然后它应该从搜索词的结果中返回5个URL。在

我花了很多时间试图让PyGoogle工作。但后来发现Google不再支持SOAP API进行搜索,也不再提供新的许可证密钥。简单地说,PyGoogle在这一点上已经死了。在

所以我的问题是。。。做这件事最紧凑/最简单的方法是什么?在

我想完全用Python来完成。在

谢谢你的帮助


Tags: 方法脚本apiurlgoogle时间密钥soap
3条回答

这里,linkxgoogle库来做同样的事情。在

我尝试过类似的方法来获得前10个链接,它也可以计算链接中的单词数。我添加了代码片段供您参考:

import operator
import urllib
#This line will import GoogleSearch, SearchError class from xgoogle/search.py file
from xgoogle.search import GoogleSearch, SearchError
my_dict = {}
print "Enter the word to be searched : "
#read user input
yourword = raw_input()
try:
  #This will perform google search on our keyword
  gs = GoogleSearch(yourword)
  gs.results_per_page = 80
  #get google search result
  results = gs.get_results()
  source = ''
  #loop through all result to get each link and it's contain
  for res in results:
     #print res.url.encode('utf8')
     #this will give url
     parsedurl = res.url.encode("utf8")
     myurl = urllib.urlopen(parsedurl)
     #above line will read url content, in below line we parse the content of that web page
     source = myurl.read()
     #This line will count occurrence of enterd keyword in our webpage
     count = source.count(yourword)
     #We store our result in dictionary data structure. For each url, we store it word occurent. Similar to array, this is dictionary
     my_dict[parsedurl] = count
except SearchError, e:
  print "Search failed: %s" % e
print my_dict
#sorted_x = sorted(my_dict, key=lambda x: x[1])

for key in sorted(my_dict, key=my_dict.get, reverse=True):
    print(key,my_dict[key])

你和pygoogle有什么问题?我知道它不再受支持,但我已经在很多场合使用过这个项目,它将很好地完成您所描述的琐碎任务。在

你的问题确实让我很好奇,所以我去了谷歌,输入了“python谷歌搜索”。Bam,找到this repository。安装了pip,在浏览文档后5分钟内就得到了您的要求:

import google
for url in google.search("red sox", num=5, stop=1):
    print(url)

也许下次再努力一点,好吗?在

使用BeautifulSoup和requests从google搜索结果中获取链接

import requests
from bs4 import BeautifulSoup
keyword = "Facebook" #enter your keyword here
search = "https://www.google.co.uk/search?sclient=psy-ab&client=ubuntu&hs=k5b&channel=fs&biw=1366&bih=648&noj=1&q=" + keyword
r = requests.get(search)
soup = BeautifulSoup(r.text, "html.parser")
container = soup.find('div',{'id':'search'})
url = container.find("cite").text
print(url)

相关问题 更多 >