1

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))

2 Answers2

1

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()
Alireza
  • 168
  • 1
  • 7
0

Here in reference to www.pythonforbeginners

# Importing the module
import urllib.request


# your search text
text="hi google"

# Define the url
url = 'http://www.google.com/#q='+text

# Add your headers
headers = {'User-Agent' : 'Mozilla 5.10'}

# Create the Request. 
request = urllib.request.Request(url, None, headers)

# Getting the response
response = urllib.request.urlopen(request)

# Print the headers
print (response.read())
Pavan Kumar T S
  • 1,539
  • 2
  • 17
  • 26