2017-09-06 4 views
1

問題はちょうどGoogleのリクエストを正しく形成できないため、ブラウザ(Chrome)使用していますが、動作していないようです。これの最終的な目標は、検索用語、解像度、および要求のjpgのファイルタイプを指定し、そのイメージをフォルダにダウンロードできることです。任意の提案は歓迎されると感謝私のコード今のところGoogleでは、Pythonリクエストを使用してGoogleサーバーのリクエストを事前に作成する方法を理解していません。

相続人は、事前になります:それは戻っているので、私は唯一のものイム省は、ページのHTMLを知っている

def funRequestsDownload(searchTerm): 
print("Getting image for track ", searchTerm) 
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36', 'content-length': bytes(searchTerm, 'utf-8')} 
queryStringParameters = {'hl': "en", "tbm": "isch", "source": "hp", "biw":1109, "bih": 475, "q": "SEARCH TERMS", "oq":"meme", "gs_l":"img.3..35i39k1j0l9.21651.21983.0.22205.10.10.0.0.0.0.131.269.2j1.3.0....0...1.1.64.img..7.3.267.0.4mTf5BYtfj8"} 
payload = {'value': searchTerm} 
url = 'http://www.google.co.uk' 
dataDump = requests.get(url, data=payload, headers=headers, "Query String Parameters"=queryStringParameters) 
temp = dataDump.content 
with open('C:/Users/Jordan/Desktop/Music Program/temp.html', 'w') as file: 
    file.write(str(temp)) 
    file.close 
return(temp) 
print("Downloaded image for track ", searchTerm) 

サイドノートで、これがあります悪いリクエストページと私は上記のエラーを見てみたい。

答えて

2

Google doesn't like people using scraping to access search results。彼らは代わりにあなたのAPIを使う方が好きです。

彼らが提供するAPIはGoogle Custom Searchと呼ばれます。画像の検索をサポートしています。 APIを使用するには、AdSenseアカウントが必要です。そこから取得したAPIキーを使用してAPI呼び出しを行います。あなたがヒットしたいと思う

URLは要求を介し戻ってあなたの結果をJSONファイルを取得する

searchUrl = "https://www.googleapis.com/customsearch/v1?q=" + \ 
      searchTerm + "&start=" + startIndex + "&key=" + key + "&cx=" + cx + \ 
      "&searchType=image" 

パスです。

Here'sさらに読む。

関連する問題