0

I'm using discord.py to code a bot, I'm trying to get links from google search based on user keyword

@client.event
async def on_message(message):


    ## GOOGLE SEARCH
    #####################################################################################

    if message.content.startswith('!google'):
        msg   = message.content
        query = msg.split(" ")[1]


        headers  = {  }
        headers['User-Agent'] = 'Mozilla/5.0'
        url      = 'http://www.google.com/search?q='
        value    = { 'q' : query }
        data     = urllib.parse.urlencode(value)
        data     = data.encode('utf-8')
        req      = urllib.request.Request(url, data)
        response = urllib.request.urlopen(req)
        final    = response.read()

thought I'm getting this as an error

urllib.error.HTTPError: HTTP Error 405: Method Not Allowed

Itachi Sama
  • 886
  • 1
  • 6
  • 18
  • Google does not like you very much ;-) – Menachem Hornbacher May 15 '17 at 00:18
  • ah i see, so it's a headers problem? – Itachi Sama May 15 '17 at 00:28
  • More then likely. Seems google is sending a 405. Try intercepting the request (or logging it) and trying it manually – Menachem Hornbacher May 15 '17 at 00:29
  • [this](http://stackoverflow.com/questions/38484268/how-to-google-in-python-using-urllib-or-requests) seems to address your question I believe. [here](http://stackoverflow.com/questions/38635419/searching-in-google-with-python) is a question based off of some package that does this, in the event you are in general looking to do this through code and not necessarily just through urllib2. – idjaw May 15 '17 at 00:31

0 Answers0