2015-09-25 75 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上。