0
votes

Update: Issue seems to be with Windows Powershell. Program works in Python IDLE.

So I have installed requests, urllib3 module properly. But whenever I try to use requests, I get HTTP 405 error. Please check the attached screenshot for my code and the error I get.

  • NOTE: I tried attaching images of my code and error but StackOverflow app gave me an error.

  • NOTE 2: I have tried GET method too but it doesn't work either, it throws the same HTTP 405 error.

My code:

from bs4 import BeautifulSoup
import requests

file = requests.post("https://w3schools.com/python/demopage.htm")

soup = BeautifulSoup(file,"lxml");

print(soup.prettify())

Error I get:

Traceback (most recent call last): File "requestspractice.py", line 1, in import requests File "C:\Users\Prasanna\AppData\Local\Programs\Python\Python36\lib\site-packages\requests__init__.py", line 43, in import urllib3 File "C:\Users\Prasanna\Python1\urllib3.py", line 15, in resp = urllib.request.urlopen(req) File "C:\Users\Prasanna\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 223, in urlopen return opener.open(url, data, timeout) File "C:\Users\Prasanna\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 532, in open response = meth(req, response) File "C:\Users\Prasanna\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 642, in http_response 'http', request, response, code, msg, hdrs) File "C:\Users\Prasanna\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 570, in error return self._call_chain(*args) File "C:\Users\Prasanna\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 504, in _call_chain result = func(*args) File "C:\Users\Prasanna\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 650, in http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 405: Method Not Allowed

1
405 is not allowed, that might mean you can not post to that url, but your code ran fine on my computer on the post part - Phung Duy Phong
I have tried GET method as well but it gives me same error. - Prasanna Deshpande
Also you are not using urllib3 - Phung Duy Phong
Works for me with python-requests, with bot get and post methods. Can you get the page to load in your browser (on the same computer) ? Are you behind a firewall / proxy / whatever ? - bruno desthuilliers
I haven't activated any firewall, I don't know if Windows 10 have any firewall by default. Page loads correctly in the browser. - Prasanna Deshpande

1 Answers

0
votes

I believe what you want to do is GET the page rather than POST anything to it.

file = requests.get("https://w3schools.com/python/demopage.htm")