2015-09-25 26 views
-2

我需要在python的帮助下获得前15页的谷歌搜索结果。我试着用这个答案Extract Google Search Results。但我没有得到先前的结果。我需要150个搜索结果,与python的原始链接。如果有人知道,给我这个解决方案。提前致谢。Python脚本获取150 Google搜索结果

+2

这种问题真的是史诗般的。 Ganeshgm7,请问你可以给你的问题添加一些你尝试的例子吗?那太好了。 – Thomas

回答

0

我得到了150个的搜索结果是这样的:

import sys # Used to add the BeautifulSoup folder the import path 
import urllib2 # Used to read the html document 

if __name__ == "__main__": 
    ### Import Beautiful Soup 
    ### Here, I have the BeautifulSoup folder in the level of this Python script 
    ### So I need to tell Python where to look. 
    sys.path.append("./BeautifulSoup") 
    from BeautifulSoup import BeautifulSoup 

    ### Create opener with Google-friendly user agent 
    opener = urllib2.build_opener() 
    opener.addheaders = [('User-agent', 'Mozilla/5.0')] 

    ### Open page & generate soup 
    ### the "start" variable will be used to iterate through 10 pages. 
    for start in range(0,15): 
     url = "http://www.google.com/search?q=site:stackoverflow.com&start=" + str(start*10) 
     page = opener.open(url) 
     soup = BeautifulSoup(page) 

     ### Parse and find 
     ### Looks like google contains URLs in <cite> tags. 
     ### So for each cite tag on each page (10), print its contents (url) 
     for cite in soup.findAll('cite'): 
      print cite.text 

你只需要之前安装BeautifulSouppip install BeautifulSoup

的代码你提到的链接来了:使用Python wrapper回购Extract Google Search Results

+0

是的。它正在工作。谢谢。但手动检查时略有不同。但我不知道这个原因。? – Ganeshgm7

1

或者,你可以使用SERP API

的说明相当简单:

pip install google-search-results 

和用法是:

from lib.google_search_results import GoogleSearchResults 
query = GoogleSearchResults({"q": "coffee"}) 
html_results = query.get_html() 

更高级的用途在SERP API Github上。