2011-07-09 22 views
0

私はウェブでスクラップするのはPythonでは新しいので、私はこのことを正しく行っているかどうかわかりません。urllib2で503のエラーを回避する

私はBeautifulSoupを呼び出して、Google検索の最初の10ページからのURLを解析するスクリプトを使用しています。 stackoverflow.comでテストしたところ、すぐに使い慣れたものでした。私は別のサイトで数回テストし、スクリプトが本当に上位のGoogleページリクエストで動作しているかどうかを確認しようとしています。私はテストするために別のURLに切り替え、いくつかの低ページリクエストで作業した後、503dしました。今私がそれに渡すすべてのURLは503です。助言がありますか?

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,10): 
    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 

答えて

5

Googleの利用規約では自動照会が許可されていません。エットーレは、検索結果をこすることは、私たちのToSに反している、と述べたよう Unusual traffic from your computer ともGoogle Terms of service

+0

これは意味があります。 Google Web Search APIを使用してこれを行う方法はありますか?私はそこにいるはずだと思うが、AJAXやJavascriptの経験はあまりない。 – AI52487963

0

: 情報についてはこちらの記事を参照してください。しかし、WebSearch api、具体的にはdocumentationの一番下のセクションをチェックしてください。非javascipt環境からAPIにアクセスする方法についてのヒントがあります。

関連する問題