Search code examples
pythonpython-3.xhttpsurlliburlopen

Python 3 : HTTP Error 405: Method Not Allowed


I'm getting 'HTTP Error 405: Method Not Allowed' error. My code is

import urllib.request
import urllib.parse

try:
    url = 'https://www.google.com/search'
    values = {'q': 'python programming tutorials'}

    data = urllib.parse.urlencode(values)
    data = data.encode('utf-8')  # data should be bytes
    headers = {}
    headers['User-Agent'] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36"
    req = urllib.request.Request(url, data,  headers = headers)
    resp = urllib.request.urlopen(req)
    print("HERE")
    respData = resp.read()
    saveFile = open('withHeaders.txt', 'w')
    saveFile.write(str(respData))
    saveFile.close()
except Exception as e:
    print(e)

The error I guess is in req = urllib.request.Request(url, data, headers = headers). What is the error, syntactical? What should be changed in code? And any conceptual mistake do correct me.


EDIT

Concept:

def URLRequest(url, params, method="GET"):
    if method == "POST":
        return urllib2.Request(url, data=urllib.urlencode(params))
    else:
        return urllib2.Request(url + "?" + urllib.urlencode(params))

Solution

  • You can use Requests library instead. It's much cleaner than urllib

    import requests
    q = 'Whatever you want to search'
    url = 'https://www.google.com/search'
    response = requests.get(url+'?'+'q='+q)
    saveFile = open('response.txt', 'w')
    savefile.write(response.text)
    savefile.close()
    

    Or if you want to stick to the urllib , you can do this:

    import urllib.request
    url = 'https://www.google.com/search'
    q = 'Search Query'
    headers = {'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36"}
    request = urllib.request.Request(url+'?'+'q='+q, headers=headers)
    response = urllib.request.urlopen(request).read() # the text of the response is here
    saveFile = open('withHeaders.txt', 'w')
    saveFile.write(str(response))
    saveFile.close()